Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring on Base64 Text Encoder / Decoder #907

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/app/dev/DevToys.Tools/Models/EncodingConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DevToys.Tools.Models;

internal enum EncodingConversion
{
Encode = 0,
Decode = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ namespace DevToys.Tools.Tools.EncodersDecoders.Base64Text;
DescriptionResourceName = nameof(Base64TextEncoderDecoder.Description))]
internal sealed class Base64TextEncoderDecoderCommandLineTool : ICommandLineTool
{
private enum Conversion
{
Encode,
Decode
}

[CommandLineOption(
Name = "input",
Alias = "i",
Expand All @@ -30,7 +24,7 @@ private enum Conversion
Name = "conversion",
Alias = "c",
DescriptionResourceName = nameof(Base64TextEncoderDecoder.ConversionOptionDescription))]
private Conversion ConversionMode { get; set; } = Conversion.Encode;
private EncodingConversion ConversionMode { get; set; } = EncodingConversion.Encode;

[CommandLineOption(
Name = "encoding",
Expand All @@ -41,20 +35,25 @@ private enum Conversion
public ValueTask<int> InvokeAsync(ILogger logger, CancellationToken cancellationToken)
{
string output;
if (ConversionMode == Conversion.Encode)
switch (ConversionMode)
{
output = Base64Helper.FromTextToBase64(Input, EncodingMode, logger, cancellationToken);
}
else
{
if (!Base64Helper.IsBase64DataStrict(Input))
{
cancellationToken.ThrowIfCancellationRequested();
Console.Error.WriteLine(Base64TextEncoderDecoder.InvalidBase64);
return new ValueTask<int>(-1);
}
case EncodingConversion.Encode:
output = Base64Helper.FromTextToBase64(Input, EncodingMode, logger, cancellationToken);
break;

case EncodingConversion.Decode:
if (!Base64Helper.IsBase64DataStrict(Input))
{
cancellationToken.ThrowIfCancellationRequested();
Console.Error.WriteLine(Base64TextEncoderDecoder.InvalidBase64);
return new ValueTask<int>(-1);
}

output = Base64Helper.FromBase64ToText(Input, EncodingMode, logger, cancellationToken);
break;

output = Base64Helper.FromBase64ToText(Input, EncodingMode, logger, cancellationToken);
default:
throw new NotSupportedException();
}

cancellationToken.ThrowIfCancellationRequested();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace DevToys.Tools.Tools.EncodersDecoders.Base64Text;
internal sealed partial class Base64TextEncoderDecoderGuiTool : IGuiTool, IDisposable
{
/// <summary>
/// Whether the tool should encode or decode Base64. True is encode, false is decode.
/// Whether the tool should encode or decode Base64.
/// </summary>
private static readonly SettingDefinition<bool> conversionMode
private static readonly SettingDefinition<EncodingConversion> conversionMode
= new(
name: $"{nameof(Base64TextEncoderDecoderGuiTool)}.{nameof(conversionMode)}",
defaultValue: true);
defaultValue: EncodingConversion.Encode);

/// <summary>
/// Whether the tool should encode/decode in Unicode or ASCII.
Expand Down Expand Up @@ -64,18 +64,21 @@ public Base64TextEncoderDecoderGuiTool(ISettingsProvider settingsProvider)
_logger = this.Log();
_settingsProvider = settingsProvider;

if (_settingsProvider.GetSetting(conversionMode))
switch (_settingsProvider.GetSetting(conversionMode))
{
// Encode
_conversionModeSwitch.On();
_inputText.AutoWrap();
_outputText.AlwaysWrap();
}
else
{
// Decode
_inputText.AlwaysWrap();
_outputText.AutoWrap();
case EncodingConversion.Encode:
_conversionModeSwitch.On();
_inputText.AutoWrap();
_outputText.AlwaysWrap();
break;

case EncodingConversion.Decode:
_inputText.AlwaysWrap();
_outputText.AutoWrap();
break;

default:
throw new NotSupportedException();
}
}

Expand Down Expand Up @@ -162,20 +165,23 @@ public void Dispose()

private void OnConversionModeChanged(bool conversionMode)
{
_settingsProvider.SetSetting(Base64TextEncoderDecoderGuiTool.conversionMode, conversionMode);
_settingsProvider.SetSetting(Base64TextEncoderDecoderGuiTool.conversionMode, conversionMode ? EncodingConversion.Encode : EncodingConversion.Decode);
_inputText.Text(_outputText.Text); // This will trigger a conversion.

if (conversionMode)
{
// Encode
_inputText.AutoWrap();
_outputText.AlwaysWrap();
}
else
switch (_settingsProvider.GetSetting(Base64TextEncoderDecoderGuiTool.conversionMode))
{
// Decode
_inputText.AlwaysWrap();
_outputText.AutoWrap();
case EncodingConversion.Encode:
_inputText.AutoWrap();
_outputText.AlwaysWrap();
break;

case EncodingConversion.Decode:
_inputText.AlwaysWrap();
_outputText.AutoWrap();
break;

default:
throw new NotSupportedException();
}
}

Expand Down Expand Up @@ -203,30 +209,36 @@ private async Task ConvertAsync(string input, CancellationToken cancellationToke
await TaskSchedulerAwaiter.SwitchOffMainThreadAsync(cancellationToken);

string conversionResult;
if (_settingsProvider.GetSetting(conversionMode))
{
conversionResult
= Base64Helper.FromTextToBase64(
input,
_settingsProvider.GetSetting(encoder),
_logger,
cancellationToken);
}
else

switch (_settingsProvider.GetSetting(conversionMode))
{
if (!string.IsNullOrEmpty(input) && !Base64Helper.IsBase64DataStrict(input))
{
cancellationToken.ThrowIfCancellationRequested();
_outputText.Text(Base64TextEncoderDecoder.InvalidBase64);
return;
}

conversionResult
= Base64Helper.FromBase64ToText(
input,
_settingsProvider.GetSetting(encoder),
_logger,
cancellationToken);
case EncodingConversion.Encode:
conversionResult
= Base64Helper.FromTextToBase64(
input,
_settingsProvider.GetSetting(encoder),
_logger,
cancellationToken);
break;

case EncodingConversion.Decode:
if (!string.IsNullOrEmpty(input) && !Base64Helper.IsBase64DataStrict(input))
{
cancellationToken.ThrowIfCancellationRequested();
_outputText.Text(Base64TextEncoderDecoder.InvalidBase64);
return;
}

conversionResult
= Base64Helper.FromBase64ToText(
input,
_settingsProvider.GetSetting(encoder),
_logger,
cancellationToken);
break;

default:
throw new NotSupportedException();
}

cancellationToken.ThrowIfCancellationRequested();
Expand Down