Convert some classes to use primary constructors

pull/1160/head
Tyrrrz 11 months ago
parent 743db67f91
commit 73574ade48

@ -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,

@ -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<string> FormatMarkdownAsync(
string markdown,

@ -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<string> 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<string, string> _previousPathsByUrl = new(StringComparer.Ordinal);
public ExportAssetDownloader(string workingDirPath, bool reuse)
{
_workingDirPath = workingDirPath;
_reuse = reuse;
}
public async ValueTask<string> DownloadAsync(
string url,
CancellationToken cancellationToken = default

@ -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<Snowflake, Member?> _membersById = new();
private readonly Dictionary<Snowflake, Channel> _channelsById = new();
private readonly Dictionary<Snowflake, Role> _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();

@ -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,

@ -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<Message> _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

@ -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<string> FormatMarkdownAsync(
string markdown,

@ -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)

@ -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;

@ -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,

@ -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<string> FormatMarkdownAsync(
string markdown,

Loading…
Cancel
Save