< Summary

Information
Class: Chronicler.Diagnostics.DiagnosticInterpolatedStringHandler
Assembly: Chronicler.Diagnostics
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler.Diagnostics/DiagnosticInterpolatedStringHandler.cs
Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 330
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
.ctor(...)100%22100%
get_IsEnabled()100%11100%
AppendLiteral(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
AppendFormatted(...)100%22100%
GetFormattedText()100%11100%

File(s)

/home/runner/work/Chronicler/Chronicler/src/Chronicler.Diagnostics/DiagnosticInterpolatedStringHandler.cs

#LineLine coverage
 1//=======================================================================
 2// DiagnosticInterpolatedStringHandler.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8namespace Chronicler.Diagnostics;
 9
 10using System;
 11using System.Runtime.CompilerServices;
 12#if !NET6_0_OR_GREATER
 13using System.Text;
 14#endif
 15
 16/// <summary>Builds a diagnostic message using an enablement decision captured at construction.</summary>
 17/// <remarks>The compiler uses the constructor's gate to skip interpolation expressions when disabled. Append calls on a
 18[InterpolatedStringHandler]
 19public ref struct DiagnosticInterpolatedStringHandler
 20{
 21    private readonly bool _isEnabled;
 22#if NET6_0_OR_GREATER
 23    private DefaultInterpolatedStringHandler _builder;
 24#else
 25    private StringBuilder? _builder;
 26#endif
 27
 28    /// <summary>
 29    /// Initializes a new instance of the <see cref="DiagnosticInterpolatedStringHandler"/> struct.
 30    /// </summary>
 31    /// <param name="literalLength">The compiler-supplied count of literal UTF-16 code units.</param>
 32    /// <param name="formattedCount">The compiler-supplied number of interpolation holes.</param>
 33    /// <param name="channel">The channel whose threshold controls formatting. Null disables the handler.</param>
 34    /// <param name="level">The severity level being evaluated.</param>
 35    /// <param name="isEnabled">
 36    /// Set to <c>true</c> when formatted expressions should be evaluated; otherwise, <c>false</c>.
 37    /// </param>
 38    public DiagnosticInterpolatedStringHandler(
 39        int literalLength,
 40        int formattedCount,
 41        DiagnosticChannel channel,
 42        DiagnosticLevel level,
 43        out bool isEnabled)
 22644        : this(literalLength, formattedCount, channel != null && channel.IsEnabled(level), out isEnabled)
 22645    { }
 46
 47    internal DiagnosticInterpolatedStringHandler(
 48        int literalLength,
 49        int formattedCount,
 50        bool shouldFormat,
 51        out bool isEnabled)
 52    {
 24853        isEnabled = shouldFormat;
 24854        _isEnabled = isEnabled;
 55#if NET6_0_OR_GREATER
 24856        _builder = isEnabled ? new DefaultInterpolatedStringHandler(literalLength, formattedCount) : default;
 57#else
 58        _ = formattedCount;
 59        _builder = isEnabled ? new StringBuilder(literalLength) : null;
 60#endif
 24861    }
 62
 63    /// <summary>Gets the captured formatting gate.</summary>
 64    /// <value>True when the handler was constructed to build a message.</value>
 22665    public bool IsEnabled => _isEnabled;
 66
 67    /// <summary>
 68    /// Appends a literal string segment.
 69    /// </summary>
 70    /// <param name="value">The literal string segment.</param>
 71    public void AppendLiteral(string value)
 72    {
 5973        if (!_isEnabled)
 274            return;
 75
 76#if NET6_0_OR_GREATER
 5777        _builder.AppendLiteral(value);
 78#else
 79        _builder!.Append(value);
 80#endif
 5781    }
 82
 83    /// <summary>
 84    /// Appends a formatted value.
 85    /// </summary>
 86    /// <typeparam name="T">The static type of the interpolation expression.</typeparam>
 87    /// <param name="value">The expression result. Null contributes no text before alignment.</param>
 88    public void AppendFormatted<T>(T value)
 89    {
 1990        if (!_isEnabled)
 191            return;
 92
 93#if NET6_0_OR_GREATER
 1894        _builder.AppendFormatted(value);
 95#else
 96        _builder!.Append(value);
 97#endif
 1898    }
 99
 100    /// <summary>
 101    /// Appends a formatted value using the specified format string.
 102    /// </summary>
 103    /// <typeparam name="T">The static type of the interpolation expression.</typeparam>
 104    /// <param name="value">The expression result. Null contributes no text before alignment.</param>
 105    /// <param name="format">The format passed to the value's formatter. Null selects its default.</param>
 106    public void AppendFormatted<T>(T value, string? format)
 107    {
 4108        if (!_isEnabled)
 1109            return;
 110
 111#if NET6_0_OR_GREATER
 3112        _builder.AppendFormatted(value, format);
 113#else
 114        AppendFormattedValue(value, 0, format);
 115#endif
 3116    }
 117
 118    /// <summary>
 119    /// Appends a formatted value with the specified alignment.
 120    /// </summary>
 121    /// <typeparam name="T">The static type of the interpolation expression.</typeparam>
 122    /// <param name="value">The expression result. Null contributes no text before alignment.</param>
 123    /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab
 124    public void AppendFormatted<T>(T value, int alignment)
 125    {
 4126        if (!_isEnabled)
 1127            return;
 128
 129#if NET6_0_OR_GREATER
 3130        _builder.AppendFormatted(value, alignment);
 131#else
 132        AppendFormattedValue(value, alignment, null);
 133#endif
 3134    }
 135
 136    /// <summary>
 137    /// Appends a formatted value with the specified alignment and format string.
 138    /// </summary>
 139    /// <typeparam name="T">The static type of the interpolation expression.</typeparam>
 140    /// <param name="value">The expression result. Null contributes no text before alignment.</param>
 141    /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab
 142    /// <param name="format">The format passed to the value's formatter. Null selects its default.</param>
 143    public void AppendFormatted<T>(T value, int alignment, string? format)
 144    {
 10145        if (!_isEnabled)
 1146            return;
 147
 148#if NET6_0_OR_GREATER
 9149        _builder.AppendFormatted(value, alignment, format);
 150#else
 151        AppendFormattedValue(value, alignment, format);
 152#endif
 9153    }
 154
 155    /// <summary>
 156    /// Appends a string value.
 157    /// </summary>
 158    /// <param name="value">The text to append. Null contributes no characters.</param>
 159    public void AppendFormatted(string? value)
 160    {
 4161        if (!_isEnabled)
 1162            return;
 163
 164#if NET6_0_OR_GREATER
 3165        _builder.AppendFormatted(value);
 166#else
 167        _builder!.Append(value);
 168#endif
 3169    }
 170
 171    /// <summary>
 172    /// Appends a string value with the specified alignment.
 173    /// </summary>
 174    /// <param name="value">The expression result. Null contributes no text before alignment.</param>
 175    /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab
 176    public void AppendFormatted(string? value, int alignment)
 177    {
 3178        if (!_isEnabled)
 1179            return;
 180
 181#if NET6_0_OR_GREATER
 2182        _builder.AppendFormatted(value, alignment);
 183#else
 184        AppendAligned(value, alignment);
 185#endif
 2186    }
 187
 188    /// <summary>
 189    /// Appends a string value with the specified alignment and format string.
 190    /// </summary>
 191    /// <param name="value">The expression result. Null contributes no text before alignment.</param>
 192    /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab
 193    /// <param name="format">Accepted for interpolation syntax. The input is appended as text.</param>
 194    public void AppendFormatted(string? value, int alignment, string? format)
 195    {
 3196        if (!_isEnabled)
 1197            return;
 198
 199#if NET6_0_OR_GREATER
 2200        _builder.AppendFormatted(value, alignment, format);
 201#else
 202        AppendAligned(value, alignment);
 203#endif
 2204    }
 205
 206    /// <summary>
 207    /// Appends a character span.
 208    /// </summary>
 209    /// <param name="value">The span whose characters are copied into the message.</param>
 210    public void AppendFormatted(ReadOnlySpan<char> value)
 211    {
 3212        if (!_isEnabled)
 1213            return;
 214
 215#if NET6_0_OR_GREATER
 2216        _builder.AppendFormatted(value);
 217#else
 218        _builder!.Append(value);
 219#endif
 2220    }
 221
 222    /// <summary>
 223    /// Appends a character span with the specified alignment.
 224    /// </summary>
 225    /// <param name="value">The span whose characters are copied into the message.</param>
 226    /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab
 227    public void AppendFormatted(ReadOnlySpan<char> value, int alignment)
 228    {
 9229        if (!_isEnabled)
 1230            return;
 231
 232#if NET6_0_OR_GREATER
 8233        _builder.AppendFormatted(value, alignment);
 234#else
 235        AppendAligned(value, alignment);
 236#endif
 8237    }
 238
 239    /// <summary>
 240    /// Appends a character span with the specified alignment and format string.
 241    /// </summary>
 242    /// <param name="value">The span whose characters are copied into the message.</param>
 243    /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab
 244    /// <param name="format">Accepted for interpolation syntax. The input is appended as text.</param>
 245    public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format)
 246    {
 3247        if (!_isEnabled)
 1248            return;
 249
 250#if NET6_0_OR_GREATER
 2251        _builder.AppendFormatted(value, alignment, format);
 252#else
 253        AppendAligned(value, alignment);
 254#endif
 2255    }
 256
 257    internal string GetFormattedText()
 258    {
 259#if NET6_0_OR_GREATER
 27260        return _builder.ToStringAndClear();
 261#else
 262        return _builder?.ToString() ?? string.Empty;
 263#endif
 264    }
 265
 266#if !NET6_0_OR_GREATER
 267    private void AppendFormattedValue<T>(T value, int alignment, string? format)
 268    {
 269        string? text = value is IFormattable formattable
 270            ? formattable.ToString(format, null)
 271            : value?.ToString();
 272
 273        AppendAligned(text, alignment);
 274    }
 275
 276    private void AppendAligned(string? value, int alignment)
 277    {
 278        value ??= string.Empty;
 279
 280        if (alignment == 0)
 281        {
 282            _builder!.Append(value);
 283            return;
 284        }
 285
 286        int width = alignment < 0 ? -alignment : alignment;
 287        int padding = width - value.Length;
 288
 289        if (padding <= 0)
 290        {
 291            _builder!.Append(value);
 292            return;
 293        }
 294
 295        if (alignment > 0)
 296            _builder!.Append(' ', padding);
 297
 298        _builder!.Append(value);
 299
 300        if (alignment < 0)
 301            _builder!.Append(' ', padding);
 302    }
 303
 304    private void AppendAligned(ReadOnlySpan<char> value, int alignment)
 305    {
 306        if (alignment == 0)
 307        {
 308            _builder!.Append(value);
 309            return;
 310        }
 311
 312        int width = alignment < 0 ? -alignment : alignment;
 313        int padding = width - value.Length;
 314
 315        if (padding <= 0)
 316        {
 317            _builder!.Append(value);
 318            return;
 319        }
 320
 321        if (alignment > 0)
 322            _builder!.Append(' ', padding);
 323
 324        _builder!.Append(value);
 325
 326        if (alignment < 0)
 327            _builder!.Append(' ', padding);
 328    }
 329#endif
 330}