| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a numeric CSS value (integer-based) used within the Allyaria theming system. Provides a strongly typed |
| | | 5 | | /// wrapper for integer-only CSS properties and validates numeric input upon construction. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleNumber : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleNumber" /> record using the specified string representation of |
| | | 11 | | /// numeric value. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="value">The string representation of a numeric value.</param> |
| | | 14 | | /// <exception cref="AryArgumentException"> |
| | | 15 | | /// Thrown when the provided <paramref name="value" /> cannot be parsed as a valid |
| | | 16 | | /// integer. |
| | | 17 | | /// </exception> |
| | | 18 | | public StyleNumber(string value) |
| | 15 | 19 | | : base(value: value) |
| | | 20 | | { |
| | 15 | 21 | | if (string.IsNullOrWhiteSpace(value: Value)) |
| | | 22 | | { |
| | 4 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 11 | 26 | | AryGuard.Check( |
| | 11 | 27 | | condition: TryNormalizeInteger(input: Value, number: out var number), |
| | 11 | 28 | | argName: nameof(value), |
| | 11 | 29 | | message: $"Invalid number: {Value}" |
| | 11 | 30 | | ); |
| | | 31 | | |
| | 7 | 32 | | Number = number; |
| | 7 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Gets the parsed integer value represented by this style number.</summary> |
| | 10 | 36 | | public int Number { get; } |
| | | 37 | | |
| | | 38 | | /// <summary>Parses the specified string into a <see cref="StyleNumber" /> instance.</summary> |
| | | 39 | | /// <param name="value">The string representation of the numeric value to parse.</param> |
| | | 40 | | /// <returns>A new <see cref="StyleNumber" /> instance representing the parsed value.</returns> |
| | | 41 | | /// <exception cref="AryArgumentException"> |
| | | 42 | | /// Thrown when the provided <paramref name="value" /> cannot be parsed as a valid |
| | | 43 | | /// integer. |
| | | 44 | | /// </exception> |
| | 8 | 45 | | public static StyleNumber Parse(string? value) => new(value: value ?? string.Empty); |
| | | 46 | | |
| | | 47 | | /// <summary>Attempts to normalize and parse an integer value from the specified input string.</summary> |
| | | 48 | | /// <param name="input">The string to parse.</param> |
| | | 49 | | /// <param name="number">When this method returns, contains the parsed integer if successful; otherwise, <c>0</c>.</ |
| | | 50 | | /// <returns> |
| | | 51 | | /// <see langword="true" /> if parsing succeeded or the input is empty/whitespace; otherwise, <see langword="false" |
| | | 52 | | /// </returns> |
| | | 53 | | private static bool TryNormalizeInteger(string input, out int number) |
| | | 54 | | { |
| | 11 | 55 | | number = 0; |
| | | 56 | | |
| | 11 | 57 | | return string.IsNullOrWhiteSpace(value: input) || int.TryParse( |
| | 11 | 58 | | s: input, |
| | 11 | 59 | | style: NumberStyles.Integer, |
| | 11 | 60 | | provider: CultureInfo.InvariantCulture, |
| | 11 | 61 | | result: out number |
| | 11 | 62 | | ); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Attempts to parse a string into a <see cref="StyleNumber" /> instance.</summary> |
| | | 66 | | /// <param name="value">The string representation of the numeric value to parse.</param> |
| | | 67 | | /// <param name="result"> |
| | | 68 | | /// When this method returns, contains the parsed <see cref="StyleNumber" /> instance or <see langword="null" /> if |
| | | 69 | | /// failed. |
| | | 70 | | /// </param> |
| | | 71 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 72 | | public static bool TryParse(string? value, out StyleNumber? result) |
| | | 73 | | { |
| | | 74 | | try |
| | | 75 | | { |
| | 3 | 76 | | result = Parse(value: value); |
| | | 77 | | |
| | 2 | 78 | | return true; |
| | | 79 | | } |
| | 1 | 80 | | catch |
| | | 81 | | { |
| | 1 | 82 | | result = null; |
| | | 83 | | |
| | 1 | 84 | | return false; |
| | | 85 | | } |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary>Implicitly converts a string into a <see cref="StyleNumber" /> instance.</summary> |
| | | 89 | | /// <param name="value">The string representation of the numeric value to convert.</param> |
| | | 90 | | /// <returns>A <see cref="StyleNumber" /> instance representing the provided value.</returns> |
| | | 91 | | /// <exception cref="ArgumentException">Thrown when the provided string cannot be parsed as a valid integer.</except |
| | 2 | 92 | | public static implicit operator StyleNumber(string? value) => Parse(value: value); |
| | | 93 | | |
| | | 94 | | /// <summary>Implicitly converts a <see cref="StyleNumber" /> instance to its string representation.</summary> |
| | | 95 | | /// <param name="value">The <see cref="StyleNumber" /> instance to convert.</param> |
| | | 96 | | /// <returns> |
| | | 97 | | /// The original string value represented by the <see cref="StyleNumber" />, or an empty string if |
| | | 98 | | /// <paramref name="value" /> is <see langword="null" />. |
| | | 99 | | /// </returns> |
| | 2 | 100 | | public static implicit operator string(StyleNumber? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 101 | | } |