|
16 | 16 |
|
17 | 17 | namespace OnixLabs.Core;
|
18 | 18 |
|
| 19 | +/// <summary> |
| 20 | +/// Represents a failed result. |
| 21 | +/// </summary> |
| 22 | +/// <typeparam name="T">The type of the underlying result value.</typeparam> |
19 | 23 | public sealed class Failure<T> : Result<T>, IValueEquatable<Failure<T>>
|
20 | 24 | {
|
21 |
| - public Failure(Exception exception) => Exception = exception; |
22 |
| - |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the <see cref="Failure{T}"/> class. |
| 27 | + /// </summary> |
| 28 | + /// <param name="exception">The underlying exception representing the failed result.</param> |
| 29 | + internal Failure(Exception exception) => Exception = exception; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets the underlying result exception. |
| 33 | + /// </summary> |
23 | 34 | public Exception Exception { get; }
|
24 | 35 |
|
| 36 | + /// <summary> |
| 37 | + /// Performs an equality comparison between two object instances. |
| 38 | + /// </summary> |
| 39 | + /// <param name="left">The left-hand instance to compare.</param> |
| 40 | + /// <param name="right">The right-hand instance to compare.</param> |
| 41 | + /// <returns>Returns <see langword="true"/> if the left-hand instance is equal to the right-hand instance; otherwise, <see langword="false"/>.</returns> |
25 | 42 | public static bool operator ==(Failure<T> left, Failure<T> right) => Equals(left, right);
|
26 | 43 |
|
| 44 | + /// <summary> |
| 45 | + /// Performs an inequality comparison between two object instances. |
| 46 | + /// </summary> |
| 47 | + /// <param name="left">The left-hand instance to compare.</param> |
| 48 | + /// <param name="right">The right-hand instance to compare.</param> |
| 49 | + /// <returns>Returns <see langword="true"/> if the left-hand instance is not equal to the right-hand instance; otherwise, <see langword="false"/>.</returns> |
27 | 50 | public static bool operator !=(Failure<T> left, Failure<T> right) => !Equals(left, right);
|
28 | 51 |
|
| 52 | + /// <summary> |
| 53 | + /// Checks whether the current object is equal to another object of the same type. |
| 54 | + /// </summary> |
| 55 | + /// <param name="other">An object to compare with the current object.</param> |
| 56 | + /// <returns>Returns <see langword="true"/> if the current object is equal to the other parameter; otherwise, <see langword="false"/>.</returns> |
29 | 57 | public bool Equals(Failure<T>? other) => ReferenceEquals(this, other) || other is not null && Equals(other.Exception, Exception);
|
30 | 58 |
|
| 59 | + /// <summary> |
| 60 | + /// Checks for equality between the current instance and another object. |
| 61 | + /// </summary> |
| 62 | + /// <param name="obj">The object to check for equality.</param> |
| 63 | + /// <returns>Returns <see langword="true"/> if the object is equal to the current instance; otherwise, <see langword="false"/>.</returns> |
31 | 64 | public override bool Equals(object? obj) => Equals(obj as Failure<T>);
|
32 | 65 |
|
| 66 | + /// <summary> |
| 67 | + /// Serves as a hash code function for the current instance. |
| 68 | + /// </summary> |
| 69 | + /// <returns>Returns a hash code for the current instance.</returns> |
33 | 70 | public override int GetHashCode() => Exception.GetHashCode();
|
34 | 71 |
|
| 72 | + /// <summary> |
| 73 | + /// Gets the underlying value of the current <see cref="Result{T}"/> instance, if the underlying value is present; |
| 74 | + /// otherwise returns the default <typeparam name="T"/> value. |
| 75 | + /// </summary> |
| 76 | + /// <returns> |
| 77 | + /// Returns the underlying value of the current <see cref="Result{T}"/> instance, if the underlying value is present; |
| 78 | + /// otherwise returns the default <typeparam name="T"/> value. |
| 79 | + /// </returns> |
35 | 80 | public override T? GetValueOrDefault() => default;
|
36 | 81 |
|
| 82 | + /// <summary> |
| 83 | + /// Gets the underlying value of the current <see cref="Result{T}"/> instance, if the underlying value is present; |
| 84 | + /// otherwise returns the specified default value. |
| 85 | + /// </summary> |
| 86 | + /// <param name="defaultValue">The default value to return in the event that the underlying value is absent.</param> |
| 87 | + /// <returns> |
| 88 | + /// Returns the underlying value of the current <see cref="Result{T}"/> instance, if the underlying value is present; |
| 89 | + /// otherwise returns the specified default value. |
| 90 | + /// </returns> |
37 | 91 | public override T GetValueOrDefault(T defaultValue) => defaultValue;
|
38 | 92 |
|
| 93 | + /// <summary> |
| 94 | + /// Gets the underlying value of the current <see cref="Result{T}"/> instance; |
| 95 | + /// otherwise throws the underlying exception if the current <see cref="Result{T}"/> is in a failed stated. |
| 96 | + /// </summary> |
| 97 | + /// <returns> |
| 98 | + /// Returns the underlying value of the current <see cref="Result{T}"/> instance; |
| 99 | + /// otherwise throws the underlying exception if the current <see cref="Result{T}"/> is in a failed stated. |
| 100 | + /// </returns> |
39 | 101 | public override T GetValueOrThrow() => throw Exception;
|
40 | 102 |
|
| 103 | + /// <summary> |
| 104 | + /// Executes the action that matches the value of the current <see cref="Result{T}"/> instance. |
| 105 | + /// </summary> |
| 106 | + /// <param name="success">The action to execute when the current <see cref="Result{T}"/> instance is in a successful state.</param> |
| 107 | + /// <param name="failure">The action to execute when the current <see cref="Result{T}"/> instance is in a failed state.</param> |
41 | 108 | public override void Match(Action<T> success, Action<Exception> failure) => failure(Exception);
|
42 | 109 |
|
| 110 | + /// <summary> |
| 111 | + /// Executes the function that matches the value of the current <see cref="Result{T}"/> instance and returns its result. |
| 112 | + /// </summary> |
| 113 | + /// <param name="success">The action to execute when the current <see cref="Result{T}"/> instance is in a successful state.</param> |
| 114 | + /// <param name="failure">The action to execute when the current <see cref="Result{T}"/> instance is in a failed state.</param> |
| 115 | + /// <typeparam name="TResult">The underlying type of the result produced by the matching function.</typeparam> |
| 116 | + /// <returns> |
| 117 | + /// Returns the result of the <paramref name="success"/> function if the current <see cref="Result{T}"/> instance is in a successful state; |
| 118 | + /// otherwise, returns the result of the <paramref name="failure"/> function if the current <see cref="Result{T}"/> instance is in a failed state. |
| 119 | + /// </returns> |
43 | 120 | public override TResult Match<TResult>(Func<T, TResult> success, Func<Exception, TResult> failure) => failure(Exception);
|
44 | 121 |
|
| 122 | + /// <summary> |
| 123 | + /// Applies the provided selector function to the value of the current <see cref="Result{T}"/> instance. |
| 124 | + /// </summary> |
| 125 | + /// <param name="selector">The function to apply to the value of the current <see cref="Result{T}"/> instance.</param> |
| 126 | + /// <typeparam name="TResult">The underlying type of the result produced by the selector function.</typeparam> |
| 127 | + /// <returns> |
| 128 | + /// Returns a new <see cref="Result{TResult}"/> instance containing the result of the function if the current |
| 129 | + /// <see cref="Result{T}"/> instance is in a successful state; otherwise, returns the current failed <see cref="Result{T}"/> instance. |
| 130 | + /// </returns> |
45 | 131 | public override Result<TResult> Select<TResult>(Func<T, TResult> selector) => Result<TResult>.Failure(Exception);
|
46 | 132 |
|
| 133 | + /// <summary> |
| 134 | + /// Applies the provided selector function to the value of the current <see cref="Result{T}"/> instance. |
| 135 | + /// </summary> |
| 136 | + /// <param name="selector">The function to apply to the value of the current <see cref="Result{T}"/> instance.</param> |
| 137 | + /// <typeparam name="TResult">The underlying type of the result produced by the selector function.</typeparam> |
| 138 | + /// <returns> |
| 139 | + /// Returns a new <see cref="Result{TResult}"/> instance containing the result of the function if the current |
| 140 | + /// <see cref="Result{T}"/> instance is in a successful state; otherwise, returns the current failed <see cref="Result{T}"/> instance. |
| 141 | + /// </returns> |
47 | 142 | public override Result<TResult> SelectMany<TResult>(Func<T, Result<TResult>> selector) => Result<TResult>.Failure(Exception);
|
48 | 143 |
|
| 144 | + /// <summary> |
| 145 | + /// Returns a <see cref="String"/> that represents the current object. |
| 146 | + /// </summary> |
| 147 | + /// <returns>Returns a <see cref="String"/> that represents the current object.</returns> |
49 | 148 | public override string ToString() => Exception.ToString();
|
50 | 149 | }
|
0 commit comments