Skip to content

Commit

Permalink
Support optional UTF-8 based contracts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Nov 28, 2023
1 parent a01c21f commit 1ecad04
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
40 changes: 40 additions & 0 deletions src/Qowaiv.CodeGeneration.SingleValueObjects/Snippets/Utf8.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#if !NotUtf8 // exec

#if NET8_0_OR_GREATER
public partial struct @TSvo
: IUtf8SpanParsable<@TSvo>
{
/// <summary>Converts the <see cref="string"/> to <see cref="@TSvo"/>.</summary>
/// <param name="utf8Text">
/// An UTF-8 string containing the @FullName to convert.
/// </param>
/// <param name="provider">
/// The specified format provider.
/// </param>
/// <returns>
/// The parsed @FullName.
/// </returns>
/// <exception cref="FormatException">
/// <paramref name="utf8Text"/> is not in the correct format.
/// </exception>
[Pure]
public static @TSvo Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider)
=> TryParse(utf8Text, provider)
?? throw Unparsable.ForValue<@TSvo>(utf8Text.ToString(), @FormatExceptionMessage);

/// <summary>Converts the <see cref="string"/> to <see cref="@TSvo"/>.</summary>
/// <param name="utf8Text">
/// An UTF-8 string containing the @FullName to convert.
/// </param>
/// <param name="provider">
/// The specified format provider.
/// </param>
/// <returns>
/// The @FullName if the string was converted successfully, otherwise default.
/// </returns>
[Pure]
public static @TSvo? TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider)
=> TryParse(utf8Text, provider, out var val) ? val : default(@TSvo?);
}
#endif
#endif // exec
35 changes: 18 additions & 17 deletions src/Qowaiv.CodeGeneration.SingleValueObjects/SvoFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
[Flags]
public enum SvoFeatures
{
Field = /* */ 0x00001,
GetHashCode = /* */ 0x00002,
IsEmpty = /* */ 0x00004,
IsUnknown = /* */ 0x00008,
IEquatable = /* */ 0x00010,
EqualsSvo = /* */ 0x00020,
ISerializable = /* */ 0x00080,
IXmlSerializable = /* */ 0x00100,
IJsonSerializable = /* */ 0x00200,
IFormattable = /* */ 0x00400,
IComparable = /* */ 0x00800,
ComparisonOperators = /**/ 0x01000,
Parsing = /* */ 0x02000,
Validation = /* */ 0x04000,
All = /* */ 0x07FFF,
Field = /*...............*/ 0x00001,
GetHashCode = /*.........*/ 0x00002,
IsEmpty = /*.............*/ 0x00004,
IsUnknown = /*...........*/ 0x00008,
IEquatable = /*..........*/ 0x00010,
EqualsSvo = /*..........*/ 0x00020,
ISerializable = /*.......*/ 0x00080,
IXmlSerializable = /*....*/ 0x00100,
IJsonSerializable = /*...*/ 0x00200,
IFormattable = /*........*/ 0x00400,
IComparable = /*.........*/ 0x00800,
ComparisonOperators = /*.*/ 0x01000,
Parsing = /*.............*/ 0x02000,
Validation = /*.........*/ 0x04000,
Utf8 = /*................*/ 0x08000,
All = /*.................*/ 0x0FFFF,
Structure = Field | IsEmpty | IsUnknown,
EqualsSvoAndGetHashCode = EqualsSvo | GetHashCode,
IsEmptyOrUnknown = IsEmpty | IsUnknown,
Default = All ^ ComparisonOperators,
Continuous = All ^ IsEmpty ^ IsUnknown,
Default = All ^ ComparisonOperators ^ Utf8,
Continuous = All ^ IsEmpty ^ IsUnknown ^ Utf8,
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static IReadOnlyCollection<Constant> Constants(SvoFeatures features)
Embedded("IXmlSerializable"),
Embedded("Parsing"),
Embedded("Validation"),
Embedded("Utf8"),
}.Concat());

public static SvoTemplate Initial { get; } = new SvoTemplate(Embedded(nameof(Initial)));
Expand Down

0 comments on commit 1ecad04

Please sign in to comment.