Result.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Result.swift
  2. //
  3. // Copyright (c) 2014–2016 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, Error: ErrorType> {
  31. case Success(Value)
  32. case Failure(Error)
  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 error value if the result is a failure, `nil` otherwise.
  56. public var error: Error? {
  57. switch self {
  58. case .Success:
  59. return nil
  60. case .Failure(let error):
  61. return error
  62. }
  63. }
  64. }
  65. // MARK: - CustomStringConvertible
  66. extension Result: CustomStringConvertible {
  67. /// The textual representation used when written to an output stream, which includes whether the result was a
  68. /// success or failure.
  69. public var description: String {
  70. switch self {
  71. case .Success:
  72. return "SUCCESS"
  73. case .Failure:
  74. return "FAILURE"
  75. }
  76. }
  77. }
  78. // MARK: - CustomDebugStringConvertible
  79. extension Result: CustomDebugStringConvertible {
  80. /// The debug textual representation used when written to an output stream, which includes whether the result was a
  81. /// success or failure in addition to the value or error.
  82. public var debugDescription: String {
  83. switch self {
  84. case .Success(let value):
  85. return "SUCCESS: \(value)"
  86. case .Failure(let error):
  87. return "FAILURE: \(error)"
  88. }
  89. }
  90. }