| | | 1 | | namespace TeleFlow.Telegram.I18n.Fluent; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents one named value supplied to a Fluent message without dictionaries or anonymous-object reflection. |
| | | 5 | | /// </summary> |
| | | 6 | | public readonly record struct I18nArgument |
| | | 7 | | { |
| | | 8 | | public I18nArgument(string name, object? value) |
| | | 9 | | { |
| | 516 | 10 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 11 | | |
| | 516 | 12 | | if (!IsValidIdentifier(name)) |
| | | 13 | | { |
| | 0 | 14 | | throw new ArgumentException( |
| | 0 | 15 | | "Fluent argument names must start with an ASCII letter and contain only ASCII letters, digits, '-' or '_ |
| | 0 | 16 | | nameof(name)); |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | Name = name; |
| | | 20 | | Value = value; |
| | 516 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the Fluent variable name without the leading <c>$</c>. |
| | | 25 | | /// </summary> |
| | | 26 | | public string Name { get; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the application value converted by the Fluent adapter at formatting time. |
| | | 30 | | /// </summary> |
| | | 31 | | public object? Value { get; } |
| | | 32 | | |
| | | 33 | | public static implicit operator I18nArgument((string Name, object? Value) argument) |
| | | 34 | | { |
| | 516 | 35 | | return new I18nArgument(argument.Name, argument.Value); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private static bool IsValidIdentifier(string name) |
| | | 39 | | { |
| | 516 | 40 | | if (!char.IsAsciiLetter(name[0])) |
| | | 41 | | { |
| | 0 | 42 | | return false; |
| | | 43 | | } |
| | | 44 | | |
| | 5186 | 45 | | for (var index = 1; index < name.Length; index++) |
| | | 46 | | { |
| | 2077 | 47 | | var character = name[index]; |
| | | 48 | | |
| | 2077 | 49 | | if (!char.IsAsciiLetterOrDigit(character) && character is not '-' and not '_') |
| | | 50 | | { |
| | 0 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | 516 | 55 | | return true; |
| | | 56 | | } |
| | | 57 | | } |