< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.Utilities.LayerResponse
Assembly: ZhEaIsNsAaBn.Utilities.LayerResponse
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.LayerResponse/LayerResponse.cs
Line coverage
85%
Covered lines: 17
Uncovered lines: 3
Coverable lines: 20
Total lines: 92
Line coverage: 85%
Branch coverage
73%
Covered branches: 19
Total branches: 26
Branch coverage: 73%
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(...)78.57%1414100%
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)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.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"/>.
 3632913    protected LayerResponse( bool isSuccess, Error error )
 14    {
 3632915        if ( isSuccess && error != Error.None ) { throw new InvalidOperationException( "A successful response can't carr
 16
 17
 3633318        if ( !isSuccess && error == Error.None ) { throw new InvalidOperationException( "A failed response needs an erro
 19
 20
 21        IsSuccess = isSuccess;
 3632522        Errors    = isSuccess ? [] : [ error ];
 3632523    }
 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>
 24229    protected LayerResponse( bool isSuccess, List<Error>? errors = null )
 30    {
 24231        if ( isSuccess && errors?.Count > 0 ) { throw new InvalidOperationException( "A successful response can't carry 
 32
 33
 24234        if ( !isSuccess && ( errors is null || errors.Count == 0 || errors.Exists( e => e == Error.None ) ) )
 35        {
 436            throw new InvalidOperationException( "A failed response needs at least one real error." );
 37        }
 38
 39
 40        IsSuccess = isSuccess;
 23841        Errors    = errors ?? [];
 23842    }
 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>
 3470952    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>
 13958    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>
 4063    public static LayerResponse Failure( List<Error> errors ) => new( false, errors );
 64
 65    /// <summary>Creates a successful response.</summary>
 66    /// <returns>A successful response.</returns>
 90267    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>
 2479    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>
 487    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}