< Summary - ZhEaIsNsAaBn.Essentials coverage

Information
Class: ZhEaIsNsAaBn.Utilities.LayerResponse
Assembly: ZhEaIsNsAaBn.LayerResponse
File(s): /_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/LayerResponse/LayerResponse.cs
Line coverage
75%
Covered lines: 15
Uncovered lines: 5
Coverable lines: 20
Total lines: 92
Line coverage: 75%
Branch coverage
57%
Covered branches: 15
Total branches: 26
Branch coverage: 57.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)87.5%88100%
.ctor(...)50%211466.66%
get_IsFailure()100%11100%
Failure(...)100%11100%
Failure(...)100%11100%
Success()100%11100%
ToLayerResponse(...)0%620%
op_Implicit(...)100%11100%
op_Implicit(...)100%210%
op_Implicit(...)50%22100%
op_Implicit(...)100%210%

File(s)

/_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/LayerResponse/LayerResponse.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.Utilities;
 2
 3/// <summary>
 4/// The result of a fallible operation with no value: either success, or failure with one or more <see cref="Error"/>s
 5/// (STYLE_GUIDE §3). Return an <see cref="Error"/> or a list of them directly; implicit conversions build the response.
 6/// </summary>
 7public class 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"/>.
 2413    protected LayerResponse( bool isSuccess, Error error )
 14    {
 2415        if ( isSuccess && error != Error.None ) { throw new InvalidOperationException( "A successful response can't carr
 16
 17
 2718        if ( !isSuccess && error == Error.None ) { throw new InvalidOperationException( "A failed response needs an erro
 19
 20
 21        IsSuccess = isSuccess;
 2122        Errors    = isSuccess ? [] : [ error ];
 2123    }
 24
 25    /// <summary>Creates a response from a list of errors.</summary>
 26    /// <param name="isSuccess">Whether the operation succeeded.</param>
 27    /// <param name="errors">Empty or <see langword="null"/> on success; at least one real error on failure.</param>
 28    /// <exception cref="InvalidOperationException">Success with errors, or failure with no real error.</exception>
 329    protected LayerResponse( bool isSuccess, List<Error>? errors = null )
 30    {
 331        if ( isSuccess && errors?.Count > 0 ) { throw new InvalidOperationException( "A successful response can't carry 
 32
 33
 334        if ( !isSuccess && ( errors is null || errors.Count == 0 || errors.Exists( e => e == Error.None ) ) )
 35        {
 336            throw new InvalidOperationException( "A failed response needs at least one real error." );
 37        }
 38
 39
 40        IsSuccess = isSuccess;
 041        Errors    = errors ?? [];
 042    }
 43
 44
 45    /// <summary>The errors. Empty on success.</summary>
 46    public List<Error> Errors { get; }
 47
 48    /// <summary>Whether the operation succeeded.</summary>
 49    public bool IsSuccess { get; }
 50
 51    /// <summary>Whether the operation failed.</summary>
 1552    public bool IsFailure => !IsSuccess;
 53
 54
 55    /// <summary>Creates a failed response.</summary>
 56    /// <param name="error">The error.</param>
 57    /// <returns>A failed response.</returns>
 658    public static LayerResponse Failure( Error error ) => new( false, error );
 59
 60    /// <summary>Creates a failed response.</summary>
 61    /// <param name="errors">The errors; at least one.</param>
 62    /// <returns>A failed response.</returns>
 363    public static LayerResponse Failure( List<Error> errors ) => new( false, errors );
 64
 65    /// <summary>Creates a successful response.</summary>
 66    /// <returns>A successful response.</returns>
 367    public static LayerResponse Success() => new( true, Error.None );
 68
 69    /// <summary>Copies the outcome into a <see cref="LayerResponse{TResult}"/> with no value.</summary>
 70    /// <typeparam name="TResult">The value type of the new response.</typeparam>
 71    /// <param name="result">The response to copy.</param>
 72    /// <returns>Success, or failure with the same errors.</returns>
 73    public static LayerResponse<TResult> ToLayerResponse< TResult >( LayerResponse result )
 074        => result.IsSuccess ? LayerResponse<TResult>.Success() : LayerResponse<TResult>.Failure( result.Errors );
 75
 76
 77    /// <summary>Wraps an error as a failed response.</summary>
 78    /// <param name="error">The error.</param>
 379    public static implicit operator LayerResponse( Error error ) => Failure( error );
 80
 81    /// <summary>Wraps errors as a failed response.</summary>
 82    /// <param name="errors">The errors; at least one.</param>
 083    public static implicit operator LayerResponse( List<Error> errors ) => Failure( errors );
 84
 85    /// <summary>Gets the first error, or <see cref="Error.None"/> on success.</summary>
 86    /// <param name="response">The response.</param>
 387    public static implicit operator Error( LayerResponse response ) => response.Errors.FirstOrDefault() ?? Error.None;
 88
 89    /// <summary>Gets the errors.</summary>
 90    /// <param name="response">The response.</param>
 091    public static implicit operator List<Error>( LayerResponse response ) => response.Errors;
 92}