| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a simple CSS string value within the Allyaria theming system. Provides a strongly typed wrapper for any |
| | | 5 | | /// arbitrary CSS-compatible string value, ensuring consistent handling and conversion across components. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleString : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary>Initializes a new instance of the <see cref="StyleString" /> record with an optional string value.</sum |
| | | 10 | | /// <param name="value">The string value to assign. If <see langword="null" />, an empty string is used.</param> |
| | | 11 | | public StyleString(string? value = "") |
| | 204 | 12 | | : base(value: value ?? string.Empty) { } |
| | | 13 | | |
| | | 14 | | /// <summary>Parses the specified string into a <see cref="StyleString" /> instance.</summary> |
| | | 15 | | /// <param name="value">The string representation of the style value.</param> |
| | | 16 | | /// <returns>A new <see cref="StyleString" /> instance representing the provided value.</returns> |
| | 7 | 17 | | public static StyleString Parse(string? value) => new(value: value); |
| | | 18 | | |
| | | 19 | | /// <summary>Attempts to parse a string into a <see cref="StyleString" /> instance.</summary> |
| | | 20 | | /// <param name="value">The string representation of the style value to parse.</param> |
| | | 21 | | /// <param name="result"> |
| | | 22 | | /// When this method returns, contains the parsed <see cref="StyleString" /> instance or <see langword="null" /> if |
| | | 23 | | /// failed. |
| | | 24 | | /// </param> |
| | | 25 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 26 | | public static bool TryParse(string? value, out StyleString? result) |
| | | 27 | | { |
| | | 28 | | try |
| | | 29 | | { |
| | 3 | 30 | | result = Parse(value: value); |
| | | 31 | | |
| | 2 | 32 | | return true; |
| | | 33 | | } |
| | 1 | 34 | | catch |
| | | 35 | | { |
| | 1 | 36 | | result = null; |
| | | 37 | | |
| | 1 | 38 | | return false; |
| | | 39 | | } |
| | 3 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary>Implicitly converts a string into a <see cref="StyleString" /> instance.</summary> |
| | | 43 | | /// <param name="value">The string value to convert.</param> |
| | | 44 | | /// <returns>A <see cref="StyleString" /> instance representing the provided string.</returns> |
| | 2 | 45 | | public static implicit operator StyleString(string? value) => Parse(value: value); |
| | | 46 | | |
| | | 47 | | /// <summary>Implicitly converts a <see cref="StyleString" /> instance to its underlying string representation.</sum |
| | | 48 | | /// <param name="value">The <see cref="StyleString" /> instance to convert.</param> |
| | | 49 | | /// <returns> |
| | | 50 | | /// The contained string value, or an empty string if <paramref name="value" /> is <see langword="null" />. |
| | | 51 | | /// </returns> |
| | 4 | 52 | | public static implicit operator string(StyleString? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 53 | | } |