|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Collections; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Text; |
| 8 | +using Elastic.Markdown.Helpers; |
| 9 | +using YamlDotNet.Serialization; |
| 10 | + |
| 11 | +namespace Elastic.Markdown.Myst.FrontMatter; |
| 12 | + |
| 13 | +[YamlSerializable] |
| 14 | +public record ApplicabilityOverTime : IReadOnlyCollection<Applicability> |
| 15 | +{ |
| 16 | + private readonly IReadOnlyCollection<Applicability> _items; |
| 17 | + public ApplicabilityOverTime(Applicability[] items) => _items = items; |
| 18 | + |
| 19 | + // <lifecycle> [version] |
| 20 | + public static bool TryParse(string? value, out ApplicabilityOverTime? availability) |
| 21 | + { |
| 22 | + availability = null; |
| 23 | + if (string.IsNullOrWhiteSpace(value) || string.Equals(value.Trim(), "all", StringComparison.InvariantCultureIgnoreCase)) |
| 24 | + { |
| 25 | + availability = GenerallyAvailable; |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + var items = value.Split(','); |
| 30 | + var applications = new List<Applicability>(items.Length); |
| 31 | + foreach (var item in items) |
| 32 | + { |
| 33 | + if (Applicability.TryParse(item.Trim(), out var a)) |
| 34 | + applications.Add(a); |
| 35 | + } |
| 36 | + |
| 37 | + if (applications.Count == 0) |
| 38 | + return false; |
| 39 | + |
| 40 | + availability = new ApplicabilityOverTime(applications.ToArray()); |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + public virtual bool Equals(ApplicabilityOverTime? other) |
| 45 | + { |
| 46 | + if ((object)this == other) |
| 47 | + return true; |
| 48 | + |
| 49 | + if ((object?)other is null || EqualityContract != other.EqualityContract) |
| 50 | + return false; |
| 51 | + |
| 52 | + var comparer = StructuralComparisons.StructuralEqualityComparer; |
| 53 | + return comparer.Equals(_items, other._items); |
| 54 | + } |
| 55 | + |
| 56 | + public override int GetHashCode() |
| 57 | + { |
| 58 | + var comparer = StructuralComparisons.StructuralEqualityComparer; |
| 59 | + return (EqualityComparer<Type>.Default.GetHashCode(EqualityContract) * -1521134295) |
| 60 | + + comparer.GetHashCode(_items); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + public static explicit operator ApplicabilityOverTime(string b) |
| 65 | + { |
| 66 | + var productAvailability = TryParse(b, out var version) ? version : null; |
| 67 | + return productAvailability ?? throw new ArgumentException($"'{b}' is not a valid applicability string array."); |
| 68 | + } |
| 69 | + |
| 70 | + public static ApplicabilityOverTime GenerallyAvailable { get; } |
| 71 | + = new([Applicability.GenerallyAvailable]); |
| 72 | + |
| 73 | + public override string ToString() |
| 74 | + { |
| 75 | + if (this == GenerallyAvailable) |
| 76 | + return "all"; |
| 77 | + var sb = new StringBuilder(); |
| 78 | + foreach (var item in _items) |
| 79 | + sb.Append(item).Append(", "); |
| 80 | + return sb.ToString(); |
| 81 | + } |
| 82 | + |
| 83 | + public IEnumerator<Applicability> GetEnumerator() => _items.GetEnumerator(); |
| 84 | + |
| 85 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 86 | + |
| 87 | + public int Count => _items.Count; |
| 88 | +} |
| 89 | + |
| 90 | +[YamlSerializable] |
| 91 | +public record Applicability |
| 92 | +{ |
| 93 | + public ProductLifecycle Lifecycle { get; init; } |
| 94 | + public SemVersion? Version { get; init; } |
| 95 | + |
| 96 | + public static Applicability GenerallyAvailable { get; } = new() |
| 97 | + { |
| 98 | + Lifecycle = ProductLifecycle.GenerallyAvailable, |
| 99 | + Version = AllVersions.Instance |
| 100 | + }; |
| 101 | + |
| 102 | + public override string ToString() |
| 103 | + { |
| 104 | + if (this == GenerallyAvailable) |
| 105 | + return "all"; |
| 106 | + var sb = new StringBuilder(); |
| 107 | + var lifecycle = Lifecycle switch |
| 108 | + { |
| 109 | + ProductLifecycle.TechnicalPreview => "preview", |
| 110 | + ProductLifecycle.Beta => "beta", |
| 111 | + ProductLifecycle.Development => "dev", |
| 112 | + ProductLifecycle.Deprecated => "deprecated", |
| 113 | + ProductLifecycle.Coming => "coming", |
| 114 | + ProductLifecycle.Discontinued => "discontinued", |
| 115 | + ProductLifecycle.Unavailable => "unavailable", |
| 116 | + ProductLifecycle.GenerallyAvailable => "ga", |
| 117 | + _ => throw new ArgumentOutOfRangeException() |
| 118 | + }; |
| 119 | + sb.Append(lifecycle); |
| 120 | + if (Version is not null && Version != AllVersions.Instance) |
| 121 | + sb.Append(" ").Append(Version); |
| 122 | + return sb.ToString(); |
| 123 | + } |
| 124 | + |
| 125 | + public static explicit operator Applicability(string b) |
| 126 | + { |
| 127 | + var productAvailability = TryParse(b, out var version) ? version : TryParse(b + ".0", out version) ? version : null; |
| 128 | + return productAvailability ?? throw new ArgumentException($"'{b}' is not a valid applicability string."); |
| 129 | + } |
| 130 | + |
| 131 | + // <lifecycle> [version] |
| 132 | + public static bool TryParse(string? value, [NotNullWhen(true)] out Applicability? availability) |
| 133 | + { |
| 134 | + if (string.IsNullOrWhiteSpace(value) || string.Equals(value.Trim(), "all", StringComparison.InvariantCultureIgnoreCase)) |
| 135 | + { |
| 136 | + availability = GenerallyAvailable; |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + var tokens = value.Split(" ", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); |
| 141 | + if (tokens.Length < 1) |
| 142 | + { |
| 143 | + availability = null; |
| 144 | + return false; |
| 145 | + } |
| 146 | + var lifecycle = tokens[0].ToLowerInvariant() switch |
| 147 | + { |
| 148 | + "preview" => ProductLifecycle.TechnicalPreview, |
| 149 | + "tech-preview" => ProductLifecycle.TechnicalPreview, |
| 150 | + "beta" => ProductLifecycle.Beta, |
| 151 | + "dev" => ProductLifecycle.Development, |
| 152 | + "development" => ProductLifecycle.Development, |
| 153 | + "deprecated" => ProductLifecycle.Deprecated, |
| 154 | + "coming" => ProductLifecycle.Coming, |
| 155 | + "discontinued" => ProductLifecycle.Discontinued, |
| 156 | + "unavailable" => ProductLifecycle.Unavailable, |
| 157 | + "ga" => ProductLifecycle.GenerallyAvailable, |
| 158 | + _ => throw new ArgumentOutOfRangeException(nameof(tokens), tokens, $"Unknown product lifecycle: {tokens[0]}") |
| 159 | + }; |
| 160 | + |
| 161 | + var version = tokens.Length < 2 ? null : tokens[1] switch |
| 162 | + { |
| 163 | + null => AllVersions.Instance, |
| 164 | + "all" => AllVersions.Instance, |
| 165 | + "" => AllVersions.Instance, |
| 166 | + var t => SemVersionConverter.TryParse(t, out var v) ? v : null |
| 167 | + }; |
| 168 | + availability = new Applicability { Version = version, Lifecycle = lifecycle }; |
| 169 | + return true; |
| 170 | + } |
| 171 | +} |
0 commit comments