Skip to content

Commit

Permalink
feat: generate code off internal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bmazzarol committed Jul 24, 2024
1 parent cb4c95a commit 8a8361f
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 465 deletions.
2 changes: 1 addition & 1 deletion Tuxedo.SourceGenerators/RefinementSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Tuxedo.SourceGenerators;

[Generator]
public class RefinementSourceGenerator : IIncrementalGenerator
public sealed class RefinementSourceGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand Down
15 changes: 3 additions & 12 deletions Tuxedo/Refinements/BoolRefinements.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
namespace Tuxedo;

/// <summary>
/// Boolean refinements
/// </summary>
public static class BoolRefinements
internal static class BoolRefinements
{
/// <summary>
/// Enforces that a boolean value is true
/// </summary>
[Refinement("The boolean value must be 'True', instead found '{value}'")]
public static bool True(bool value) => value;
internal static bool True(bool value) => value;

/// <summary>
/// Enforces that a boolean value is false
/// </summary>
[Refinement("The boolean value must be 'False', instead found '{value}'")]
public static bool False(bool value) => !value;
internal static bool False(bool value) => !value;
}
75 changes: 15 additions & 60 deletions Tuxedo/Refinements/CharRefinements.cs
Original file line number Diff line number Diff line change
@@ -1,91 +1,46 @@
namespace Tuxedo;

/// <summary>
/// Refinements for <see cref="char"/>
/// </summary>
public static class CharRefinements
internal static class CharRefinements
{
/// <summary>
/// Enforces that a character is a digit
/// </summary>
[Refinement("The character must be a letter, instead found '{value}'")]
public static bool Letter(char value) => char.IsLetter(value);
internal static bool Letter(char value) => char.IsLetter(value);

/// <summary>
/// Enforces that a character is a digit
/// </summary>
[Refinement("The character must be a digit, instead found '{value}'")]
public static bool Digit(char value) => char.IsDigit(value);
internal static bool Digit(char value) => char.IsDigit(value);

/// <summary>
/// Enforces that a character is lower case
/// </summary>
[Refinement("The character must be lower case, instead found '{value}'")]
public static bool Lower(char value) => char.IsLower(value);
internal static bool Lower(char value) => char.IsLower(value);

/// <summary>
/// Enforces that a character is upper case
/// </summary>
[Refinement("The character must be upper case, instead found '{value}'")]
public static bool Upper(char value) => char.IsUpper(value);
internal static bool Upper(char value) => char.IsUpper(value);

/// <summary>
/// Enforces that a character is a symbol
/// </summary>
[Refinement("The character must be a symbol, instead found '{value}'")]
public static bool Symbol(char value) => char.IsSymbol(value);
internal static bool Symbol(char value) => char.IsSymbol(value);

/// <summary>
/// Enforces that a character is a punctuation
/// </summary>
[Refinement("The character must be a punctuation, instead found '{value}'")]
public static bool Punctuation(char value) => char.IsPunctuation(value);
internal static bool Punctuation(char value) => char.IsPunctuation(value);

/// <summary>
/// Enforces that a character is a separator
/// </summary>
[Refinement("The character must be a separator, instead found '{value}'")]
public static bool Separator(char value) => char.IsSeparator(value);
internal static bool Separator(char value) => char.IsSeparator(value);

/// <summary>
/// Enforces that a character is a control
/// </summary>
[Refinement("The character must be a control, instead found '{value}'")]
public static bool Control(char value) => char.IsControl(value);
internal static bool Control(char value) => char.IsControl(value);

/// <summary>
/// Enforces that a character is a whitespace
/// </summary>
[Refinement("The character must be a whitespace, instead found '{value}'")]
public static bool Whitespace(char value) => char.IsWhiteSpace(value);
internal static bool Whitespace(char value) => char.IsWhiteSpace(value);

/// <summary>
/// Enforces that a character is a surrogate
/// </summary>
[Refinement("The character must be a surrogate, instead found '{value}'")]
public static bool Surrogate(char value) => char.IsSurrogate(value);
internal static bool Surrogate(char value) => char.IsSurrogate(value);

/// <summary>
/// Enforces that a character is a high surrogate
/// </summary>
[Refinement("The character must be a high surrogate, instead found '{value}'")]
public static bool HighSurrogate(char value) => char.IsHighSurrogate(value);
internal static bool HighSurrogate(char value) => char.IsHighSurrogate(value);

/// <summary>
/// Enforces that a character is a low surrogate
/// </summary>
[Refinement("The character must be a low surrogate, instead found '{value}'")]
public static bool LowSurrogate(char value) => char.IsLowSurrogate(value);
internal static bool LowSurrogate(char value) => char.IsLowSurrogate(value);

/// <summary>
/// Enforces that a character is a valid ASCII character
/// </summary>
[Refinement("The character must be a valid ASCII character, instead found '{value}'")]
public static bool Ascii(char value) => value < 128;
internal static bool Ascii(char value) => value < 128;

/// <summary>
/// Enforces that a character is a valid-extended ASCII character
/// </summary>
[Refinement("The character must be a valid extended ASCII character, instead found '{value}'")]
public static bool ExtendedAscii(char value) => value < 256;
internal static bool ExtendedAscii(char value) => value < 256;
}
60 changes: 12 additions & 48 deletions Tuxedo/Refinements/Numeric/BetweenRefinements.cs
Original file line number Diff line number Diff line change
@@ -1,127 +1,91 @@
namespace Tuxedo;

