| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftThrowInterpolatedStringHandler.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 SwiftCollections.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Builds exception messages only when the guarded throw condition is true. |
| | | 15 | | /// </summary> |
| | | 16 | | [InterpolatedStringHandler] |
| | | 17 | | public ref struct SwiftThrowInterpolatedStringHandler |
| | | 18 | | { |
| | | 19 | | private DiagnosticInterpolatedStringHandler _message; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="SwiftThrowInterpolatedStringHandler"/> struct. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="literalLength">The combined length of literal portions in the interpolated string.</param> |
| | | 25 | | /// <param name="formattedCount">The number of formatted expressions in the interpolated string.</param> |
| | | 26 | | /// <param name="condition">The throw condition that controls whether formatting occurs.</param> |
| | | 27 | | /// <param name="isEnabled">Set to <see langword="true"/> when formatted expressions should be evaluated.</param> |
| | | 28 | | public SwiftThrowInterpolatedStringHandler( |
| | | 29 | | int literalLength, |
| | | 30 | | int formattedCount, |
| | | 31 | | bool condition, |
| | | 32 | | out bool isEnabled) |
| | | 33 | | { |
| | 3 | 34 | | _message = new DiagnosticInterpolatedStringHandler(literalLength, formattedCount, condition, out isEnabled); |
| | 3 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets whether this handler is actively building an exception message. |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public bool IsEnabled => _message.IsEnabled; |
| | | 41 | | |
| | 1 | 42 | | internal string GetFormattedText() => _message.GetFormattedText(); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Appends a literal string segment. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="value">The literal string segment.</param> |
| | | 48 | | public void AppendLiteral(string value) |
| | | 49 | | { |
| | 2 | 50 | | _message.AppendLiteral(value); |
| | 2 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Appends a formatted value. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 57 | | /// <param name="value">The value to append.</param> |
| | | 58 | | public void AppendFormatted<T>(T value) |
| | | 59 | | { |
| | 1 | 60 | | _message.AppendFormatted(value); |
| | 1 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Appends a formatted value using the specified format string. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 67 | | /// <param name="value">The value to append.</param> |
| | | 68 | | /// <param name="format">The format string to apply.</param> |
| | | 69 | | public void AppendFormatted<T>(T value, string? format) |
| | | 70 | | { |
| | 0 | 71 | | _message.AppendFormatted(value, format); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Appends a formatted value with the specified alignment. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 78 | | /// <param name="value">The value to append.</param> |
| | | 79 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 80 | | public void AppendFormatted<T>(T value, int alignment) |
| | | 81 | | { |
| | 0 | 82 | | _message.AppendFormatted(value, alignment); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Appends a formatted value with the specified alignment and format string. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 89 | | /// <param name="value">The value to append.</param> |
| | | 90 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 91 | | /// <param name="format">The format string to apply.</param> |
| | | 92 | | public void AppendFormatted<T>(T value, int alignment, string? format) |
| | | 93 | | { |
| | 0 | 94 | | _message.AppendFormatted(value, alignment, format); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Appends a string value. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="value">The value to append.</param> |
| | | 101 | | public void AppendFormatted(string? value) |
| | | 102 | | { |
| | 0 | 103 | | _message.AppendFormatted(value); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Appends a string value with the specified alignment. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="value">The value to append.</param> |
| | | 110 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 111 | | public void AppendFormatted(string? value, int alignment) |
| | | 112 | | { |
| | 0 | 113 | | _message.AppendFormatted(value, alignment); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Appends a string value with the specified alignment and format string. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="value">The value to append.</param> |
| | | 120 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 121 | | /// <param name="format">The format string to apply.</param> |
| | | 122 | | public void AppendFormatted(string? value, int alignment, string? format) |
| | | 123 | | { |
| | 0 | 124 | | _message.AppendFormatted(value, alignment, format); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Appends a character span. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="value">The span to append.</param> |
| | | 131 | | public void AppendFormatted(ReadOnlySpan<char> value) |
| | | 132 | | { |
| | 0 | 133 | | _message.AppendFormatted(value); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Appends a character span with the specified alignment. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="value">The span to append.</param> |
| | | 140 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 141 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment) |
| | | 142 | | { |
| | 0 | 143 | | _message.AppendFormatted(value, alignment); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Appends a character span with the specified alignment and format string. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="value">The span to append.</param> |
| | | 150 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 151 | | /// <param name="format">The format string to apply.</param> |
| | | 152 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format) |
| | | 153 | | { |
| | 0 | 154 | | _message.AppendFormatted(value, alignment, format); |
| | 0 | 155 | | } |
| | | 156 | | } |