TimePasses/Model/ReplacementText.cs

67 lines
1.8 KiB
C#

using System.Collections;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace TimePasses.Model;
[Serializable]
public class ReplacementText : IReadOnlyList<string> {
private IReadOnlyList<string> List { get; }
internal ReplacementText(IReadOnlyList<string> list) {
this.List = list;
}
public string this[int index] => this.List[index];
public int Count => this.List.Count;
public IEnumerator<string> 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<string>();
if (parser.Current is Scalar) {
AddItem();
} else if (parser.Current is SequenceStart) {
parser.Consume<SequenceStart>();
while (parser.Current is not SequenceEnd) {
AddItem();
}
parser.Consume<SequenceEnd>();
} else {
throw new YamlException("expected string or list of strings");
}
return new ReplacementText(list);
void AddItem() {
var scalar = parser.Consume<Scalar>();
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();
}
}