| | | 1 | | namespace ZhEaIsNsAaBn.Utilities; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A <see cref="LayerResponse{TResult}"/> for long-running work that can also be <see cref="IsPaused">paused</see>. |
| | | 5 | | /// A pause with no errors counts as success; a pause with errors counts as failure. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <typeparam name="TResult">The value type.</typeparam> |
| | | 8 | | public class LayerResponseAsync< TResult > : LayerResponse<TResult> |
| | | 9 | | { |
| | | 10 | | /// <summary>Creates a response from one error.</summary> |
| | | 11 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 12 | | /// <param name="error"><see cref="Error.None"/> on success; a real error on failure.</param> |
| | | 13 | | /// <param name="result">The value, if any.</param> |
| | | 14 | | /// <exception cref="InvalidOperationException">Success with a real error, or failure with <see cref="Error.None"/>. |
| | 0 | 15 | | protected LayerResponseAsync( bool isSuccess, Error error, TResult? result = default ) : base( isSuccess, error, res |
| | | 16 | | |
| | | 17 | | /// <summary>Creates a response from a list of errors.</summary> |
| | | 18 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 19 | | /// <param name="errors">Empty or <see langword="null"/> on success; at least one real error on failure.</param> |
| | | 20 | | /// <param name="result">The value, if any.</param> |
| | | 21 | | /// <exception cref="InvalidOperationException">Success with errors, or failure with no real error.</exception> |
| | 24 | 22 | | protected LayerResponseAsync( bool isSuccess, List<Error>? errors, TResult? result = default ) : base( isSuccess, er |
| | | 23 | | |
| | | 24 | | |
| | | 25 | | /// <summary>Whether the work was paused rather than finished.</summary> |
| | | 26 | | public bool IsPaused { get; protected internal set; } |
| | | 27 | | |
| | | 28 | | |
| | | 29 | | /// <summary>Creates a failed response.</summary> |
| | | 30 | | /// <param name="error">The error.</param> |
| | | 31 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 32 | | /// <returns>A failed response.</returns> |
| | 0 | 33 | | public static new LayerResponseAsync<TResult> Failure( Error error, TResult? result = default ) => new( false, error |
| | | 34 | | |
| | | 35 | | /// <summary>Creates a failed response.</summary> |
| | | 36 | | /// <param name="errors">The errors; at least one.</param> |
| | | 37 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 38 | | /// <returns>A failed response.</returns> |
| | 0 | 39 | | public static new LayerResponseAsync<TResult> Failure( List<Error> errors, TResult? result = default ) => new( false |
| | | 40 | | |
| | | 41 | | /// <summary>Creates a failed response with the errors of another response.</summary> |
| | | 42 | | /// <param name="layerResponse">The failed response to copy errors from.</param> |
| | | 43 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 44 | | /// <returns>A failed response.</returns> |
| | | 45 | | public static LayerResponseAsync<TResult> Failure( LayerResponseAsync layerResponse, TResult? result = default ) |
| | 0 | 46 | | => new( false, layerResponse.Errors, result ); |
| | | 47 | | |
| | | 48 | | /// <summary>Creates a failed response with the errors of another typed response.</summary> |
| | | 49 | | /// <typeparam name="TOtherResult">The value type of the source response.</typeparam> |
| | | 50 | | /// <param name="layerResponse">The failed response to copy errors from.</param> |
| | | 51 | | /// <param name="result">An optional value to return with the failure.</param> |
| | | 52 | | /// <returns>A failed response.</returns> |
| | | 53 | | public static LayerResponseAsync<TResult> Failure< TOtherResult >( LayerResponseAsync<TOtherResult> layerResponse, T |
| | 0 | 54 | | => new( false, layerResponse.Errors, result ); |
| | | 55 | | |
| | | 56 | | /// <summary>Creates a paused response. It is a failure if <paramref name="errors"/> has any errors.</summary> |
| | | 57 | | /// <param name="errors">Errors that caused the pause, if any.</param> |
| | | 58 | | /// <param name="result">The value so far, if any.</param> |
| | | 59 | | /// <returns>A paused response.</returns> |
| | | 60 | | public static LayerResponseAsync<TResult> Pause( List<Error>? errors, TResult? result ) |
| | 12 | 61 | | => new( errors is not { Count: > 0 }, errors, result ) { IsPaused = true }; |
| | | 62 | | |
| | | 63 | | /// <summary>Creates a paused response with no value.</summary> |
| | | 64 | | /// <param name="errors">Errors that caused the pause, if any.</param> |
| | | 65 | | /// <returns>A paused response.</returns> |
| | 4 | 66 | | public static LayerResponseAsync<TResult> Pause( List<Error>? errors ) => Pause( errors, default ); |
| | | 67 | | |
| | | 68 | | /// <summary>Creates a paused failure with no value.</summary> |
| | | 69 | | /// <param name="error">The error that caused the pause.</param> |
| | | 70 | | /// <returns>A paused, failed response.</returns> |
| | 4 | 71 | | public static LayerResponseAsync<TResult> Pause( Error error ) => Pause( [ error ], default ); |
| | | 72 | | |
| | | 73 | | /// <summary>Creates a paused failure with a value.</summary> |
| | | 74 | | /// <param name="error">The error that caused the pause.</param> |
| | | 75 | | /// <param name="result">The value so far.</param> |
| | | 76 | | /// <returns>A paused, failed response.</returns> |
| | 0 | 77 | | public static LayerResponseAsync<TResult> Pause( Error error, TResult? result ) => Pause( [ error ], result ); |
| | | 78 | | |
| | | 79 | | /// <summary>Creates a paused response with a value and no errors.</summary> |
| | | 80 | | /// <param name="result">The value so far.</param> |
| | | 81 | | /// <returns>A paused, successful response.</returns> |
| | 4 | 82 | | public static LayerResponseAsync<TResult> Pause( TResult? result ) => Pause( errors: null, result ); |
| | | 83 | | |
| | | 84 | | /// <summary>Creates a paused response with no value and no errors.</summary> |
| | | 85 | | /// <returns>A paused, successful response.</returns> |
| | 0 | 86 | | public static LayerResponseAsync<TResult> Pause() => Pause( errors: null, result: default ); |
| | | 87 | | |
| | | 88 | | /// <summary>Creates a successful response.</summary> |
| | | 89 | | /// <param name="result">The value.</param> |
| | | 90 | | /// <returns>A successful response.</returns> |
| | 0 | 91 | | public static new LayerResponseAsync<TResult> Success( TResult? result = default ) => new( true, Error.None, result |
| | | 92 | | |
| | | 93 | | /// <summary>Copies the outcome into a plain <see cref="LayerResponse"/>. A pause counts as success.</summary> |
| | | 94 | | /// <param name="result">The response to copy.</param> |
| | | 95 | | /// <returns>Success, or failure with the same errors.</returns> |
| | | 96 | | public static LayerResponse ToLayerResponse( LayerResponseAsync result ) |
| | 0 | 97 | | => result.IsSuccess || result.IsPaused ? LayerResponse.Success() : LayerResponse.Failure( result.Errors ); |
| | | 98 | | |
| | | 99 | | /// <summary>Copies the outcome, including a pause, into a <see cref="LayerResponseAsync{TResult}"/> of another type |
| | | 100 | | /// <typeparam name="TResult2">The value type of the new response.</typeparam> |
| | | 101 | | /// <param name="result">The response to copy.</param> |
| | | 102 | | /// <returns>The same pause, success or failure.</returns> |
| | | 103 | | public static LayerResponseAsync<TResult2> ToLayerResponseAsync< TResult2 >( LayerResponseAsync<TResult> result ) |
| | | 104 | | { |
| | 8 | 105 | | if ( result.IsPaused ) { return LayerResponseAsync<TResult2>.Pause( result.Errors ); } |
| | | 106 | | |
| | | 107 | | |
| | 0 | 108 | | return result.IsFailure ? LayerResponseAsync<TResult2>.Failure( result.Errors ) : LayerResponseAsync<TResult2>.S |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary>Copies the outcome, including a pause, into a <see cref="LayerResponseAsync"/>.</summary> |
| | | 112 | | /// <param name="result">The response to copy.</param> |
| | | 113 | | /// <returns>The same pause, success or failure.</returns> |
| | | 114 | | public static LayerResponseAsync ToLayerResponseAsync( LayerResponseAsync<TResult> result ) |
| | | 115 | | { |
| | 0 | 116 | | if ( result.IsPaused ) { return LayerResponseAsync.Pause( result.Errors ); } |
| | | 117 | | |
| | | 118 | | |
| | 0 | 119 | | return result.IsFailure ? LayerResponseAsync.Failure( result.Errors ) : LayerResponseAsync.Success(); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | |
| | | 123 | | /// <summary>Wraps a value as a successful response.</summary> |
| | | 124 | | /// <param name="result">The value.</param> |
| | 0 | 125 | | public static implicit operator LayerResponseAsync<TResult>( TResult? result ) => Success( result ); |
| | | 126 | | |
| | | 127 | | /// <summary>Gets the value.</summary> |
| | | 128 | | /// <param name="result">The response.</param> |
| | 0 | 129 | | public static implicit operator TResult( LayerResponseAsync<TResult> result ) => result.Result!; |
| | | 130 | | |
| | | 131 | | /// <summary>Wraps an error as a failed response.</summary> |
| | | 132 | | /// <param name="error">The error.</param> |
| | 0 | 133 | | public static implicit operator LayerResponseAsync<TResult>( Error error ) => Failure( error ); |
| | | 134 | | |
| | | 135 | | /// <summary>Wraps errors as a failed response.</summary> |
| | | 136 | | /// <param name="errors">The errors; at least one.</param> |
| | 0 | 137 | | public static implicit operator LayerResponseAsync<TResult>( List<Error> errors ) => Failure( errors ); |
| | | 138 | | |
| | | 139 | | /// <summary>Wraps an exception as a failed response with <see cref="Error.Unknown"/>.</summary> |
| | | 140 | | /// <param name="exception">The exception.</param> |
| | 0 | 141 | | public static implicit operator LayerResponseAsync<TResult>( Exception exception ) => Failure( Error.Unknown( except |
| | | 142 | | |
| | | 143 | | /// <summary>Gets the first error, or <see cref="Error.None"/> if there is none.</summary> |
| | | 144 | | /// <param name="response">The response.</param> |
| | 0 | 145 | | public static implicit operator Error( LayerResponseAsync<TResult> response ) => response.Errors.FirstOrDefault() ?? |
| | | 146 | | |
| | | 147 | | /// <summary>Gets the errors.</summary> |
| | | 148 | | /// <param name="response">The response.</param> |
| | 0 | 149 | | public static implicit operator List<Error>( LayerResponseAsync<TResult> response ) => response.Errors; |
| | | 150 | | } |