2024-12-03 22:14:49 -05:00

29 lines
801 B
C#

namespace AdventOfCode2024.Input.Parsers;
public abstract class TextLineParser<U> : IParser<IList<U>> where U : notnull
{
/// <summary>
/// Parses a line to the given Entity type
/// </summary>
/// <param name="line">Line in the form of a string</param>
/// <returns>The parsed entity</returns>
protected abstract U ParseLine(string line);
/// <inheritdoc />
public async Task<IList<U>> ParseAsync(Stream input)
{
using StreamReader reader = new StreamReader(input);
List<U> entities = [];
while (!reader.EndOfStream)
{
string? line = await reader.ReadLineAsync();
if (line != null)
{
entities.Add(ParseLine(line));
}
}
return entities;
}
}