Created
October 11, 2023 05:55
-
-
Save tail-call/9ea3eacaf4c1adf1383c096124d3aca0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Используется для декодирования объектов, которые могут принимать один из двух типов | |
enum EitherDecodableEquatable< | |
Left: Decodable & Equatable, | |
Right: Decodable & Equatable | |
>: Decodable, Equatable { | |
case left(Left) | |
case right(Right) | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
do { | |
self = .left(try container.decode(Left.self)) | |
} catch { | |
self = .right(try container.decode(Right.self)) | |
} | |
} | |
static func == ( | |
lhs: EitherDecodableEquatable, | |
rhs: EitherDecodableEquatable | |
) -> Bool { | |
switch (lhs, rhs) { | |
case (.left(let lhsLeft), .left(let rhsLeft)): | |
return lhsLeft == rhsLeft | |
case (.right(let lhsRight), .right(let rhsRight)): | |
return lhsRight == rhsRight | |
default: | |
return false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment