using System.Globalization;
namespace Anubis.Tests;
///
/// The part of YAML the workflow of the CI uses, for the tests only: maps, lists, plain and quoted scalars, lists of
/// strings in brackets, block text (| and >-) and comments. Anything else is refused with the line number, so a
/// workflow written in a way this reader does not understand fails the test instead of passing unread.
/// Values are , or .
///
internal sealed class MiniYaml
{
private readonly List<(int Number, int Indent, string Text)> _lines = [];
private readonly string[] _raw;
private int _at;
private MiniYaml(string text)
{
_raw = text.ReplaceLineEndings("\n").Split('\n');
for (var i = 0; i < _raw.Length; i++)
{
var indent = _raw[i].Length - _raw[i].TrimStart(' ').Length;
var body = _raw[i].Trim(' ');
if (_raw[i][indent..].StartsWith('\t'))
{
throw Bad(i + 1, "tab");
}
_lines.Add((i + 1, indent, body));
}
}
public static Dictionary Read(string text)
{
var yaml = new MiniYaml(text);
yaml.SkipBlank();
var map = yaml.Map(0);
yaml.SkipBlank();
return yaml._at == yaml._lines.Count ? map : throw Bad(yaml._lines[yaml._at].Number, "unexpected indentation");
}
private static FormatException Bad(int line, string why) => new($"YAML line {line.ToString(CultureInfo.InvariantCulture)}: {why}.");
private void SkipBlank()
{
while (_at < _lines.Count && (_lines[_at].Text.Length == 0 || _lines[_at].Text.StartsWith('#')))
{
_at++;
}
}
private object Node(int indent, int line)
{
SkipBlank();
if (_at >= _lines.Count || _lines[_at].Indent < indent)
{
return "";
}
var found = _lines[_at];
return found.Text.StartsWith("- ", StringComparison.Ordinal) ? List(found.Indent) : found.Indent >= indent ? Map(found.Indent) : throw Bad(line, "indentation");
}
private List