From 73574ade48a7e8f31dd3d24ead0f0b894daa6c15 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:57:02 +0200 Subject: [PATCH] Convert some classes to use primary constructors --- .../Exporting/ChannelExporter.cs | 6 ++-- .../Exporting/CsvMessageWriter.cs | 11 ++----- .../Exporting/ExportAssetDownloader.cs | 12 ++----- .../Exporting/ExportContext.cs | 19 +++-------- .../Exporting/HtmlMarkdownVisitor.cs | 19 +++++------ .../Exporting/HtmlMessageWriter.cs | 14 +++----- .../Exporting/JsonMessageWriter.cs | 33 ++++++++----------- .../Exporting/MessageExporter.cs | 9 ++--- .../Exporting/MessageWriter.cs | 12 ++----- .../Exporting/PlainTextMarkdownVisitor.cs | 13 +++----- .../Exporting/PlainTextMessageWriter.cs | 11 ++----- 11 files changed, 51 insertions(+), 108 deletions(-) diff --git a/DiscordChatExporter.Core/Exporting/ChannelExporter.cs b/DiscordChatExporter.Core/Exporting/ChannelExporter.cs index b4064fc..276ff7e 100644 --- a/DiscordChatExporter.Core/Exporting/ChannelExporter.cs +++ b/DiscordChatExporter.Core/Exporting/ChannelExporter.cs @@ -8,11 +8,9 @@ using Gress; namespace DiscordChatExporter.Core.Exporting; -public class ChannelExporter +public class ChannelExporter(DiscordClient discord) { - private readonly DiscordClient _discord; - - public ChannelExporter(DiscordClient discord) => _discord = discord; + private readonly DiscordClient _discord = discord; public async ValueTask ExportChannelAsync( ExportRequest request, diff --git a/DiscordChatExporter.Core/Exporting/CsvMessageWriter.cs b/DiscordChatExporter.Core/Exporting/CsvMessageWriter.cs index b0b6fb1..3d52477 100644 --- a/DiscordChatExporter.Core/Exporting/CsvMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/CsvMessageWriter.cs @@ -8,15 +8,10 @@ using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Core.Exporting; -internal partial class CsvMessageWriter : MessageWriter +internal partial class CsvMessageWriter(Stream stream, ExportContext context) + : MessageWriter(stream, context) { - private readonly TextWriter _writer; - - public CsvMessageWriter(Stream stream, ExportContext context) - : base(stream, context) - { - _writer = new StreamWriter(stream); - } + private readonly TextWriter _writer = new StreamWriter(stream); private async ValueTask FormatMarkdownAsync( string markdown, diff --git a/DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs b/DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs index 1bef119..075ce6b 100644 --- a/DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs +++ b/DiscordChatExporter.Core/Exporting/ExportAssetDownloader.cs @@ -14,7 +14,7 @@ using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Core.Exporting; -internal partial class ExportAssetDownloader +internal partial class ExportAssetDownloader(string workingDirPath, bool reuse) { private static readonly AsyncKeyedLocker Locker = new(o => @@ -23,18 +23,12 @@ internal partial class ExportAssetDownloader o.PoolInitialFill = 1; }); - private readonly string _workingDirPath; - private readonly bool _reuse; + private readonly string _workingDirPath = workingDirPath; + private readonly bool _reuse = reuse; // File paths of the previously downloaded assets private readonly Dictionary _previousPathsByUrl = new(StringComparer.Ordinal); - public ExportAssetDownloader(string workingDirPath, bool reuse) - { - _workingDirPath = workingDirPath; - _reuse = reuse; - } - public async ValueTask DownloadAsync( string url, CancellationToken cancellationToken = default diff --git a/DiscordChatExporter.Core/Exporting/ExportContext.cs b/DiscordChatExporter.Core/Exporting/ExportContext.cs index 626986b..224c5aa 100644 --- a/DiscordChatExporter.Core/Exporting/ExportContext.cs +++ b/DiscordChatExporter.Core/Exporting/ExportContext.cs @@ -12,27 +12,18 @@ using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Core.Exporting; -internal class ExportContext +internal class ExportContext(DiscordClient discord, ExportRequest request) { private readonly Dictionary _membersById = new(); private readonly Dictionary _channelsById = new(); private readonly Dictionary _rolesById = new(); - private readonly ExportAssetDownloader _assetDownloader; - public DiscordClient Discord { get; } + private readonly ExportAssetDownloader _assetDownloader = + new(request.AssetsDirPath, request.ShouldReuseAssets); - public ExportRequest Request { get; } + public DiscordClient Discord { get; } = discord; - public ExportContext(DiscordClient discord, ExportRequest request) - { - Discord = discord; - Request = request; - - _assetDownloader = new ExportAssetDownloader( - request.AssetsDirPath, - request.ShouldReuseAssets - ); - } + public ExportRequest Request { get; } = request; public DateTimeOffset NormalizeDate(DateTimeOffset instant) => Request.IsUtcNormalizationEnabled ? instant.ToUniversalTime() : instant.ToLocalTime(); diff --git a/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs index 4094b64..b37835b 100644 --- a/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/HtmlMarkdownVisitor.cs @@ -12,18 +12,15 @@ using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Core.Exporting; -internal partial class HtmlMarkdownVisitor : MarkdownVisitor +internal partial class HtmlMarkdownVisitor( + ExportContext context, + StringBuilder buffer, + bool isJumbo +) : MarkdownVisitor { - private readonly ExportContext _context; - private readonly StringBuilder _buffer; - private readonly bool _isJumbo; - - public HtmlMarkdownVisitor(ExportContext context, StringBuilder buffer, bool isJumbo) - { - _context = context; - _buffer = buffer; - _isJumbo = isJumbo; - } + private readonly ExportContext _context = context; + private readonly StringBuilder _buffer = buffer; + private readonly bool _isJumbo = isJumbo; protected override ValueTask VisitTextAsync( TextNode text, diff --git a/DiscordChatExporter.Core/Exporting/HtmlMessageWriter.cs b/DiscordChatExporter.Core/Exporting/HtmlMessageWriter.cs index e741c2f..a8a06d3 100644 --- a/DiscordChatExporter.Core/Exporting/HtmlMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/HtmlMessageWriter.cs @@ -9,21 +9,15 @@ using WebMarkupMin.Core; namespace DiscordChatExporter.Core.Exporting; -internal class HtmlMessageWriter : MessageWriter +internal class HtmlMessageWriter(Stream stream, ExportContext context, string themeName) + : MessageWriter(stream, context) { - private readonly TextWriter _writer; - private readonly string _themeName; + private readonly TextWriter _writer = new StreamWriter(stream); + private readonly string _themeName = themeName; private readonly HtmlMinifier _minifier = new(); private readonly List _messageGroup = new(); - public HtmlMessageWriter(Stream stream, ExportContext context, string themeName) - : base(stream, context) - { - _writer = new StreamWriter(stream); - _themeName = themeName; - } - private bool CanJoinGroup(Message message) { // If the group is empty, any message can join it diff --git a/DiscordChatExporter.Core/Exporting/JsonMessageWriter.cs b/DiscordChatExporter.Core/Exporting/JsonMessageWriter.cs index 693442e..6f7424e 100644 --- a/DiscordChatExporter.Core/Exporting/JsonMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/JsonMessageWriter.cs @@ -12,26 +12,21 @@ using JsonExtensions.Writing; namespace DiscordChatExporter.Core.Exporting; -internal class JsonMessageWriter : MessageWriter +internal class JsonMessageWriter(Stream stream, ExportContext context) + : MessageWriter(stream, context) { - private readonly Utf8JsonWriter _writer; - - public JsonMessageWriter(Stream stream, ExportContext context) - : base(stream, context) - { - _writer = new Utf8JsonWriter( - stream, - new JsonWriterOptions - { - // https://github.com/Tyrrrz/DiscordChatExporter/issues/450 - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - Indented = true, - // Validation errors may mask actual failures - // https://github.com/Tyrrrz/DiscordChatExporter/issues/413 - SkipValidation = true - } - ); - } + private readonly Utf8JsonWriter _writer = new Utf8JsonWriter( + stream, + new JsonWriterOptions + { + // https://github.com/Tyrrrz/DiscordChatExporter/issues/450 + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Indented = true, + // Validation errors may mask actual failures + // https://github.com/Tyrrrz/DiscordChatExporter/issues/413 + SkipValidation = true + } + ); private async ValueTask FormatMarkdownAsync( string markdown, diff --git a/DiscordChatExporter.Core/Exporting/MessageExporter.cs b/DiscordChatExporter.Core/Exporting/MessageExporter.cs index 0b178df..5bc47af 100644 --- a/DiscordChatExporter.Core/Exporting/MessageExporter.cs +++ b/DiscordChatExporter.Core/Exporting/MessageExporter.cs @@ -6,20 +6,15 @@ using DiscordChatExporter.Core.Discord.Data; namespace DiscordChatExporter.Core.Exporting; -internal partial class MessageExporter : IAsyncDisposable +internal partial class MessageExporter(ExportContext context) : IAsyncDisposable { - private readonly ExportContext _context; + private readonly ExportContext _context = context; private int _partitionIndex; private MessageWriter? _writer; public long MessagesExported { get; private set; } - public MessageExporter(ExportContext context) - { - _context = context; - } - private async ValueTask ResetWriterAsync(CancellationToken cancellationToken = default) { if (_writer is not null) diff --git a/DiscordChatExporter.Core/Exporting/MessageWriter.cs b/DiscordChatExporter.Core/Exporting/MessageWriter.cs index dd9c42f..8c0820d 100644 --- a/DiscordChatExporter.Core/Exporting/MessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/MessageWriter.cs @@ -6,22 +6,16 @@ using DiscordChatExporter.Core.Discord.Data; namespace DiscordChatExporter.Core.Exporting; -internal abstract class MessageWriter : IAsyncDisposable +internal abstract class MessageWriter(Stream stream, ExportContext context) : IAsyncDisposable { - protected Stream Stream { get; } + protected Stream Stream { get; } = stream; - protected ExportContext Context { get; } + protected ExportContext Context { get; } = context; public long MessagesWritten { get; private set; } public long BytesWritten => Stream.Length; - protected MessageWriter(Stream stream, ExportContext context) - { - Stream = stream; - Context = context; - } - public virtual ValueTask WritePreambleAsync(CancellationToken cancellationToken = default) => default; diff --git a/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs b/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs index 84f4135..86282ec 100644 --- a/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs +++ b/DiscordChatExporter.Core/Exporting/PlainTextMarkdownVisitor.cs @@ -8,16 +8,11 @@ using DiscordChatExporter.Core.Utils.Extensions; namespace DiscordChatExporter.Core.Exporting; -internal partial class PlainTextMarkdownVisitor : MarkdownVisitor +internal partial class PlainTextMarkdownVisitor(ExportContext context, StringBuilder buffer) + : MarkdownVisitor { - private readonly ExportContext _context; - private readonly StringBuilder _buffer; - - public PlainTextMarkdownVisitor(ExportContext context, StringBuilder buffer) - { - _context = context; - _buffer = buffer; - } + private readonly ExportContext _context = context; + private readonly StringBuilder _buffer = buffer; protected override ValueTask VisitTextAsync( TextNode text, diff --git a/DiscordChatExporter.Core/Exporting/PlainTextMessageWriter.cs b/DiscordChatExporter.Core/Exporting/PlainTextMessageWriter.cs index bdb3f15..38206ac 100644 --- a/DiscordChatExporter.Core/Exporting/PlainTextMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/PlainTextMessageWriter.cs @@ -8,15 +8,10 @@ using DiscordChatExporter.Core.Discord.Data.Embeds; namespace DiscordChatExporter.Core.Exporting; -internal class PlainTextMessageWriter : MessageWriter +internal class PlainTextMessageWriter(Stream stream, ExportContext context) + : MessageWriter(stream, context) { - private readonly TextWriter _writer; - - public PlainTextMessageWriter(Stream stream, ExportContext context) - : base(stream, context) - { - _writer = new StreamWriter(stream); - } + private readonly TextWriter _writer = new StreamWriter(stream); private async ValueTask FormatMarkdownAsync( string markdown,