| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>text-transform</c> value within the Allyaria theming system. Provides a strongly typed wrapper f |
| | | 5 | | /// defining text casing transformations applied to content. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleTextTransform : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleTextTransform" /> record using the specified <see cref="Kind" |
| | | 11 | | /// value. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="kind">The text transformation behavior to represent.</param> |
| | | 14 | | public StyleTextTransform(Kind kind) |
| | 32 | 15 | | : base(value: kind.GetDescription()) { } |
| | | 16 | | |
| | | 17 | | /// <summary>Defines the supported CSS <c>text-transform</c> property values.</summary> |
| | | 18 | | public enum Kind |
| | | 19 | | { |
| | | 20 | | /// <summary>Transforms the first character of each word to uppercase while leaving the rest unchanged.</summary |
| | | 21 | | [Description(description: "capitalize")] |
| | | 22 | | Capitalize, |
| | | 23 | | |
| | | 24 | | /// <summary>Transforms all characters to lowercase.</summary> |
| | | 25 | | [Description(description: "lowercase")] |
| | | 26 | | Lowercase, |
| | | 27 | | |
| | | 28 | | /// <summary>Prevents any case transformation; text appears as originally authored.</summary> |
| | | 29 | | [Description(description: "none")] |
| | | 30 | | None, |
| | | 31 | | |
| | | 32 | | /// <summary>Transforms all characters to uppercase.</summary> |
| | | 33 | | [Description(description: "uppercase")] |
| | | 34 | | Uppercase |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Parses a string representation of a CSS <c>text-transform</c> value into a <see cref="StyleTextTransform" /> ins |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="value">The string representation of the text-transform value.</param> |
| | | 41 | | /// <returns>A new <see cref="StyleTextTransform" /> instance representing the parsed value.</returns> |
| | | 42 | | /// <exception cref="AryArgumentException"> |
| | | 43 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 44 | | /// </exception> |
| | | 45 | | public static StyleTextTransform Parse(string? value) |
| | 11 | 46 | | => value.TryParseEnum<Kind>(result: out var kind) |
| | 11 | 47 | | ? new StyleTextTransform(kind: kind) |
| | 11 | 48 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 49 | | |
| | | 50 | | /// <summary>Attempts to parse a string into a <see cref="StyleTextTransform" /> instance.</summary> |
| | | 51 | | /// <param name="value">The string representation of the text-transform value to parse.</param> |
| | | 52 | | /// <param name="result"> |
| | | 53 | | /// When this method returns, contains the parsed <see cref="StyleTextTransform" /> instance or <see langword="null" |
| | | 54 | | /// parsing failed. |
| | | 55 | | /// </param> |
| | | 56 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 57 | | public static bool TryParse(string? value, out StyleTextTransform? result) |
| | | 58 | | { |
| | | 59 | | try |
| | | 60 | | { |
| | 3 | 61 | | result = Parse(value: value); |
| | | 62 | | |
| | 1 | 63 | | return true; |
| | | 64 | | } |
| | 2 | 65 | | catch |
| | | 66 | | { |
| | 2 | 67 | | result = null; |
| | | 68 | | |
| | 2 | 69 | | return false; |
| | | 70 | | } |
| | 3 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary>Implicitly converts a string into a <see cref="StyleTextTransform" /> instance.</summary> |
| | | 74 | | /// <param name="value">The string representation of the text-transform value.</param> |
| | | 75 | | /// <returns>A <see cref="StyleTextTransform" /> instance representing the provided value.</returns> |
| | | 76 | | /// <exception cref="AryArgumentException"> |
| | | 77 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 78 | | /// <see cref="Kind" />. |
| | | 79 | | /// </exception> |
| | 2 | 80 | | public static implicit operator StyleTextTransform(string? value) => Parse(value: value); |
| | | 81 | | |
| | | 82 | | /// <summary>Implicitly converts a <see cref="StyleTextTransform" /> instance to its string representation.</summary |
| | | 83 | | /// <param name="value">The <see cref="StyleTextTransform" /> instance to convert.</param> |
| | | 84 | | /// <returns> |
| | | 85 | | /// The underlying CSS <c>text-transform</c> string value, or an empty string if <paramref name="value" /> is |
| | | 86 | | /// <see langword="null" />. |
| | | 87 | | /// </returns> |
| | 2 | 88 | | public static implicit operator string(StyleTextTransform? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 89 | | } |