| | 1 | | using Allyaria.Theming.Contracts; |
| | 2 | |
|
| | 3 | | namespace Allyaria.Theming.Values; |
| | 4 | |
|
| | 5 | | /// <summary>Represents a theming string value with enforced normalization (trimmed, non-null, non-whitespace).</summary |
| | 6 | | public sealed class AllyariaStringValue : ValueBase |
| | 7 | | { |
| | 8 | | /// <summary>Initializes a new instance of the <see cref="AllyariaStringValue" /> class.</summary> |
| | 9 | | /// <param name="value">The string value to normalize and store.</param> |
| | 10 | | /// <exception cref="ArgumentException">Thrown when <paramref name="value" /> is <c>null</c>, empty, or whitespace.< |
| | 11 | | public AllyariaStringValue(string value) |
| 614 | 12 | | : base(Normalize(value)) { } |
| | 13 | |
|
| | 14 | | /// <summary>Normalizes the input string by trimming and validating it.</summary> |
| | 15 | | /// <param name="value">The string value to normalize.</param> |
| | 16 | | /// <returns>The normalized (trimmed) string.</returns> |
| | 17 | | /// <exception cref="ArgumentException"> |
| | 18 | | /// Thrown when <paramref name="value" /> is <c>null</c>, empty, whitespace, or contains control characters. |
| | 19 | | /// </exception> |
| 320 | 20 | | private static string Normalize(string value) => ValidateInput(value); |
| | 21 | |
|
| | 22 | | /// <summary>Parses the specified string into an <see cref="AllyariaStringValue" />.</summary> |
| | 23 | | /// <param name="value">The input string to parse.</param> |
| | 24 | | /// <returns>A new <see cref="AllyariaStringValue" /> containing the normalized <paramref name="value" />.</returns> |
| | 25 | | /// <exception cref="ArgumentException">Thrown when <paramref name="value" /> is <c>null</c>, empty, or whitespace.< |
| 10 | 26 | | public static AllyariaStringValue Parse(string value) => new(value); |
| | 27 | |
|
| | 28 | | /// <summary>Attempts to parse the specified string into an <see cref="AllyariaStringValue" />.</summary> |
| | 29 | | /// <param name="value">The input string to parse.</param> |
| | 30 | | /// <param name="result"> |
| | 31 | | /// When this method returns, contains the parsed <see cref="AllyariaStringValue" /> if parsing succeeded, or <c>nul |
| | 32 | | /// if it failed. |
| | 33 | | /// </param> |
| | 34 | | /// <returns><c>true</c> if parsing succeeded; otherwise, <c>false</c>.</returns> |
| | 35 | | public static bool TryParse(string value, out AllyariaStringValue? result) |
| | 36 | | { |
| | 37 | | try |
| | 38 | | { |
| 8 | 39 | | result = new AllyariaStringValue(value); |
| | 40 | |
|
| 2 | 41 | | return true; |
| | 42 | | } |
| 6 | 43 | | catch |
| | 44 | | { |
| 6 | 45 | | result = null; |
| | 46 | |
|
| 6 | 47 | | return false; |
| | 48 | | } |
| 8 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary>Defines an implicit conversion from <see cref="string" /> to <see cref="AllyariaStringValue" />.</summa |
| | 52 | | /// <param name="value">The string value to convert.</param> |
| | 53 | | /// <returns>A new <see cref="AllyariaStringValue" /> containing the normalized <paramref name="value" />.</returns> |
| | 54 | | /// <exception cref="ArgumentException">Thrown when <paramref name="value" /> is <c>null</c>, empty, or whitespace.< |
| 8 | 55 | | public static implicit operator AllyariaStringValue(string value) => new(value); |
| | 56 | |
|
| | 57 | | /// <summary>Defines an implicit conversion from <see cref="AllyariaStringValue" /> to <see cref="string" />.</summa |
| | 58 | | /// <param name="value">The <see cref="AllyariaStringValue" /> instance.</param> |
| | 59 | | /// <returns>The underlying normalized string value.</returns> |
| | 60 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="value" /> is <c>null</c>.</exception> |
| 2 | 61 | | public static implicit operator string(AllyariaStringValue value) => value.Value; |
| | 62 | | } |