2
0

AFResult.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // AFResult.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. import Foundation
  25. public typealias AFResult<T> = Result<T, Error>
  26. // MARK: - Internal APIs
  27. extension AFResult {
  28. /// Returns the associated value if the result is a success, `nil` otherwise.
  29. var value: Success? {
  30. guard case .success(let value) = self else { return nil }
  31. return value
  32. }
  33. /// Returns the associated error value if the result is a failure, `nil` otherwise.
  34. var error: Failure? {
  35. guard case .failure(let error) = self else { return nil }
  36. return error
  37. }
  38. /// Initializes an `AFResult` from value or error. Returns `.failure` if the error is non-nil, `.success` otherwise.
  39. ///
  40. /// - Parameters:
  41. /// - value: A value.
  42. /// - error: An `Error`.
  43. init(value: Success, error: Failure?) {
  44. if let error = error {
  45. self = .failure(error)
  46. } else {
  47. self = .success(value)
  48. }
  49. }
  50. /// Evaluates the specified closure when the `AFResult` is a success, passing the unwrapped value as a parameter.
  51. ///
  52. /// Use the `flatMap` method with a closure that may throw an error. For example:
  53. ///
  54. /// let possibleData: AFResult<Data> = .success(Data(...))
  55. /// let possibleObject = possibleData.flatMap {
  56. /// try JSONSerialization.jsonObject(with: $0)
  57. /// }
  58. ///
  59. /// - parameter transform: A closure that takes the success value of the instance.
  60. ///
  61. /// - returns: An `AFResult` containing the result of the given closure. If this instance is a failure, returns the
  62. /// same failure.
  63. func flatMap<T>(_ transform: (Success) throws -> T) -> AFResult<T> {
  64. switch self {
  65. case .success(let value):
  66. do {
  67. return try .success(transform(value))
  68. } catch {
  69. return .failure(error)
  70. }
  71. case .failure(let error):
  72. return .failure(error)
  73. }
  74. }
  75. /// Evaluates the specified closure when the `AFResult` is a failure, passing the unwrapped error as a parameter.
  76. ///
  77. /// Use the `flatMapError` function with a closure that may throw an error. For example:
  78. ///
  79. /// let possibleData: AFResult<Data> = .success(Data(...))
  80. /// let possibleObject = possibleData.flatMapError {
  81. /// try someFailableFunction(taking: $0)
  82. /// }
  83. ///
  84. /// - Parameter transform: A throwing closure that takes the error of the instance.
  85. ///
  86. /// - Returns: An `AFResult` instance containing the result of the transform. If this instance is a success, returns
  87. /// the same success.
  88. func flatMapError<T: Error>(_ transform: (Failure) throws -> T) -> AFResult<Success> {
  89. switch self {
  90. case .failure(let error):
  91. do {
  92. return try .failure(transform(error))
  93. } catch {
  94. return .failure(error)
  95. }
  96. case .success(let value):
  97. return .success(value)
  98. }
  99. }
  100. }