< Summary

Information
Class: MyNet.IO.ItemsFileProvider<T>
Assembly: MyNet.IO
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/ItemsFileProvider.cs
Tag: 323_28699572109
Line coverage
92%
Covered lines: 13
Uncovered lines: 1
Coverable lines: 14
Total lines: 78
Line coverage: 92.8%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SetFilename(...)100%11100%
GetItemsAsync()100%22100%
ValidateAsync()100%11100%
ValidateCoreAsync(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/ItemsFileProvider.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ItemsFileProvider.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Collections.Generic;
 8using System.Runtime.CompilerServices;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using MyNet.Primitives;
 12using MyNet.Primitives.Providers;
 13
 14namespace MyNet.IO;
 15
 16/// <summary>
 17/// Base class for items providers that read items from a file.
 18/// </summary>
 19/// <param name="filename">The file path.</param>
 20/// <typeparam name="T">The type of items.</typeparam>
 21public abstract class ItemsFileProvider<T>(string? filename = null) : IValidatedItemsProvider<T>
 22{
 23    /// <summary>
 24    /// Gets the source file path.
 25    /// </summary>
 1526    public string? Filename { get; private set; } = filename;
 27
 28    /// <summary>
 29    /// Sets the source file path.
 30    /// </summary>
 31    /// <param name="filename">The file path.</param>
 332    public void SetFilename(string? filename) => Filename = filename;
 33
 34    /// <inheritdoc/>
 35    public async IAsyncEnumerable<T> GetItemsAsync([EnumeratorCancellation] CancellationToken cancellationToken = defaul
 36    {
 937        cancellationToken.ThrowIfCancellationRequested();
 38
 939        var filename = FileHelper.EnsureFileExists(Filename.OrEmpty());
 40
 2441        await foreach (var item in GetItemsCoreAsync(filename, cancellationToken).ConfigureAwait(false))
 42        {
 943            cancellationToken.ThrowIfCancellationRequested();
 44
 945            yield return item;
 46        }
 347    }
 48
 49    /// <inheritdoc />
 50    public async Task<IReadOnlyList<ItemLoadError>> ValidateAsync(CancellationToken cancellationToken = default)
 51    {
 352        cancellationToken.ThrowIfCancellationRequested();
 53
 354        var filename = FileHelper.EnsureFileExists(Filename.OrEmpty());
 55
 356        var errors = await ValidateCoreAsync(filename, cancellationToken).ConfigureAwait(false);
 57
 358        return errors.AsReadOnly();
 359    }
 60
 61    /// <summary>
 62    /// When implemented in a derived class, retrieves items from the specified file path.
 63    /// </summary>
 64    /// <param name="filename">The file path.</param>
 65    /// <param name="cancellationToken">A cancellation token.</param>
 66    /// <returns>An asynchronous stream of items.</returns>
 67    protected abstract IAsyncEnumerable<T> GetItemsCoreAsync(string filename, CancellationToken cancellationToken);
 68
 69    /// <summary>
 70    /// Validates the specified file and returns discovered errors.
 71    /// </summary>
 72    /// <param name="filename">The source filename.</param>
 73    /// <param name="cancellationToken">
 74    /// A token used to cancel the operation.
 75    /// </param>
 076    protected virtual Task<List<ItemLoadError>> ValidateCoreAsync(string filename, CancellationToken cancellationToken) 
 77}
 78