-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-add support for line endings being configurable. (#960)
* Re-add support for line endings being configurable. closes #935 * Format file * Update Src/CSharpier.Cli/Options/CaseInsensitiveEnumConverter.cs Co-authored-by: Lasath Fernando <devel@lasath.org> --------- Co-authored-by: Lasath Fernando <devel@lasath.org>
- Loading branch information
1 parent
85c6a68
commit 2ad4e5d
Showing
7 changed files
with
103 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace CSharpier.Cli.Options; | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
internal class CaseInsensitiveEnumConverter<TEnum> : JsonConverter<TEnum> | ||
where TEnum : struct | ||
{ | ||
public override TEnum Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
if (reader.TokenType != JsonTokenType.String) | ||
{ | ||
throw new JsonException( | ||
$"Expected a string for enum parsing, but got {reader.TokenType}." | ||
); | ||
} | ||
|
||
var enumText = reader.GetString(); | ||
if (Enum.TryParse(enumText, ignoreCase: true, out TEnum result)) | ||
{ | ||
return result; | ||
} | ||
|
||
throw new JsonException($"Unable to parse '{enumText}' to enum of type {typeof(TEnum)}."); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) | ||
{ | ||
var enumText = value.ToString(); | ||
writer.WriteStringValue(enumText); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters