< Summary

Information
Class: ArturRios.Util.IO.FileReaderAsync
Assembly: ArturRios.Util
File(s): /home/runner/work/dotnet-util/dotnet-util/src/IO/FileReaderAsync.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 103
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 4
Fully covered methods: 4
Total methods: 4
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReadAsync()100%11100%
ReadAsDictionaryAsync()100%11100%
ReadLinesAsync()100%11100%
ReadAndDeserializeAsync()100%11100%

File(s)

/home/runner/work/dotnet-util/dotnet-util/src/IO/FileReaderAsync.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace ArturRios.Util.IO;
 4
 5/// <summary>
 6/// Provides asynchronous helper methods for reading and deserializing file contents.
 7/// </summary>
 8/// <remarks>
 9/// All methods validate the provided path and throw <see cref="ArgumentException"/> for null/whitespace and <see cref="
 10/// Behavior matches <see cref="FileReader"/> exactly; only the I/O is asynchronous.
 11/// </remarks>
 12public static class FileReaderAsync
 13{
 14    /// <summary>
 15    /// Asynchronously reads the entire contents of a text file.
 16    /// </summary>
 17    /// <param name="path">Absolute or relative path to the file.</param>
 18    /// <param name="cancellationToken">Cancels the read.</param>
 19    /// <returns>File content as a string.</returns>
 20    /// <exception cref="ArgumentException">Path is null or whitespace.</exception>
 21    /// <exception cref="FileNotFoundException">File does not exist.</exception>
 22    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception>
 23    public static async Task<string> ReadAsync(string path, CancellationToken cancellationToken = default)
 24    {
 725        FileReader.ValidatePath(path);
 26
 327        return await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false);
 228    }
 29
 30    /// <summary>
 31    /// Asynchronously reads a delimited text file (e.g. CSV) mapping each header to its column values.
 32    /// </summary>
 33    /// <param name="path">Path to the file.</param>
 34    /// <param name="separator">Delimiter separating fields (e.g. <c>','</c>).</param>
 35    /// <param name="cancellationToken">Cancels the read.</param>
 36    /// <returns>A dictionary keyed by column header mapping to arrays of its values.</returns>
 37    /// <exception cref="ArgumentException">Path is null or whitespace.</exception>
 38    /// <exception cref="FileNotFoundException">File does not exist.</exception>
 39    /// <exception cref="InvalidOperationException">
 40    /// The file has fewer than two lines, or the header row repeats a name, which would silently drop a
 41    /// column from the result.
 42    /// </exception>
 43    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception>
 44    /// <remarks>
 45    /// This is a plain split on <paramref name="separator"/>, not an RFC 4180 parser: quoting is not
 46    /// interpreted, so a quoted field containing the separator or a line break is split like any other.
 47    /// Use a dedicated CSV library when the data can contain either. Rows shorter than the header are
 48    /// padded with empty strings, and values beyond the last header are discarded.
 49    /// </remarks>
 50    public static async Task<Dictionary<string, string[]>> ReadAsDictionaryAsync(
 51        string path,
 52        char separator,
 53        CancellationToken cancellationToken = default)
 54    {
 1655        FileReader.ValidatePath(path);
 56
 1257        var lines = await File.ReadAllLinesAsync(path, cancellationToken).ConfigureAwait(false);
 58
 1159        return DelimitedText.ToDictionary(lines, separator);
 860    }
 61
 62    /// <summary>
 63    /// Asynchronously reads all lines of a text file into an array.
 64    /// </summary>
 65    /// <param name="path">Path to the file.</param>
 66    /// <param name="cancellationToken">Cancels the read.</param>
 67    /// <returns>Array of lines.</returns>
 68    /// <exception cref="ArgumentException">Path is null or whitespace.</exception>
 69    /// <exception cref="FileNotFoundException">File does not exist.</exception>
 70    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception>
 71    public static async Task<string[]> ReadLinesAsync(string path, CancellationToken cancellationToken = default)
 72    {
 773        FileReader.ValidatePath(path);
 74
 375        return await File.ReadAllLinesAsync(path, cancellationToken).ConfigureAwait(false);
 276    }
 77
 78    /// <summary>
 79    /// Asynchronously reads a JSON file and deserializes its content into the specified type.
 80    /// </summary>
 81    /// <typeparam name="T">Target type.</typeparam>
 82    /// <param name="path">Path to the JSON file.</param>
 83    /// <param name="cancellationToken">Cancels the read and the deserialization.</param>
 84    /// <returns>
 85    /// The deserialized object, or <c>null</c> when the file contains the JSON literal <c>null</c>.
 86    /// </returns>
 87    /// <exception cref="ArgumentException">Path is null or whitespace.</exception>
 88    /// <exception cref="FileNotFoundException">File does not exist.</exception>
 89    /// <exception cref="JsonException">
 90    /// The file is empty, holds only whitespace, or does not contain JSON matching
 91    /// <typeparamref name="T"/>. An unreadable file is reported rather than silently turned into
 92    /// <c>null</c>, which would be indistinguishable from a genuine null payload.
 93    /// </exception>
 94    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception>
 95    public static async Task<T?> ReadAndDeserializeAsync<T>(string path, CancellationToken cancellationToken = default)
 96    {
 1197        FileReader.ValidatePath(path);
 98
 799        await using var stream = File.OpenRead(path);
 100
 7101        return await JsonSerializer.DeserializeAsync<T>(stream, cancellationToken: cancellationToken).ConfigureAwait(fal
 2102    }
 103}