Created
April 12, 2017 21:53
-
-
Save bfernandesbfs/7da775130f6a7ee62e8efa7e3f99989a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public typealias JSON = [String: Any] | |
public enum Result<A> { | |
case success(A) | |
case failure(Error) | |
public var isSuccess: Bool { | |
switch self { | |
case .success: | |
return true | |
case .failure: | |
return false | |
} | |
} | |
public var value: A? { | |
guard case .success(let v) = self else { return nil } | |
return v | |
} | |
public var error: Error? { | |
switch self { | |
case .success: | |
return nil | |
case .failure(let error): | |
return error | |
} | |
} | |
} | |
// MARK: - Initialize | |
extension Result { | |
public init(_ value: A?, or error: Error) { | |
if let value = value { | |
self = .success(value) | |
} else { | |
self = .failure(error) | |
} | |
} | |
} | |
// MARK: - CustomStringConvertible | |
extension Result: CustomStringConvertible { | |
public var description: String { | |
switch self { | |
case .success: | |
return "SUCCESS" | |
case .failure: | |
return "FAILURE" | |
} | |
} | |
} | |
// MARK: - CustomDebugStringConvertible | |
extension Result: CustomDebugStringConvertible { | |
public var debugDescription: String { | |
switch self { | |
case .success(let value): | |
return "SUCCESS: \(value)" | |
case .failure(let error): | |
return "FAILURE: \(error)" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment