Result+Alamofire.swift 4.5 KB

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