|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | + |
| 3 | +namespace SharpCoreDB.Functional; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Represents the result of an operation — either a success value or an <see cref="Error"/>. |
| 7 | +/// </summary> |
| 8 | +/// <typeparam name="T">The type of the success value.</typeparam> |
| 9 | +public readonly struct Fin<T> : IEquatable<Fin<T>> |
| 10 | +{ |
| 11 | + private readonly T? _value; |
| 12 | + private readonly Error? _error; |
| 13 | + private readonly bool _isSucc; |
| 14 | + |
| 15 | + private Fin(T value) |
| 16 | + { |
| 17 | + _value = value; |
| 18 | + _error = null; |
| 19 | + _isSucc = true; |
| 20 | + } |
| 21 | + |
| 22 | + private Fin(Error error) |
| 23 | + { |
| 24 | + _value = default; |
| 25 | + _error = error; |
| 26 | + _isSucc = false; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Creates a success result wrapping <paramref name="value"/>. |
| 31 | + /// </summary> |
| 32 | + /// <param name="value">The success value.</param> |
| 33 | + /// <returns>A successful <see cref="Fin{T}"/>.</returns> |
| 34 | + public static Fin<T> Succ(T value) => new(value); |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates a failure result wrapping <paramref name="error"/>. |
| 38 | + /// </summary> |
| 39 | + /// <param name="error">The error.</param> |
| 40 | + /// <returns>A failed <see cref="Fin{T}"/>.</returns> |
| 41 | + public static Fin<T> Fail(Error error) |
| 42 | + { |
| 43 | + ArgumentNullException.ThrowIfNull(error); |
| 44 | + return new(error); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets whether this result is a success. |
| 49 | + /// </summary> |
| 50 | + [MemberNotNullWhen(true, nameof(_value))] |
| 51 | + public bool IsSucc => _isSucc; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets whether this result is a failure. |
| 55 | + /// </summary> |
| 56 | + [MemberNotNullWhen(true, nameof(_error))] |
| 57 | + public bool IsFail => !_isSucc; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Pattern-matches on this result, returning a value from the appropriate branch. |
| 61 | + /// </summary> |
| 62 | + /// <typeparam name="TResult">The return type.</typeparam> |
| 63 | + /// <param name="Succ">Function invoked on success.</param> |
| 64 | + /// <param name="Fail">Function invoked on failure.</param> |
| 65 | + /// <returns>The result of the matched branch.</returns> |
| 66 | + public TResult Match<TResult>(Func<T, TResult> Succ, Func<Error, TResult> Fail) => |
| 67 | + _isSucc ? Succ(_value!) : Fail(_error!); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Async pattern-match on this result. |
| 71 | + /// </summary> |
| 72 | + /// <typeparam name="TResult">The return type.</typeparam> |
| 73 | + /// <param name="Succ">Async function invoked on success.</param> |
| 74 | + /// <param name="Fail">Async function invoked on failure.</param> |
| 75 | + /// <returns>The result of the matched branch.</returns> |
| 76 | + public Task<TResult> Match<TResult>(Func<T, Task<TResult>> Succ, Func<Error, Task<TResult>> Fail) => |
| 77 | + _isSucc ? Succ(_value!) : Fail(_error!); |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Pattern-matches on this result, executing the appropriate action. |
| 81 | + /// </summary> |
| 82 | + /// <param name="Succ">Action invoked on success.</param> |
| 83 | + /// <param name="Fail">Action invoked on failure.</param> |
| 84 | + public void Match(Action<T> Succ, Action<Error> Fail) |
| 85 | + { |
| 86 | + if (_isSucc) |
| 87 | + Succ(_value!); |
| 88 | + else |
| 89 | + Fail(_error!); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Transforms the success value using <paramref name="map"/>. |
| 94 | + /// Failures pass through unchanged. |
| 95 | + /// </summary> |
| 96 | + /// <typeparam name="TResult">The mapped type.</typeparam> |
| 97 | + /// <param name="map">The mapping function.</param> |
| 98 | + /// <returns>A new result with the mapped success value, or the original failure.</returns> |
| 99 | + public Fin<TResult> Map<TResult>(Func<T, TResult> map) => |
| 100 | + _isSucc ? Fin<TResult>.Succ(map(_value!)) : Fin<TResult>.Fail(_error!); |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Executes <paramref name="action"/> if this result is a success. |
| 104 | + /// </summary> |
| 105 | + /// <param name="action">The action to execute on the success value.</param> |
| 106 | + public void IfSucc(Action<T> action) |
| 107 | + { |
| 108 | + if (_isSucc) |
| 109 | + action(_value!); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Executes <paramref name="action"/> if this result is a failure. |
| 114 | + /// </summary> |
| 115 | + /// <param name="action">The action to execute on the error.</param> |
| 116 | + public void IfFail(Action<Error> action) |
| 117 | + { |
| 118 | + if (!_isSucc) |
| 119 | + action(_error!); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Flat-maps the success value using <paramref name="bind"/>. |
| 124 | + /// Failures pass through unchanged. |
| 125 | + /// </summary> |
| 126 | + /// <typeparam name="TResult">The bound type.</typeparam> |
| 127 | + /// <param name="bind">The binding function.</param> |
| 128 | + /// <returns>The result of the bind, or the original failure.</returns> |
| 129 | + public Fin<TResult> Bind<TResult>(Func<T, Fin<TResult>> bind) => |
| 130 | + _isSucc ? bind(_value!) : Fin<TResult>.Fail(_error!); |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Returns the success value or invokes <paramref name="handler"/> on the error. |
| 134 | + /// Prefer <see cref="Match{TResult}(Func{T, TResult}, Func{Error, TResult})"/> over this method. |
| 135 | + /// </summary> |
| 136 | + /// <param name="handler">Handler invoked when this result is a failure.</param> |
| 137 | + /// <returns>The success value or the handler result.</returns> |
| 138 | + public T IfFail(Func<Error, T> handler) => |
| 139 | + _isSucc ? _value! : handler(_error!); |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Implicit conversion from a value to a success result. |
| 143 | + /// </summary> |
| 144 | + public static implicit operator Fin<T>(T value) => Succ(value); |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Implicit conversion from an <see cref="Error"/> to a failure result. |
| 148 | + /// </summary> |
| 149 | + public static implicit operator Fin<T>(Error error) => Fail(error); |
| 150 | + |
| 151 | + /// <inheritdoc /> |
| 152 | + public bool Equals(Fin<T> other) => |
| 153 | + _isSucc == other._isSucc && |
| 154 | + (_isSucc |
| 155 | + ? EqualityComparer<T>.Default.Equals(_value, other._value) |
| 156 | + : EqualityComparer<Error>.Default.Equals(_error, other._error)); |
| 157 | + |
| 158 | + /// <inheritdoc /> |
| 159 | + public override bool Equals(object? obj) => obj is Fin<T> other && Equals(other); |
| 160 | + |
| 161 | + /// <inheritdoc /> |
| 162 | + public override int GetHashCode() => |
| 163 | + _isSucc |
| 164 | + ? HashCode.Combine(true, _value) |
| 165 | + : HashCode.Combine(false, _error); |
| 166 | + |
| 167 | + /// <summary>Equality operator.</summary> |
| 168 | + public static bool operator ==(Fin<T> left, Fin<T> right) => left.Equals(right); |
| 169 | + |
| 170 | + /// <summary>Inequality operator.</summary> |
| 171 | + public static bool operator !=(Fin<T> left, Fin<T> right) => !left.Equals(right); |
| 172 | + |
| 173 | + /// <inheritdoc /> |
| 174 | + public override string ToString() => |
| 175 | + _isSucc ? $"Succ({_value})" : $"Fail({_error})"; |
| 176 | +} |
0 commit comments