Result.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // Result.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/22.
  6. //
  7. // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// Represents a result of some operation, whether it is successful or an error happens.
  28. ///
  29. /// - success: The operation is successful and an associated value could be provided.
  30. /// - failure: An error happens during the operation.
  31. public enum Result<Value> {
  32. case success(Value)
  33. case failure(Error)
  34. /// Returns `true` if the result is a success, `false` otherwise.
  35. public var isSuccess: Bool {
  36. if case .success = self {
  37. return true
  38. }
  39. return false
  40. }
  41. /// Returns the associated value if the result is a success, `nil` otherwise.
  42. public var value: Value? {
  43. if case .success(let v) = self {
  44. return v
  45. }
  46. return nil
  47. }
  48. /// Returns `true` if the result is a failure, `false` otherwise.
  49. public var failure: Bool {
  50. return !isSuccess
  51. }
  52. /// Returns the associated error value if the result is a failure, `nil` otherwise.
  53. public var error: Error? {
  54. if case .failure(let e) = self {
  55. return e
  56. }
  57. return nil
  58. }
  59. /// Map over the `Result` value. If it was a `.success`, `transform` closure will be applied to associated value
  60. /// and a new `.success` with transformed value will be returned. If it was a `.failure`, the same error will be
  61. /// returned.
  62. ///
  63. /// - Parameter transform: A closure that takes the success value of the instance.
  64. /// - Returns: A `Result` containing the result of the given closure. If this instance is a failure, returns the
  65. /// same failure.
  66. public func map<T>(_ transform: (Value) -> T) -> Result<T> {
  67. switch self {
  68. case .success(let value): return .success(transform(value))
  69. case .failure(let error): return .failure(error)
  70. }
  71. }
  72. }