1 |
efrain |
1 |
//
|
|
|
2 |
// Result+Alamofire.swift
|
|
|
3 |
//
|
|
|
4 |
// Copyright (c) 2019 Alamofire Software Foundation (http://alamofire.org/)
|
|
|
5 |
//
|
|
|
6 |
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
7 |
// of this software and associated documentation files (the "Software"), to deal
|
|
|
8 |
// in the Software without restriction, including without limitation the rights
|
|
|
9 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
10 |
// copies of the Software, and to permit persons to whom the Software is
|
|
|
11 |
// furnished to do so, subject to the following conditions:
|
|
|
12 |
//
|
|
|
13 |
// The above copyright notice and this permission notice shall be included in
|
|
|
14 |
// all copies or substantial portions of the Software.
|
|
|
15 |
//
|
|
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
17 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
18 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
19 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
20 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
21 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
22 |
// THE SOFTWARE.
|
|
|
23 |
//
|
|
|
24 |
|
|
|
25 |
import Foundation
|
|
|
26 |
|
|
|
27 |
/// Default type of `Result` returned by Alamofire, with an `AFError` `Failure` type.
|
|
|
28 |
public typealias AFResult<Success> = Result<Success, AFError>
|
|
|
29 |
|
|
|
30 |
// MARK: - Internal APIs
|
|
|
31 |
|
|
|
32 |
extension Result {
|
|
|
33 |
/// Returns whether the instance is `.success`.
|
|
|
34 |
var isSuccess: Bool {
|
|
|
35 |
guard case .success = self else { return false }
|
|
|
36 |
return true
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/// Returns whether the instance is `.failure`.
|
|
|
40 |
var isFailure: Bool {
|
|
|
41 |
!isSuccess
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/// Returns the associated value if the result is a success, `nil` otherwise.
|
|
|
45 |
var success: Success? {
|
|
|
46 |
guard case let .success(value) = self else { return nil }
|
|
|
47 |
return value
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/// Returns the associated error value if the result is a failure, `nil` otherwise.
|
|
|
51 |
var failure: Failure? {
|
|
|
52 |
guard case let .failure(error) = self else { return nil }
|
|
|
53 |
return error
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/// Initializes a `Result` from value or error. Returns `.failure` if the error is non-nil, `.success` otherwise.
|
|
|
57 |
///
|
|
|
58 |
/// - Parameters:
|
|
|
59 |
/// - value: A value.
|
|
|
60 |
/// - error: An `Error`.
|
|
|
61 |
init(value: Success, error: Failure?) {
|
|
|
62 |
if let error = error {
|
|
|
63 |
self = .failure(error)
|
|
|
64 |
} else {
|
|
|
65 |
self = .success(value)
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/// Evaluates the specified closure when the `Result` is a success, passing the unwrapped value as a parameter.
|
|
|
70 |
///
|
|
|
71 |
/// Use the `tryMap` method with a closure that may throw an error. For example:
|
|
|
72 |
///
|
|
|
73 |
/// let possibleData: Result<Data, Error> = .success(Data(...))
|
|
|
74 |
/// let possibleObject = possibleData.tryMap {
|
|
|
75 |
/// try JSONSerialization.jsonObject(with: $0)
|
|
|
76 |
/// }
|
|
|
77 |
///
|
|
|
78 |
/// - parameter transform: A closure that takes the success value of the instance.
|
|
|
79 |
///
|
|
|
80 |
/// - returns: A `Result` containing the result of the given closure. If this instance is a failure, returns the
|
|
|
81 |
/// same failure.
|
|
|
82 |
func tryMap<NewSuccess>(_ transform: (Success) throws -> NewSuccess) -> Result<NewSuccess, Error> {
|
|
|
83 |
switch self {
|
|
|
84 |
case let .success(value):
|
|
|
85 |
do {
|
|
|
86 |
return try .success(transform(value))
|
|
|
87 |
} catch {
|
|
|
88 |
return .failure(error)
|
|
|
89 |
}
|
|
|
90 |
case let .failure(error):
|
|
|
91 |
return .failure(error)
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/// Evaluates the specified closure when the `Result` is a failure, passing the unwrapped error as a parameter.
|
|
|
96 |
///
|
|
|
97 |
/// Use the `tryMapError` function with a closure that may throw an error. For example:
|
|
|
98 |
///
|
|
|
99 |
/// let possibleData: Result<Data, Error> = .success(Data(...))
|
|
|
100 |
/// let possibleObject = possibleData.tryMapError {
|
|
|
101 |
/// try someFailableFunction(taking: $0)
|
|
|
102 |
/// }
|
|
|
103 |
///
|
|
|
104 |
/// - Parameter transform: A throwing closure that takes the error of the instance.
|
|
|
105 |
///
|
|
|
106 |
/// - Returns: A `Result` instance containing the result of the transform. If this instance is a success, returns
|
|
|
107 |
/// the same success.
|
|
|
108 |
func tryMapError<NewFailure: Error>(_ transform: (Failure) throws -> NewFailure) -> Result<Success, Error> {
|
|
|
109 |
switch self {
|
|
|
110 |
case let .failure(error):
|
|
|
111 |
do {
|
|
|
112 |
return try .failure(transform(error))
|
|
|
113 |
} catch {
|
|
|
114 |
return .failure(error)
|
|
|
115 |
}
|
|
|
116 |
case let .success(value):
|
|
|
117 |
return .success(value)
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|