| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ItemsFileProvider.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | using MyNet.Primitives.Providers; |
| | | 13 | | |
| | | 14 | | namespace 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> |
| | | 21 | | public abstract class ItemsFileProvider<T>(string? filename = null) : IValidatedItemsProvider<T> |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the source file path. |
| | | 25 | | /// </summary> |
| | 15 | 26 | | 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> |
| | 3 | 32 | | public void SetFilename(string? filename) => Filename = filename; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public async IAsyncEnumerable<T> GetItemsAsync([EnumeratorCancellation] CancellationToken cancellationToken = defaul |
| | | 36 | | { |
| | 9 | 37 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 38 | | |
| | 9 | 39 | | var filename = FileHelper.EnsureFileExists(Filename.OrEmpty()); |
| | | 40 | | |
| | 24 | 41 | | await foreach (var item in GetItemsCoreAsync(filename, cancellationToken).ConfigureAwait(false)) |
| | | 42 | | { |
| | 9 | 43 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 44 | | |
| | 9 | 45 | | yield return item; |
| | | 46 | | } |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public async Task<IReadOnlyList<ItemLoadError>> ValidateAsync(CancellationToken cancellationToken = default) |
| | | 51 | | { |
| | 3 | 52 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 53 | | |
| | 3 | 54 | | var filename = FileHelper.EnsureFileExists(Filename.OrEmpty()); |
| | | 55 | | |
| | 3 | 56 | | var errors = await ValidateCoreAsync(filename, cancellationToken).ConfigureAwait(false); |
| | | 57 | | |
| | 3 | 58 | | return errors.AsReadOnly(); |
| | 3 | 59 | | } |
| | | 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> |
| | 0 | 76 | | protected virtual Task<List<ItemLoadError>> ValidateCoreAsync(string filename, CancellationToken cancellationToken) |
| | | 77 | | } |
| | | 78 | | |