| | | 1 | | #region Usings |
| | | 2 | | using System.Collections; |
| | | 3 | | #endregion |
| | | 4 | | |
| | | 5 | | namespace ZhEaIsNsAaBn.Utilities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// The result of a fallible operation that returns a value: success with <see cref="Result"/>, or failure with |
| | | 9 | | /// <see cref="LayerResponse.Errors"/>. <c>return value;</c>, <c>return error;</c> and <c>return ex;</c> all convert imp |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="TResult">The value type.</typeparam> |
| | | 12 | | public class LayerResponse< TResult > : LayerResponse |
| | | 13 | | { |
| | | 14 | | /// <summary>Creates a response from one error.</summary> |
| | | 15 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 16 | | /// <param name="error"><see cref="Error.None"/> on success; a real error on failure.</param> |
| | | 17 | | /// <param name="result">The value, if any.</param> |
| | | 18 | | /// <exception cref="InvalidOperationException">Success with a real error, or failure with <see cref="Error.None"/>. |
| | 70576 | 19 | | protected LayerResponse( bool isSuccess, Error error, TResult? result = default ) : base( isSuccess, error ) => Resu |
| | | 20 | | |
| | | 21 | | /// <summary>Creates a response from a list of errors.</summary> |
| | | 22 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 23 | | /// <param name="errors">Empty or <see langword="null"/> on success; at least one real error on failure.</param> |
| | | 24 | | /// <param name="result">The value, if any.</param> |
| | | 25 | | /// <exception cref="InvalidOperationException">Success with errors, or failure with no real error.</exception> |
| | 396 | 26 | | protected LayerResponse( bool isSuccess, List<Error>? errors, TResult? result = default ) : base( isSuccess, errors |
| | | 27 | | |
| | | 28 | | |
| | | 29 | | /// <summary>The value. <see langword="default"/> on failure unless one was supplied.</summary> |
| | | 30 | | public TResult? Result { get; } |
| | | 31 | | |
| | | 32 | | /// <summary>Whether the operation failed, or succeeded with a <see langword="default"/> value or an empty collectio |
| | 12 | 33 | | public bool IsFailureOrEmpty => IsFailure || Equals( Result, default( TResult ) ) || Result is ICollection { Count: |
| | | 34 | | |
| | | 35 | | |
| | | 36 | | /// <summary>Creates a failed response.</summary> |
| | | 37 | | /// <param name="error">The error.</param> |
| | | 38 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 39 | | /// <returns>A failed response.</returns> |
| | 327 | 40 | | public static LayerResponse<TResult> Failure( Error error, TResult? result = default ) => new( false, error, result |
| | | 41 | | |
| | | 42 | | /// <summary>Creates a failed response.</summary> |
| | | 43 | | /// <param name="errors">The errors; at least one.</param> |
| | | 44 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 45 | | /// <returns>A failed response.</returns> |
| | 27 | 46 | | public static LayerResponse<TResult> Failure( List<Error> errors, TResult? result = default ) => new( false, errors, |
| | | 47 | | |
| | | 48 | | /// <summary>Creates a failed response with the errors of another response.</summary> |
| | | 49 | | /// <param name="layerResponse">The failed response to copy errors from.</param> |
| | | 50 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 51 | | /// <returns>A failed response.</returns> |
| | | 52 | | public static LayerResponse<TResult> Failure( LayerResponse layerResponse, TResult? result = default ) |
| | 20 | 53 | | => new( false, layerResponse.Errors, result ); |
| | | 54 | | |
| | | 55 | | /// <summary>Creates a failed response with the errors of another typed response.</summary> |
| | | 56 | | /// <typeparam name="TOtherResult">The value type of the source response.</typeparam> |
| | | 57 | | /// <param name="layerResponse">The failed response to copy errors from.</param> |
| | | 58 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 59 | | /// <returns>A failed response.</returns> |
| | | 60 | | public static LayerResponse<TResult> Failure< TOtherResult >( LayerResponse<TOtherResult> layerResponse, TResult? re |
| | 139 | 61 | | => new( false, layerResponse.Errors, result ); |
| | | 62 | | |
| | | 63 | | /// <summary>Creates a successful response.</summary> |
| | | 64 | | /// <param name="result">The value.</param> |
| | | 65 | | /// <returns>A successful response.</returns> |
| | 34961 | 66 | | public static LayerResponse<TResult> Success( TResult? result = default ) => new( true, Error.None, result ); |
| | | 67 | | |
| | | 68 | | /// <summary>Copies the outcome into a <see cref="LayerResponse{TResult}"/> of another type, with no value.</summary |
| | | 69 | | /// <typeparam name="TResult2">The value type of the new response.</typeparam> |
| | | 70 | | /// <param name="result">The response to copy.</param> |
| | | 71 | | /// <returns>Success, or failure with the same errors.</returns> |
| | | 72 | | public static new LayerResponse<TResult2> ToLayerResponse< TResult2 >( LayerResponse result ) |
| | 0 | 73 | | => result.IsSuccess ? LayerResponse<TResult2>.Success() : LayerResponse<TResult2>.Failure( result.Errors ); |
| | | 74 | | |
| | | 75 | | /// <summary>Copies the outcome into a plain <see cref="LayerResponse"/>.</summary> |
| | | 76 | | /// <param name="result">The response to copy.</param> |
| | | 77 | | /// <returns>Success, or failure with the same errors.</returns> |
| | | 78 | | public static LayerResponse ToLayerResponse( LayerResponse result ) |
| | 0 | 79 | | => result.IsSuccess ? LayerResponse.Success() : LayerResponse.Failure( result.Errors ); |
| | | 80 | | |
| | | 81 | | |
| | | 82 | | /// <summary>Wraps a value as a successful response.</summary> |
| | | 83 | | /// <param name="result">The value.</param> |
| | 25252 | 84 | | public static implicit operator LayerResponse<TResult>( TResult? result ) => Success( result ); |
| | | 85 | | |
| | | 86 | | /// <summary>Gets the value.</summary> |
| | | 87 | | /// <param name="result">The response.</param> |
| | 4 | 88 | | public static implicit operator TResult( LayerResponse<TResult> result ) => result.Result!; |
| | | 89 | | |
| | | 90 | | /// <summary>Wraps an error as a failed response.</summary> |
| | | 91 | | /// <param name="error">The error.</param> |
| | 275 | 92 | | public static implicit operator LayerResponse<TResult>( Error error ) => Failure( error ); |
| | | 93 | | |
| | | 94 | | /// <summary>Wraps errors as a failed response.</summary> |
| | | 95 | | /// <param name="errors">The errors; at least one.</param> |
| | 11 | 96 | | public static implicit operator LayerResponse<TResult>( List<Error> errors ) => Failure( errors ); |
| | | 97 | | |
| | | 98 | | /// <summary>Wraps an exception as a failed response with <see cref="Error.Unknown"/>.</summary> |
| | | 99 | | /// <param name="exception">The exception.</param> |
| | 4 | 100 | | public static implicit operator LayerResponse<TResult>( Exception exception ) => Failure( Error.Unknown( exception ) |
| | | 101 | | |
| | | 102 | | /// <summary>Gets the first error, or <see cref="Error.None"/> on success.</summary> |
| | | 103 | | /// <param name="response">The response.</param> |
| | 0 | 104 | | public static implicit operator Error( LayerResponse<TResult> response ) => response.Errors.FirstOrDefault() ?? Erro |
| | | 105 | | |
| | | 106 | | /// <summary>Gets the errors.</summary> |
| | | 107 | | /// <param name="response">The response.</param> |
| | 0 | 108 | | public static implicit operator List<Error>( LayerResponse<TResult> response ) => response.Errors; |
| | | 109 | | } |