| | | 1 | | namespace ZhEaIsNsAaBn.Utilities; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A <see cref="LayerResponse"/> 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 | | public class LayerResponseAsync : LayerResponse |
| | | 8 | | { |
| | | 9 | | /// <summary>Creates a response from one error.</summary> |
| | | 10 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 11 | | /// <param name="error"><see cref="Error.None"/> on success; a real error on failure.</param> |
| | | 12 | | /// <exception cref="InvalidOperationException">Success with a real error, or failure with <see cref="Error.None"/>. |
| | 0 | 13 | | protected LayerResponseAsync( bool isSuccess, Error error ) : base( isSuccess, error ) { } |
| | | 14 | | |
| | | 15 | | /// <summary>Creates a response from a list of errors.</summary> |
| | | 16 | | /// <param name="isSuccess">Whether the operation succeeded.</param> |
| | | 17 | | /// <param name="errors">Empty or <see langword="null"/> on success; at least one real error on failure.</param> |
| | | 18 | | /// <exception cref="InvalidOperationException">Success with errors, or failure with no real error.</exception> |
| | 8 | 19 | | protected LayerResponseAsync( bool isSuccess, List<Error>? errors = null ) : base( isSuccess, errors ) { } |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | /// <summary>Whether the work was paused rather than finished.</summary> |
| | | 23 | | public bool IsPaused { get; protected internal set; } |
| | | 24 | | |
| | | 25 | | |
| | | 26 | | /// <summary>Creates a failed response.</summary> |
| | | 27 | | /// <param name="error">The error.</param> |
| | | 28 | | /// <returns>A failed response.</returns> |
| | 0 | 29 | | public static new LayerResponseAsync Failure( Error error ) => new( false, error ); |
| | | 30 | | |
| | | 31 | | /// <summary>Creates a failed response.</summary> |
| | | 32 | | /// <param name="errors">The errors; at least one.</param> |
| | | 33 | | /// <returns>A failed response.</returns> |
| | 0 | 34 | | public static new LayerResponseAsync Failure( List<Error> errors ) => new( false, errors ); |
| | | 35 | | |
| | | 36 | | /// <summary>Creates a paused response. It is a failure if <paramref name="errors"/> has any errors.</summary> |
| | | 37 | | /// <param name="errors">Errors that caused the pause, if any.</param> |
| | | 38 | | /// <returns>A paused response.</returns> |
| | 4 | 39 | | public static LayerResponseAsync Pause( List<Error>? errors ) => new( errors is not { Count: > 0 }, errors ) { IsPau |
| | | 40 | | |
| | | 41 | | /// <summary>Creates a paused failure.</summary> |
| | | 42 | | /// <param name="error">The error that caused the pause.</param> |
| | | 43 | | /// <returns>A paused, failed response.</returns> |
| | 4 | 44 | | public static LayerResponseAsync Pause( Error error ) => Pause( [ error ] ); |
| | | 45 | | |
| | | 46 | | /// <summary>Creates a paused response with no errors.</summary> |
| | | 47 | | /// <returns>A paused, successful response.</returns> |
| | 0 | 48 | | public static LayerResponseAsync Pause() => Pause( errors: null ); |
| | | 49 | | |
| | | 50 | | /// <summary>Creates a successful response.</summary> |
| | | 51 | | /// <returns>A successful response.</returns> |
| | 0 | 52 | | public static new LayerResponseAsync Success() => new( true, Error.None ); |
| | | 53 | | |
| | | 54 | | /// <summary>Copies the outcome, including a pause, into a <see cref="LayerResponseAsync{TResult}"/> with no value.< |
| | | 55 | | /// <typeparam name="TResult">The value type of the new response.</typeparam> |
| | | 56 | | /// <param name="result">The response to copy.</param> |
| | | 57 | | /// <returns>The same pause, success or failure.</returns> |
| | | 58 | | public static LayerResponseAsync<TResult> ToLayerResponseAsync< TResult >( LayerResponseAsync result ) |
| | | 59 | | { |
| | 0 | 60 | | if ( result.IsPaused ) { return LayerResponseAsync<TResult>.Pause( result.Errors ); } |
| | | 61 | | |
| | | 62 | | |
| | 0 | 63 | | return result.IsFailure ? LayerResponseAsync<TResult>.Failure( result.Errors ) : LayerResponseAsync<TResult>.Suc |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | |
| | | 67 | | /// <summary>Wraps an error as a failed response.</summary> |
| | | 68 | | /// <param name="error">The error.</param> |
| | 0 | 69 | | public static implicit operator LayerResponseAsync( Error error ) => Failure( error ); |
| | | 70 | | |
| | | 71 | | /// <summary>Wraps errors as a failed response.</summary> |
| | | 72 | | /// <param name="errors">The errors; at least one.</param> |
| | 0 | 73 | | public static implicit operator LayerResponseAsync( List<Error> errors ) => Failure( errors ); |
| | | 74 | | |
| | | 75 | | /// <summary>Gets the first error, or <see cref="Error.None"/> if there is none.</summary> |
| | | 76 | | /// <param name="response">The response.</param> |
| | 0 | 77 | | public static implicit operator Error( LayerResponseAsync response ) => response.Errors.FirstOrDefault() ?? Error.No |
| | | 78 | | |
| | | 79 | | /// <summary>Gets the errors.</summary> |
| | | 80 | | /// <param name="response">The response.</param> |
| | 0 | 81 | | public static implicit operator List<Error>( LayerResponseAsync response ) => response.Errors; |
| | | 82 | | } |