Result.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Result.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. /**
  24. Used to represent whether a request was successful or encountered an error.
  25. - Success: The request and all post processing operations were successful resulting in the serialization of the
  26. provided associated value.
  27. - Failure: The request encountered an error resulting in a failure. The associated values are the original data
  28. provided by the server as well as the error that caused the failure.
  29. */
  30. public enum Result<Value> {
  31. case Success(Value)
  32. case Failure(NSData?, NSError)
  33. /// Returns `true` if the result is a success, `false` otherwise.
  34. public var isSuccess: Bool {
  35. switch self {
  36. case .Success:
  37. return true
  38. case .Failure:
  39. return false
  40. }
  41. }
  42. /// Returns `true` if the result is a failure, `false` otherwise.
  43. public var isFailure: Bool {
  44. return !isSuccess
  45. }
  46. /// Returns the associated value if the result is a success, `nil` otherwise.
  47. public var value: Value? {
  48. switch self {
  49. case .Success(let value):
  50. return value
  51. case .Failure:
  52. return nil
  53. }
  54. }
  55. /// Returns the associated data value if the result is a failure, `nil` otherwise.
  56. public var data: NSData? {
  57. switch self {
  58. case .Success:
  59. return nil
  60. case .Failure(let data, _):
  61. return data
  62. }
  63. }
  64. /// Returns the associated error value if the result is a failure, `nil` otherwise.
  65. public var error: NSError? {
  66. switch self {
  67. case .Success:
  68. return nil
  69. case .Failure(_, let error):
  70. return error
  71. }
  72. }
  73. }
  74. // MARK: - CustomStringConvertible
  75. extension Result: CustomStringConvertible {
  76. public var description: String {
  77. switch self {
  78. case .Success:
  79. return "SUCCESS"
  80. case .Failure:
  81. return "FAILURE"
  82. }
  83. }
  84. }
  85. // MARK: - CustomDebugStringConvertible
  86. extension Result: CustomDebugStringConvertible {
  87. public var debugDescription: String {
  88. switch self {
  89. case .Success(let value):
  90. return "SUCCESS: \(value)"
  91. case .Failure(let data, let error):
  92. if let
  93. data = data,
  94. utf8Data = NSString(data: data, encoding: NSUTF8StringEncoding)
  95. {
  96. return "FAILURE: \(error) \(utf8Data)"
  97. } else {
  98. return "FAILURE with Error: \(error)"
  99. }
  100. }
  101. }
  102. }