123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import Foundation
- public enum Result<Value> {
- case success(Value)
- case failure(Error)
-
- public var isSuccess: Bool {
- switch self {
- case .success:
- return true
- case .failure:
- return false
- }
- }
-
- public var isFailure: Bool {
- return !isSuccess
- }
-
- public var value: Value? {
- switch self {
- case .success(let value):
- return value
- case .failure:
- return nil
- }
- }
-
- public var error: Error? {
- switch self {
- case .success:
- return nil
- case .failure(let error):
- return error
- }
- }
- }
- extension Result: CustomStringConvertible {
-
-
- public var description: String {
- switch self {
- case .success:
- return "SUCCESS"
- case .failure:
- return "FAILURE"
- }
- }
- }
- extension Result: CustomDebugStringConvertible {
-
-
- public var debugDescription: String {
- switch self {
- case .success(let value):
- return "SUCCESS: \(value)"
- case .failure(let error):
- return "FAILURE: \(error)"
- }
- }
- }
- extension Result {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public init(value: () throws -> Value) {
- do {
- self = try .success(value())
- } catch {
- self = .failure(error)
- }
- }
-
-
-
-
-
-
-
-
-
- public func unwrap() throws -> Value {
- switch self {
- case .success(let value):
- return value
- case .failure(let error):
- throw error
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func map<T>(_ transform: (Value) -> T) -> Result<T> {
- switch self {
- case .success(let value):
- return .success(transform(value))
- case .failure(let error):
- return .failure(error)
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func flatMap<T>(_ transform: (Value) throws -> T) -> Result<T> {
- switch self {
- case .success(let value):
- do {
- return try .success(transform(value))
- } catch {
- return .failure(error)
- }
- case .failure(let error):
- return .failure(error)
- }
- }
-
-
-
-
-
-
-
-
-
-
- public func mapError<T: Error>(_ transform: (Error) -> T) -> Result {
- switch self {
- case .failure(let error):
- return .failure(transform(error))
- case .success:
- return self
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func flatMapError<T: Error>(_ transform: (Error) throws -> T) -> Result {
- switch self {
- case .failure(let error):
- do {
- return try .failure(transform(error))
- } catch {
- return .failure(error)
- }
- case .success:
- return self
- }
- }
-
-
-
-
-
-
- @discardableResult
- public func withValue(_ closure: (Value) throws -> Void) rethrows -> Result {
- if case let .success(value) = self { try closure(value) }
- return self
- }
-
-
-
-
-
-
- @discardableResult
- public func withError(_ closure: (Error) throws -> Void) rethrows -> Result {
- if case let .failure(error) = self { try closure(error) }
- return self
- }
-
-
-
-
-
-
- @discardableResult
- public func ifSuccess(_ closure: () throws -> Void) rethrows -> Result {
- if isSuccess { try closure() }
- return self
- }
-
-
-
-
-
-
- @discardableResult
- public func ifFailure(_ closure: () throws -> Void) rethrows -> Result {
- if isFailure { try closure() }
- return self
- }
- }
|