| | | 1 | | using ArturRios.Data.Export.Interfaces; |
| | | 2 | | using ArturRios.Output; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Data.Export.Exporters; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Base for exporters: handles null-guarding, envelope conversion, cancellation propagation, and |
| | | 8 | | /// file-stream lifetime. Concrete exporters implement <see cref="WriteCoreAsync" />. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="T">The record type.</typeparam> |
| | | 11 | | public abstract class ExporterBase<T> : IExporter<T> where T : class |
| | | 12 | | { |
| | | 13 | | /// <summary>Prefix for enveloped error messages.</summary> |
| | | 14 | | protected const string ExportFailedMessage = "An export error occurred:"; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public Task<ProcessOutput> WriteAsync(IEnumerable<T> data, Stream destination, CancellationToken ct = default) => |
| | 40 | 18 | | GuardedWriteAsync(data, destination, stream => WriteCoreAsync(data, stream, ct)); |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public Task<ProcessOutput> WriteToFileAsync(IEnumerable<T> data, string path, CancellationToken ct = default) => |
| | 2 | 22 | | GuardedFileAsync(data, path, stream => WriteCoreAsync(data, stream, ct)); |
| | | 23 | | |
| | | 24 | | /// <summary>Guards a stream write: null checks, envelope conversion, cancellation propagation.</summary> |
| | | 25 | | protected async Task<ProcessOutput> GuardedWriteAsync(IEnumerable<T> data, Stream destination, Func<Stream, Task> wr |
| | 23 | 26 | | { |
| | 25 | 27 | | if (data is null) return ProcessOutput.New.WithError($"{ExportFailedMessage} data is null."); |
| | 22 | 28 | | if (destination is null) return ProcessOutput.New.WithError($"{ExportFailedMessage} destination is null."); |
| | | 29 | | |
| | | 30 | | try |
| | 20 | 31 | | { |
| | 20 | 32 | | await write(destination); |
| | 18 | 33 | | return ProcessOutput.New; |
| | | 34 | | } |
| | 3 | 35 | | catch (OperationCanceledException) { throw; } |
| | 3 | 36 | | catch (Exception ex) { return ProcessOutput.New.WithError($"{ExportFailedMessage} {ex.GetBaseException().Message |
| | 22 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary>Guards a file write: opens/truncates the file, then delegates to <paramref name="write" />.</summary> |
| | | 40 | | protected async Task<ProcessOutput> GuardedFileAsync(IEnumerable<T> data, string path, Func<Stream, Task> write) |
| | 1 | 41 | | { |
| | 1 | 42 | | if (data is null) return ProcessOutput.New.WithError($"{ExportFailedMessage} data is null."); |
| | 1 | 43 | | if (string.IsNullOrEmpty(path)) return ProcessOutput.New.WithError($"{ExportFailedMessage} path is null or empty |
| | | 44 | | |
| | | 45 | | try |
| | 1 | 46 | | { |
| | 1 | 47 | | await using var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); |
| | 1 | 48 | | await write(stream); |
| | 1 | 49 | | return ProcessOutput.New; |
| | 0 | 50 | | } |
| | 0 | 51 | | catch (OperationCanceledException) { throw; } |
| | 0 | 52 | | catch (Exception ex) { return ProcessOutput.New.WithError($"{ExportFailedMessage} {ex.GetBaseException().Message |
| | 1 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary>Format-specific write. Implementations must honor <paramref name="ct" /> and not dispose the stream.</s |
| | | 56 | | protected abstract Task WriteCoreAsync(IEnumerable<T> data, Stream destination, CancellationToken ct); |
| | | 57 | | } |