| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace Itinero; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Represents a result of some calculation and associated status information. |
| | 7 | | /// </summary> |
| | 8 | | /// <typeparam name="T"></typeparam> |
| | 9 | | public class Result<T> |
| | 10 | | { |
| | 11 | | private readonly T _value; |
| | 12 | | private readonly Func<string, Exception>? _createException; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Creates a new result. |
| | 16 | | /// </summary> |
| 117 | 17 | | public Result(T result) |
| 117 | 18 | | { |
| 117 | 19 | | _value = result; |
| 117 | 20 | | this.ErrorMessage = string.Empty; |
| 117 | 21 | | this.IsError = false; |
| 117 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Creates a new result. |
| | 26 | | /// </summary> |
| | 27 | | public Result(string errorMessage) |
| 0 | 28 | | : this(errorMessage, (m) => new Exception(m)) { } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Creates a new result. |
| | 32 | | /// </summary> |
| 0 | 33 | | public Result(string errorMessage, Func<string, Exception>? createException) |
| 0 | 34 | | { |
| 0 | 35 | | _value = default!; |
| 0 | 36 | | _createException = createException; |
| 0 | 37 | | this.ErrorMessage = errorMessage; |
| 0 | 38 | | this.IsError = true; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets the result. |
| | 43 | | /// </summary> |
| | 44 | | public T Value |
| | 45 | | { |
| | 46 | | get |
| 131 | 47 | | { |
| 131 | 48 | | if (this.IsError && |
| 131 | 49 | | _createException != null) |
| 0 | 50 | | { |
| 0 | 51 | | throw _createException(this.ErrorMessage); |
| | 52 | | } |
| | 53 | |
|
| 131 | 54 | | return _value; |
| 131 | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Implicit conversion to a boolean indication success or fail. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="result">The result object.</param> |
| | 62 | | /// <returns>The success or fail boolean.</returns> |
| | 63 | | public static implicit operator bool(Result<T> result) |
| 0 | 64 | | { |
| 0 | 65 | | return !result.IsError; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Implicit conversion to the result object type. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="result">The result object.</param> |
| | 72 | | /// <returns>The result object type.</returns> |
| | 73 | | public static implicit operator T(Result<T> result) |
| 55 | 74 | | { |
| 55 | 75 | | return result.Value; |
| 55 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Implicit conversion from the result object type. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="result">The result.</param> |
| | 82 | | /// <returns>The result object.</returns> |
| | 83 | | public static implicit operator Result<T>(T result) |
| 69 | 84 | | { |
| 69 | 85 | | return new(result); |
| 69 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets the status. |
| | 90 | | /// </summary> |
| 303 | 91 | | public bool IsError { get; private set; } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Gets the error message. |
| | 95 | | /// </summary> |
| 117 | 96 | | public string ErrorMessage { get; private set; } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Converts this result, when an error to an result of another type. |
| | 100 | | /// </summary> |
| | 101 | | /// <returns></returns> |
| | 102 | | public Result<TNew> ConvertError<TNew>() |
| 0 | 103 | | { |
| 0 | 104 | | if (!this.IsError) |
| 0 | 105 | | { |
| 0 | 106 | | throw new Exception("Cannot convert a result that represents more than an error."); |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | return new Result<TNew>(this.ErrorMessage, _createException); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Converts this result. |
| | 114 | | /// </summary> |
| | 115 | | /// <typeparam name="TOther">A derived type.</typeparam> |
| | 116 | | /// <returns></returns> |
| | 117 | | public static Result<T> Create<TOther>(Result<TOther> result) where TOther : T |
| 0 | 118 | | { |
| 0 | 119 | | return new Result<T>(result); |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Returns a description. |
| | 124 | | /// </summary> |
| | 125 | | /// <returns></returns> |
| | 126 | | public override string ToString() |
| 0 | 127 | | { |
| 0 | 128 | | if (this.IsError) |
| 0 | 129 | | { |
| 0 | 130 | | return $"Result<{nameof(T)}>: {this.ErrorMessage}"; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | if (this.Value == null) |
| 0 | 134 | | { |
| 0 | 135 | | return $"Result<{nameof(T)}>: null"; |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | return $"Result<{nameof(T)}>: {this.Value.ToString()}"; |
| 0 | 139 | | } |
| | 140 | | } |