using System.Collections; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization; namespace TimePasses.Model; [Serializable] public class ReplacementText : IReadOnlyList { private IReadOnlyList List { get; } internal ReplacementText(IReadOnlyList list) { this.List = list; } public string this[int index] => this.List[index]; public int Count => this.List.Count; public IEnumerator GetEnumerator() { return this.List.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.List.GetEnumerator(); } } internal class ReplacementTextConverter : IYamlTypeConverter { internal static readonly ReplacementTextConverter Instance = new(); public bool Accepts(Type type) { return type == typeof(ReplacementText); } public object? ReadYaml(IParser parser, Type type) { var list = new List(); if (parser.Current is Scalar) { AddItem(); } else if (parser.Current is SequenceStart) { parser.Consume(); while (parser.Current is not SequenceEnd) { AddItem(); } parser.Consume(); } else { throw new YamlException("expected string or list of strings"); } return new ReplacementText(list); void AddItem() { var scalar = parser.Consume(); if (scalar.IsKey) { throw new YamlException("invalid key found: expected string or list of strings"); } list.Add(scalar.Value); } } public void WriteYaml(IEmitter emitter, object? value, Type type) { throw new NotImplementedException(); } }