< Summary - ZhEaIsNsAaBn.Essentials coverage

Information
Class: ZhEaIsNsAaBn.Utilities.Severity
Assembly: ZhEaIsNsAaBn.LayerResponse
File(s): /_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/LayerResponse/Errors/Severity.cs
Line coverage
72%
Covered lines: 13
Uncovered lines: 5
Coverable lines: 18
Total lines: 77
Line coverage: 72.2%
Branch coverage
65%
Covered branches: 13
Total branches: 20
Branch coverage: 65%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
Deconstruct(...)100%210%
Read(...)61.11%341863.63%
Write(...)100%22100%

File(s)

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

#LineLine coverage
 1#region Usings
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4#endregion
 5
 6namespace ZhEaIsNsAaBn.Utilities;
 7
 8/// <summary>How serious an <see cref="Error"/> is. Compared by <see cref="Value"/>.</summary>
 9/// <remarks>
 10/// JSON: a plain string, e.g. <c>"High"</c> — not <c>{"Value":"High"}</c> (the default for a
 11/// single-property record). <see cref="Unknown"/> writes as <c>"Unknown"</c> and reads back from
 12/// <c>"Unknown"</c> or <c>""</c>; its in-memory <see cref="Value"/> stays an empty string.
 13/// </remarks>
 14[ JsonConverter( typeof( SeverityJsonConverter ) ) ]
 15public sealed record Severity
 16{
 17    /// <summary>Serious; needs attention.</summary>
 318    public static readonly Severity High = new( nameof( High ) );
 19
 20    /// <summary>Minor.</summary>
 321    public static readonly Severity Low = new( nameof( Low ) );
 22
 23    /// <summary>Normal; the default for new errors.</summary>
 324    public static readonly Severity Medium = new( nameof( Medium ) );
 25
 26    /// <summary>Not set. <see cref="Value"/> is an empty string.</summary>
 327    public static readonly Severity Unknown = new( string.Empty );
 28
 29
 2430    private Severity( string value ) => Value = value;
 31
 32
 33    /// <summary>The severity name: <c>High</c>, <c>Medium</c>, <c>Low</c>, or empty for <see cref="Unknown"/>.</summary
 34    public string Value { get; init; }
 35
 36
 37    /// <summary>Gets the severity name.</summary>
 38    /// <param name="value">Receives <see cref="Value"/>.</param>
 039    public void Deconstruct( out string value ) => value = Value;
 40
 41
 42    /// <summary>Reads/writes a <see cref="Severity"/> as a plain JSON string.</summary>
 43    public sealed class SeverityJsonConverter : JsonConverter<Severity>
 44    {
 45        /// <summary>Reads a plain JSON string into the matching severity.</summary>
 46        /// <param name="reader">The JSON reader, positioned on the severity value.</param>
 47        /// <param name="typeToConvert">Always <see cref="Severity"/>.</param>
 48        /// <param name="options">The serializer options.</param>
 49        /// <returns>The matching severity; <see cref="Unknown"/> for <c>null</c>, <c>""</c> or <c>"Unknown"</c>.</retur
 50        /// <exception cref="JsonException">The JSON value is not a string.</exception>
 51        public override Severity? Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options )
 52        {
 1253            if ( reader.TokenType is JsonTokenType.Null ) { return Unknown; }
 54
 1255            if ( reader.TokenType is not JsonTokenType.String )
 56            {
 057                throw new JsonException( "Severity must be a JSON string, e.g. \"High\"." );
 58            }
 59
 1260            return reader.GetString() switch
 1261            {
 662                nameof( High )                 => High,
 063                nameof( Medium )               => Medium,
 064                nameof( Low )                  => Low,
 665                nameof( Unknown ) or "" or null => Unknown,
 066                var other                      => new Severity( other ),
 1267            };
 68        }
 69
 70        /// <summary>Writes a severity as a plain JSON string.</summary>
 71        /// <param name="writer">The JSON writer.</param>
 72        /// <param name="value">The severity to write; <see cref="Unknown"/> writes as <c>"Unknown"</c>.</param>
 73        /// <param name="options">The serializer options.</param>
 74        public override void Write( Utf8JsonWriter writer, Severity value, JsonSerializerOptions options )
 1875            => writer.WriteStringValue( value.Value.Length == 0 ? nameof( Unknown ) : value.Value );
 76    }
 77}