| | | 1 | | //======================================================================= |
| | | 2 | | // ThrowInterpolatedStringHandler.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 | | |
| | | 8 | | namespace Chronicler.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | /// <summary>Builds an exception message only for a failing guard.</summary> |
| | | 14 | | /// <remarks>The compiler uses the constructor's gate to skip interpolation expressions when disabled. Append calls on a |
| | | 15 | | [InterpolatedStringHandler] |
| | | 16 | | public ref struct ThrowInterpolatedStringHandler |
| | | 17 | | { |
| | | 18 | | private DiagnosticInterpolatedStringHandler _message; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="ThrowInterpolatedStringHandler"/> struct. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="literalLength">The compiler-supplied count of literal UTF-16 code units.</param> |
| | | 24 | | /// <param name="formattedCount">The compiler-supplied number of interpolation holes.</param> |
| | | 25 | | /// <param name="condition">The throw condition that controls whether formatting occurs.</param> |
| | | 26 | | /// <param name="isEnabled">Set to true to evaluate interpolation expressions, or false to skip them.</param> |
| | | 27 | | public ThrowInterpolatedStringHandler( |
| | | 28 | | int literalLength, |
| | | 29 | | int formattedCount, |
| | | 30 | | bool condition, |
| | | 31 | | out bool isEnabled) |
| | | 32 | | { |
| | 22 | 33 | | _message = new DiagnosticInterpolatedStringHandler(literalLength, formattedCount, condition, out isEnabled); |
| | 22 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary>Gets the captured formatting gate.</summary> |
| | | 37 | | /// <value>True when the handler was constructed to build a message.</value> |
| | 2 | 38 | | public bool IsEnabled => _message.IsEnabled; |
| | | 39 | | |
| | 10 | 40 | | internal string GetFormattedText() => _message.GetFormattedText(); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendLiteral(string)"/> |
| | | 43 | | public void AppendLiteral(string value) |
| | | 44 | | { |
| | 27 | 45 | | _message.AppendLiteral(value); |
| | 27 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T)"/> |
| | | 49 | | public void AppendFormatted<T>(T value) |
| | | 50 | | { |
| | 9 | 51 | | _message.AppendFormatted(value); |
| | 9 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T, string)"/> |
| | | 55 | | public void AppendFormatted<T>(T value, string? format) |
| | | 56 | | { |
| | 1 | 57 | | _message.AppendFormatted(value, format); |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T, int)"/> |
| | | 61 | | public void AppendFormatted<T>(T value, int alignment) |
| | | 62 | | { |
| | 1 | 63 | | _message.AppendFormatted(value, alignment); |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T, int, string)"/> |
| | | 67 | | public void AppendFormatted<T>(T value, int alignment, string? format) |
| | | 68 | | { |
| | 1 | 69 | | _message.AppendFormatted(value, alignment, format); |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted(string)"/> |
| | | 73 | | public void AppendFormatted(string? value) |
| | | 74 | | { |
| | 1 | 75 | | _message.AppendFormatted(value); |
| | 1 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted(string, int)"/> |
| | | 79 | | public void AppendFormatted(string? value, int alignment) |
| | | 80 | | { |
| | 1 | 81 | | _message.AppendFormatted(value, alignment); |
| | 1 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted(string, int, string)"/> |
| | | 85 | | public void AppendFormatted(string? value, int alignment, string? format) |
| | | 86 | | { |
| | 1 | 87 | | _message.AppendFormatted(value, alignment, format); |
| | 1 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted(ReadOnlySpan{char})"/> |
| | | 91 | | public void AppendFormatted(ReadOnlySpan<char> value) |
| | | 92 | | { |
| | 1 | 93 | | _message.AppendFormatted(value); |
| | 1 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted(ReadOnlySpan{char}, int)"/> |
| | | 97 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment) |
| | | 98 | | { |
| | 1 | 99 | | _message.AppendFormatted(value, alignment); |
| | 1 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted(ReadOnlySpan{char}, int, string)"/> |
| | | 103 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format) |
| | | 104 | | { |
| | 1 | 105 | | _message.AppendFormatted(value, alignment, format); |
| | 1 | 106 | | } |
| | | 107 | | } |