< Summary

Information
Class: ArturRios.Util.IO.FileReaderAsync
Assembly: ArturRios.Util
File(s): /home/ci/agent/workspace/dotnet-util-docs_main/app/src/IO/FileReaderAsync.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 109
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/ci/agent/workspace/dotnet-util-docs_main/app/src/IO/FileReaderAsync.cs

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