/// <summary>
/// Refinements to ensure that a numeric value is between two other numeric values
/// </summary>
public static class BetweenRefinements
internal static class BetweenRefinements
{
/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(sbyte value)
internal static bool Between<TMin, TMax>(sbyte value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(short value)
internal static bool Between<TMin, TMax>(short value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(int value)
internal static bool Between<TMin, TMax>(int value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(long value)
internal static bool Between<TMin, TMax>(long value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(byte value)
internal static bool Between<TMin, TMax>(byte value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(ushort value)
internal static bool Between<TMin, TMax>(ushort value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(uint value)
internal static bool Between<TMin, TMax>(uint value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(ulong value)
internal static bool Between<TMin, TMax>(ulong value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= (ulong)default(TMin).Value && value <= (ulong)default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(float value)
internal static bool Between<TMin, TMax>(float value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(double value)
internal static bool Between<TMin, TMax>(double value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;

/// <summary>
/// Ensures that a numeric value is between two other numeric values
/// </summary>
[Refinement(
"Number must be between {default(TMin).Value} and {default(TMax).Value}, but found {value}"
)]
public static bool Between<TMin, TMax>(decimal value)
internal static bool Between<TMin, TMax>(decimal value)
where TMin : struct, IConstant<TMin, long>
where TMax : struct, IConstant<TMax, long> =>
value >= default(TMin).Value && value <= default(TMax).Value;
Expand Down
45 changes: 9 additions & 36 deletions Tuxedo/Refinements/Numeric/EvenRefinements.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,28 @@
namespace Tuxedo;

/// <summary>
/// Refinements to ensure that a numeric value is even
/// </summary>
public static class EvenRefinements
internal static class EvenRefinements
{
/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(sbyte value) => value % 2 == 0;
internal static bool Even(sbyte value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(short value) => value % 2 == 0;
internal static bool Even(short value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(int value) => value % 2 == 0;
internal static bool Even(int value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(long value) => value % 2 == 0;
internal static bool Even(long value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(byte value) => value % 2 == 0;
internal static bool Even(byte value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(ushort value) => value % 2 == 0;
internal static bool Even(ushort value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(uint value) => value % 2 == 0;
internal static bool Even(uint value) => value % 2 == 0;

/// <summary>
/// Enforces that a numeric value is even
/// </summary>
[Refinement("Number must be an even number, but found {value}")]
public static bool Even(ulong value) => value % 2 == 0;
internal static bool Even(ulong value) => value % 2 == 0;
}
Loading

0 comments on commit 8a8361f

Please sign in to comment.