mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
50 lines
1.5 KiB
Swift
50 lines
1.5 KiB
Swift
enum Result<Value> {
|
|
case success(Value)
|
|
case failure(Error)
|
|
|
|
init(value: () throws -> Value) {
|
|
do {
|
|
self = try .success(value())
|
|
} catch {
|
|
self = .failure(error)
|
|
}
|
|
}
|
|
|
|
/// Evaluates the given closure when this `Result` is a success, passing the
|
|
/// unwrapped value as a parameter.
|
|
///
|
|
/// Use the `map` method with a closure that does not throw. For example:
|
|
///
|
|
/// let possibleData: Result<Data> = .success(Data())
|
|
/// let possibleInt = possibleData.map { $0.count }
|
|
/// try print(possibleInt.unwrap())
|
|
/// // Prints "0"
|
|
///
|
|
/// let noData: Result<Data> = .failure(error)
|
|
/// let noInt = noData.map { $0.count }
|
|
/// try print(noInt.unwrap())
|
|
/// // Throws error
|
|
///
|
|
/// - parameter transform: A closure that takes the success value of
|
|
/// the instance.
|
|
/// - returns: A `Result` containing the result of the given closure. If
|
|
/// this instance is a failure, returns the same failure.
|
|
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)
|
|
}
|
|
}
|
|
|
|
func unwrap() throws -> Value {
|
|
switch self {
|
|
case .success(let value):
|
|
return value
|
|
case .failure(let error):
|
|
throw error
|
|
}
|
|
}
|
|
}
|