Result.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Result.swift
  3. //
  4. // Copyright (c) 2014-2016 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. /// Used to represent whether a request was successful or encountered an error.
  26. ///
  27. /// - success: The request and all post processing operations were successful resulting in the serialization of the
  28. /// provided associated value.
  29. ///
  30. /// - failure: The request encountered an error resulting in a failure. The associated values are the original data
  31. /// provided by the server as well as the error that caused the failure.
  32. public enum Result<Value> {
  33. case success(Value)
  34. case failure(Error)
  35. /// Returns `true` if the result is a success, `false` otherwise.
  36. public var isSuccess: Bool {
  37. switch self {
  38. case .success:
  39. return true
  40. case .failure:
  41. return false
  42. }
  43. }
  44. /// Returns `true` if the result is a failure, `false` otherwise.
  45. public var isFailure: Bool {
  46. return !isSuccess
  47. }
  48. /// Returns the associated value if the result is a success, `nil` otherwise.
  49. public var value: Value? {
  50. switch self {
  51. case .success(let value):
  52. return value
  53. case .failure:
  54. return nil
  55. }
  56. }
  57. /// Returns the associated error value if the result is a failure, `nil` otherwise.
  58. public var error: Error? {
  59. switch self {
  60. case .success:
  61. return nil
  62. case .failure(let error):
  63. return error
  64. }
  65. }
  66. }
  67. // MARK: - CustomStringConvertible
  68. extension Result: CustomStringConvertible {
  69. /// The textual representation used when written to an output stream, which includes whether the result was a
  70. /// success or failure.
  71. public var description: String {
  72. switch self {
  73. case .success:
  74. return "SUCCESS"
  75. case .failure:
  76. return "FAILURE"
  77. }
  78. }
  79. }
  80. // MARK: - CustomDebugStringConvertible
  81. extension Result: CustomDebugStringConvertible {
  82. /// The debug textual representation used when written to an output stream, which includes whether the result was a
  83. /// success or failure in addition to the value or error.
  84. public var debugDescription: String {
  85. switch self {
  86. case .success(let value):
  87. return "SUCCESS: \(value)"
  88. case .failure(let error):
  89. return "FAILURE: \(error)"
  90. }
  91. }
  92. }