diff --git a/Code/Light.GuardClauses.Performance/CollectionAssertions/SpanMustHaveLengthBenchmark.cs b/Code/Light.GuardClauses.Performance/CollectionAssertions/SpanMustHaveLengthBenchmark.cs index 947efbf..0398812 100644 --- a/Code/Light.GuardClauses.Performance/CollectionAssertions/SpanMustHaveLengthBenchmark.cs +++ b/Code/Light.GuardClauses.Performance/CollectionAssertions/SpanMustHaveLengthBenchmark.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Text; using BenchmarkDotNet.Attributes; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses.Performance.CollectionAssertions @@ -47,7 +48,7 @@ public static class SpanMustHaveLengthExtensions public static Span MustHaveLengthCopyByValue(this Span parameter, int length, string parameterName = null, string message = null) { if (parameter.Length != length) - Throw.InvalidSpanLength(parameter, length, parameterName, message); + Throw.InvalidSpanLength((ReadOnlySpan) parameter, length, parameterName, message); return parameter; } @@ -55,7 +56,7 @@ public static Span MustHaveLengthCopyByValue(this Span parameter, int l public static Span MustHaveLengthInParameter(in this Span parameter, int length, string parameterName = null, string message = null) { if (parameter.Length != length) - Throw.InvalidSpanLength(parameter, length, parameterName, message); + Throw.InvalidSpanLength((ReadOnlySpan) parameter, length, parameterName, message); return parameter; } @@ -63,7 +64,7 @@ public static Span MustHaveLengthInParameter(in this Span parameter, in public static ref Span MustHaveLengthInOut(ref this Span parameter, int length, string parameterName = null, string message = null) { if (parameter.Length != length) - Throw.InvalidSpanLength(parameter, length, parameterName, message); + Throw.InvalidSpanLength((ReadOnlySpan) parameter, length, parameterName, message); return ref parameter; } } diff --git a/Code/Light.GuardClauses.Performance/CommonAssertions/InvalidArgumentBenchmark.cs b/Code/Light.GuardClauses.Performance/CommonAssertions/InvalidArgumentBenchmark.cs index dbc6241..3e922c3 100644 --- a/Code/Light.GuardClauses.Performance/CommonAssertions/InvalidArgumentBenchmark.cs +++ b/Code/Light.GuardClauses.Performance/CommonAssertions/InvalidArgumentBenchmark.cs @@ -1,5 +1,6 @@ using System; using BenchmarkDotNet.Attributes; +using Light.GuardClauses.ExceptionFactory; namespace Light.GuardClauses.Performance.CommonAssertions { @@ -34,7 +35,7 @@ public bool LightGuardClausesCustomException() public bool LightGuardClausesCustomExceptionManualInlining() { if (Condition) - Exceptions.Throw.CustomException(() => new ArgumentException(Message, ParameterName)); + Throw.CustomException(() => new ArgumentException(Message, ParameterName)); return Condition; } diff --git a/Code/Light.GuardClauses.Performance/CommonAssertions/MustHaveValueBenchmark.cs b/Code/Light.GuardClauses.Performance/CommonAssertions/MustHaveValueBenchmark.cs index e42ec29..1458a09 100644 --- a/Code/Light.GuardClauses.Performance/CommonAssertions/MustHaveValueBenchmark.cs +++ b/Code/Light.GuardClauses.Performance/CommonAssertions/MustHaveValueBenchmark.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses.Performance.CommonAssertions diff --git a/Code/Light.GuardClauses.Performance/CommonAssertions/MustNotBeNullReferenceBenchmark.cs b/Code/Light.GuardClauses.Performance/CommonAssertions/MustNotBeNullReferenceBenchmark.cs index e6c2a8a..ea6aa9e 100644 --- a/Code/Light.GuardClauses.Performance/CommonAssertions/MustNotBeNullReferenceBenchmark.cs +++ b/Code/Light.GuardClauses.Performance/CommonAssertions/MustNotBeNullReferenceBenchmark.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; -using Light.GuardClauses.Exceptions; +using Light.GuardClauses.ExceptionFactory; namespace Light.GuardClauses.Performance.CommonAssertions { @@ -56,7 +56,7 @@ public static T MustNotBeNullReferenceV1(this T parameter, string parameterNa return parameter; Throw.ArgumentNull(parameterName, message); - return default(T); + return default; } return parameter; @@ -71,7 +71,7 @@ public static T MustNotBeNullReferenceV2(this T parameter, string parameterNa return parameter; Throw.ArgumentNull(parameterName, message); - return default(T); + return default; } } } \ No newline at end of file diff --git a/Code/Light.GuardClauses/Check.Equals.cs b/Code/Light.GuardClauses/Check.Equals.cs index 688e191..05f38e6 100644 --- a/Code/Light.GuardClauses/Check.Equals.cs +++ b/Code/Light.GuardClauses/Check.Equals.cs @@ -1,6 +1,6 @@ using System; using System.Runtime.CompilerServices; -using Light.GuardClauses.Exceptions; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.FrameworkExtensions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.InvalidArgument.cs b/Code/Light.GuardClauses/Check.InvalidArgument.cs index cc50e69..29bf0ec 100644 --- a/Code/Light.GuardClauses/Check.InvalidArgument.cs +++ b/Code/Light.GuardClauses/Check.InvalidArgument.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; -using Light.GuardClauses.Exceptions; +using Light.GuardClauses.ExceptionFactory; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.InvalidOperation.cs b/Code/Light.GuardClauses/Check.InvalidOperation.cs index c4ff5b7..e0b3b79 100644 --- a/Code/Light.GuardClauses/Check.InvalidOperation.cs +++ b/Code/Light.GuardClauses/Check.InvalidOperation.cs @@ -1,6 +1,6 @@ using System; using System.Runtime.CompilerServices; -using Light.GuardClauses.Exceptions; +using Light.GuardClauses.ExceptionFactory; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.InvalidState.cs b/Code/Light.GuardClauses/Check.InvalidState.cs index 1e89445..496ec78 100644 --- a/Code/Light.GuardClauses/Check.InvalidState.cs +++ b/Code/Light.GuardClauses/Check.InvalidState.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustBe.cs b/Code/Light.GuardClauses/Check.MustBe.cs index 722e499..99a7121 100644 --- a/Code/Light.GuardClauses/Check.MustBe.cs +++ b/Code/Light.GuardClauses/Check.MustBe.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustBeAbsoluteUri.cs b/Code/Light.GuardClauses/Check.MustBeAbsoluteUri.cs index 88415b0..b4d3f75 100644 --- a/Code/Light.GuardClauses/Check.MustBeAbsoluteUri.cs +++ b/Code/Light.GuardClauses/Check.MustBeAbsoluteUri.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeEmailAddress.cs b/Code/Light.GuardClauses/Check.MustBeEmailAddress.cs index ef2b33e..8cd52c7 100644 --- a/Code/Light.GuardClauses/Check.MustBeEmailAddress.cs +++ b/Code/Light.GuardClauses/Check.MustBeEmailAddress.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeFileExtension.cs b/Code/Light.GuardClauses/Check.MustBeFileExtension.cs index 1b3834f..ae24e8b 100644 --- a/Code/Light.GuardClauses/Check.MustBeFileExtension.cs +++ b/Code/Light.GuardClauses/Check.MustBeFileExtension.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeGreaterThan.cs b/Code/Light.GuardClauses/Check.MustBeGreaterThan.cs index c8b6234..2e66b68 100644 --- a/Code/Light.GuardClauses/Check.MustBeGreaterThan.cs +++ b/Code/Light.GuardClauses/Check.MustBeGreaterThan.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeGreaterThanOrEqualTo.cs b/Code/Light.GuardClauses/Check.MustBeGreaterThanOrEqualTo.cs index 90cd127..213f825 100644 --- a/Code/Light.GuardClauses/Check.MustBeGreaterThanOrEqualTo.cs +++ b/Code/Light.GuardClauses/Check.MustBeGreaterThanOrEqualTo.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeHttpOrHttpsUrl.cs b/Code/Light.GuardClauses/Check.MustBeHttpOrHttpsUrl.cs index a431469..cbcea62 100644 --- a/Code/Light.GuardClauses/Check.MustBeHttpOrHttpsUrl.cs +++ b/Code/Light.GuardClauses/Check.MustBeHttpOrHttpsUrl.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeIn.cs b/Code/Light.GuardClauses/Check.MustBeIn.cs index b727d36..061c236 100644 --- a/Code/Light.GuardClauses/Check.MustBeIn.cs +++ b/Code/Light.GuardClauses/Check.MustBeIn.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeLessThan.cs b/Code/Light.GuardClauses/Check.MustBeLessThan.cs index 21faebd..7667730 100644 --- a/Code/Light.GuardClauses/Check.MustBeLessThan.cs +++ b/Code/Light.GuardClauses/Check.MustBeLessThan.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeLessThanOrEqualTo.cs b/Code/Light.GuardClauses/Check.MustBeLessThanOrEqualTo.cs index 9e813df..5ac96d3 100644 --- a/Code/Light.GuardClauses/Check.MustBeLessThanOrEqualTo.cs +++ b/Code/Light.GuardClauses/Check.MustBeLessThanOrEqualTo.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeLocal.cs b/Code/Light.GuardClauses/Check.MustBeLocal.cs index 5a41690..6f2c31b 100644 --- a/Code/Light.GuardClauses/Check.MustBeLocal.cs +++ b/Code/Light.GuardClauses/Check.MustBeLocal.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustBeLongerThan.cs b/Code/Light.GuardClauses/Check.MustBeLongerThan.cs index c884530..7b6cc14 100644 --- a/Code/Light.GuardClauses/Check.MustBeLongerThan.cs +++ b/Code/Light.GuardClauses/Check.MustBeLongerThan.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; @@ -73,11 +74,7 @@ public static Span MustBeLongerThan( string? message = null ) { - if (parameter.Length <= length) - { - Throw.SpanMustBeLongerThan(parameter, length, parameterName, message); - } - + ((ReadOnlySpan) parameter).MustBeLongerThan(length, parameterName, message); return parameter; } diff --git a/Code/Light.GuardClauses/Check.MustBeLongerThanOrEqualTo.cs b/Code/Light.GuardClauses/Check.MustBeLongerThanOrEqualTo.cs index f2e98ec..f85231b 100644 --- a/Code/Light.GuardClauses/Check.MustBeLongerThanOrEqualTo.cs +++ b/Code/Light.GuardClauses/Check.MustBeLongerThanOrEqualTo.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; @@ -73,11 +74,7 @@ public static Span MustBeLongerThanOrEqualTo( string? message = null ) { - if (parameter.Length < length) - { - Throw.SpanMustBeLongerThanOrEqualTo(parameter, length, parameterName, message); - } - + ((ReadOnlySpan) parameter).MustBeLongerThanOrEqualTo(length, parameterName, message); return parameter; } @@ -115,7 +112,7 @@ SpanExceptionFactory exceptionFactory public static ReadOnlySpan MustBeLongerThanOrEqualTo( this ReadOnlySpan parameter, int length, - [CallerArgumentExpression("parameter")] string? parameterName = null, + [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null ) { diff --git a/Code/Light.GuardClauses/Check.MustBeNewLine.cs b/Code/Light.GuardClauses/Check.MustBeNewLine.cs index c5fef10..dcba3db 100644 --- a/Code/Light.GuardClauses/Check.MustBeNewLine.cs +++ b/Code/Light.GuardClauses/Check.MustBeNewLine.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeOfType.cs b/Code/Light.GuardClauses/Check.MustBeOfType.cs index bbf9001..7e9f44c 100644 --- a/Code/Light.GuardClauses/Check.MustBeOfType.cs +++ b/Code/Light.GuardClauses/Check.MustBeOfType.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeOneOf.cs b/Code/Light.GuardClauses/Check.MustBeOneOf.cs index 6d70417..edcf7b4 100644 --- a/Code/Light.GuardClauses/Check.MustBeOneOf.cs +++ b/Code/Light.GuardClauses/Check.MustBeOneOf.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeRelativeUri.cs b/Code/Light.GuardClauses/Check.MustBeRelativeUri.cs index 6864c3d..9eee840 100644 --- a/Code/Light.GuardClauses/Check.MustBeRelativeUri.cs +++ b/Code/Light.GuardClauses/Check.MustBeRelativeUri.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeShorterThan.cs b/Code/Light.GuardClauses/Check.MustBeShorterThan.cs index f871358..7e47e1e 100644 --- a/Code/Light.GuardClauses/Check.MustBeShorterThan.cs +++ b/Code/Light.GuardClauses/Check.MustBeShorterThan.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using Light.GuardClauses.Exceptions; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; namespace Light.GuardClauses; @@ -73,11 +74,7 @@ public static Span MustBeShorterThan( string? message = null ) { - if (parameter.Length >= length) - { - Throw.SpanMustBeShorterThan(parameter, length, parameterName, message); - } - + ((ReadOnlySpan) parameter).MustBeShorterThan(length, parameterName, message); return parameter; } diff --git a/Code/Light.GuardClauses/Check.MustBeShorterThanOrEqualTo.cs b/Code/Light.GuardClauses/Check.MustBeShorterThanOrEqualTo.cs index 20f2de8..dc9903f 100644 --- a/Code/Light.GuardClauses/Check.MustBeShorterThanOrEqualTo.cs +++ b/Code/Light.GuardClauses/Check.MustBeShorterThanOrEqualTo.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; @@ -73,11 +74,7 @@ public static Span MustBeShorterThanOrEqualTo( string? message = null ) { - if (parameter.Length > length) - { - Throw.SpanMustBeShorterThanOrEqualTo(parameter, length, parameterName, message); - } - + ((ReadOnlySpan) parameter).MustBeShorterThanOrEqualTo(length, parameterName, message); return parameter; } diff --git a/Code/Light.GuardClauses/Check.MustBeSubstringOf.cs b/Code/Light.GuardClauses/Check.MustBeSubstringOf.cs index 63fd7f9..81d8cda 100644 --- a/Code/Light.GuardClauses/Check.MustBeSubstringOf.cs +++ b/Code/Light.GuardClauses/Check.MustBeSubstringOf.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeTrimmed.cs b/Code/Light.GuardClauses/Check.MustBeTrimmed.cs index c4e163e..ea9fb3c 100644 --- a/Code/Light.GuardClauses/Check.MustBeTrimmed.cs +++ b/Code/Light.GuardClauses/Check.MustBeTrimmed.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeTrimmedAtEnd.cs b/Code/Light.GuardClauses/Check.MustBeTrimmedAtEnd.cs index de90549..f37319c 100644 --- a/Code/Light.GuardClauses/Check.MustBeTrimmedAtEnd.cs +++ b/Code/Light.GuardClauses/Check.MustBeTrimmedAtEnd.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeTrimmedAtStart.cs b/Code/Light.GuardClauses/Check.MustBeTrimmedAtStart.cs index 34dad8b..6096052 100644 --- a/Code/Light.GuardClauses/Check.MustBeTrimmedAtStart.cs +++ b/Code/Light.GuardClauses/Check.MustBeTrimmedAtStart.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustBeUnspecified.cs b/Code/Light.GuardClauses/Check.MustBeUnspecified.cs index 97810eb..d4c806d 100644 --- a/Code/Light.GuardClauses/Check.MustBeUnspecified.cs +++ b/Code/Light.GuardClauses/Check.MustBeUnspecified.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustBeUtc.cs b/Code/Light.GuardClauses/Check.MustBeUtc.cs index efa2369..87cbc2b 100644 --- a/Code/Light.GuardClauses/Check.MustBeUtc.cs +++ b/Code/Light.GuardClauses/Check.MustBeUtc.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustBeValidEnumValue.cs b/Code/Light.GuardClauses/Check.MustBeValidEnumValue.cs index 9672a6d..c078cb3 100644 --- a/Code/Light.GuardClauses/Check.MustBeValidEnumValue.cs +++ b/Code/Light.GuardClauses/Check.MustBeValidEnumValue.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustContain.cs b/Code/Light.GuardClauses/Check.MustContain.cs index 24b7175..7158a37 100644 --- a/Code/Light.GuardClauses/Check.MustContain.cs +++ b/Code/Light.GuardClauses/Check.MustContain.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustEndWith.cs b/Code/Light.GuardClauses/Check.MustEndWith.cs index 06dc8b2..3f15afd 100644 --- a/Code/Light.GuardClauses/Check.MustEndWith.cs +++ b/Code/Light.GuardClauses/Check.MustEndWith.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveCount.cs b/Code/Light.GuardClauses/Check.MustHaveCount.cs index 3c88fad..cf1755b 100644 --- a/Code/Light.GuardClauses/Check.MustHaveCount.cs +++ b/Code/Light.GuardClauses/Check.MustHaveCount.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using Light.GuardClauses.FrameworkExtensions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveLength.cs b/Code/Light.GuardClauses/Check.MustHaveLength.cs index d912c6b..a7d8d4f 100644 --- a/Code/Light.GuardClauses/Check.MustHaveLength.cs +++ b/Code/Light.GuardClauses/Check.MustHaveLength.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; @@ -73,11 +74,7 @@ public static Span MustHaveLength( string? message = null ) { - if (parameter.Length != length) - { - Throw.InvalidSpanLength(parameter, length, parameterName, message); - } - + ((ReadOnlySpan) parameter).MustHaveLength(length, parameterName, message); return parameter; } diff --git a/Code/Light.GuardClauses/Check.MustHaveLengthIn.cs b/Code/Light.GuardClauses/Check.MustHaveLengthIn.cs index 340d142..c190c98 100644 --- a/Code/Light.GuardClauses/Check.MustHaveLengthIn.cs +++ b/Code/Light.GuardClauses/Check.MustHaveLengthIn.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveMaximumCount.cs b/Code/Light.GuardClauses/Check.MustHaveMaximumCount.cs index b141e55..1f37f9f 100644 --- a/Code/Light.GuardClauses/Check.MustHaveMaximumCount.cs +++ b/Code/Light.GuardClauses/Check.MustHaveMaximumCount.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using Light.GuardClauses.FrameworkExtensions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveMinimumCount.cs b/Code/Light.GuardClauses/Check.MustHaveMinimumCount.cs index c858def..94b101b 100644 --- a/Code/Light.GuardClauses/Check.MustHaveMinimumCount.cs +++ b/Code/Light.GuardClauses/Check.MustHaveMinimumCount.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using Light.GuardClauses.FrameworkExtensions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveOneSchemeOf.cs b/Code/Light.GuardClauses/Check.MustHaveOneSchemeOf.cs index 19e6b80..61c333f 100644 --- a/Code/Light.GuardClauses/Check.MustHaveOneSchemeOf.cs +++ b/Code/Light.GuardClauses/Check.MustHaveOneSchemeOf.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveScheme.cs b/Code/Light.GuardClauses/Check.MustHaveScheme.cs index aef3ae0..1176b03 100644 --- a/Code/Light.GuardClauses/Check.MustHaveScheme.cs +++ b/Code/Light.GuardClauses/Check.MustHaveScheme.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustHaveValue.cs b/Code/Light.GuardClauses/Check.MustHaveValue.cs index b20b452..4af6c87 100644 --- a/Code/Light.GuardClauses/Check.MustHaveValue.cs +++ b/Code/Light.GuardClauses/Check.MustHaveValue.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustMatch.cs b/Code/Light.GuardClauses/Check.MustMatch.cs index 591e357..fcf5728 100644 --- a/Code/Light.GuardClauses/Check.MustMatch.cs +++ b/Code/Light.GuardClauses/Check.MustMatch.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBe.cs b/Code/Light.GuardClauses/Check.MustNotBe.cs index 93b4ced..ab562a4 100644 --- a/Code/Light.GuardClauses/Check.MustNotBe.cs +++ b/Code/Light.GuardClauses/Check.MustNotBe.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustNotBeDefault.cs b/Code/Light.GuardClauses/Check.MustNotBeDefault.cs index d2fade3..e5a8fcd 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeDefault.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeDefault.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeEmpty.cs b/Code/Light.GuardClauses/Check.MustNotBeEmpty.cs index f933994..675e769 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeEmpty.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeEmpty.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustNotBeGreaterThan.cs b/Code/Light.GuardClauses/Check.MustNotBeGreaterThan.cs index 4c02346..c64ff6c 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeGreaterThan.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeGreaterThan.cs @@ -1,6 +1,7 @@ using System; using JetBrains.Annotations; using System.Runtime.CompilerServices; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeGreaterThanOrEqualTo.cs b/Code/Light.GuardClauses/Check.MustNotBeGreaterThanOrEqualTo.cs index 26a996e..7ffde4d 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeGreaterThanOrEqualTo.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeGreaterThanOrEqualTo.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeIn.cs b/Code/Light.GuardClauses/Check.MustNotBeIn.cs index 4b7a385..8cf809f 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeIn.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeIn.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeLessThan.cs b/Code/Light.GuardClauses/Check.MustNotBeLessThan.cs index e3f21b7..ccb762b 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeLessThan.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeLessThan.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeLessThanOrEqualTo.cs b/Code/Light.GuardClauses/Check.MustNotBeLessThanOrEqualTo.cs index 86b0574..14211f6 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeLessThanOrEqualTo.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeLessThanOrEqualTo.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeNull.cs b/Code/Light.GuardClauses/Check.MustNotBeNull.cs index 544a57b..76a451f 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeNull.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeNull.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeNullOrEmpty.cs b/Code/Light.GuardClauses/Check.MustNotBeNullOrEmpty.cs index 3ff1488..7beedb7 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeNullOrEmpty.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeNullOrEmpty.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using Light.GuardClauses.FrameworkExtensions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeNullOrWhiteSpace.cs b/Code/Light.GuardClauses/Check.MustNotBeNullOrWhiteSpace.cs index e93d21b..de13b50 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeNullOrWhiteSpace.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeNullOrWhiteSpace.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeNullReference.cs b/Code/Light.GuardClauses/Check.MustNotBeNullReference.cs index ffd3ce3..b6b5ee5 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeNullReference.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeNullReference.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeOneOf.cs b/Code/Light.GuardClauses/Check.MustNotBeOneOf.cs index f4001b0..4ad82f8 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeOneOf.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeOneOf.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotBeSameAs.cs b/Code/Light.GuardClauses/Check.MustNotBeSameAs.cs index 9b1af7f..59e9ef4 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeSameAs.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeSameAs.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; namespace Light.GuardClauses; diff --git a/Code/Light.GuardClauses/Check.MustNotBeSubstringOf.cs b/Code/Light.GuardClauses/Check.MustNotBeSubstringOf.cs index b098ae8..98cc45c 100644 --- a/Code/Light.GuardClauses/Check.MustNotBeSubstringOf.cs +++ b/Code/Light.GuardClauses/Check.MustNotBeSubstringOf.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotContain.cs b/Code/Light.GuardClauses/Check.MustNotContain.cs index d9290ff..ddadc32 100644 --- a/Code/Light.GuardClauses/Check.MustNotContain.cs +++ b/Code/Light.GuardClauses/Check.MustNotContain.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotEndWith.cs b/Code/Light.GuardClauses/Check.MustNotEndWith.cs index 549a631..380dbba 100644 --- a/Code/Light.GuardClauses/Check.MustNotEndWith.cs +++ b/Code/Light.GuardClauses/Check.MustNotEndWith.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/Check.MustNotStartWith.cs b/Code/Light.GuardClauses/Check.MustNotStartWith.cs index d4cc668..0557068 100644 --- a/Code/Light.GuardClauses/Check.MustNotStartWith.cs +++ b/Code/Light.GuardClauses/Check.MustNotStartWith.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; @@ -24,8 +25,7 @@ public static string MustNotStartWith( [NotNull, ValidatedNotNull] this string? parameter, [NotNull, ValidatedNotNull] string value, StringComparison comparisonType = StringComparison.CurrentCulture, - [CallerArgumentExpression("parameter")] - string? parameterName = null, + [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, string? message = null ) { @@ -93,4 +93,95 @@ public static string MustNotStartWith( } return parameter; } + + /// + /// Ensures that the span does not start with the specified value, or otherwise throws a . + /// + /// The span to be checked. + /// The other span that must not start with. + /// One of the enumeration values that specifies the rules for the search. + /// The name of the parameter (optional). + /// The message that will be passed to the resulting exception (optional). + /// Thrown when starts with . + /// Thrown when or is null. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan MustNotStartWith( + this ReadOnlySpan parameter, + ReadOnlySpan value, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) + { + if (parameter.StartsWith(value, comparisonType)) + { + Throw.StringStartsWith(parameter, value, comparisonType, parameterName, message); + } + + return parameter; + } + + /// + /// Ensures that the span does not start with the specified value, or otherwise throws your custom exception. + /// + /// The span to be checked. + /// The other span that must not start with. + /// + /// The delegate that creates your custom exception. and + /// are passed to this delegate. + /// + /// + /// Your custom exception thrown when does not start with , + /// or when is null, + /// or when is null. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan MustNotStartWith( + this ReadOnlySpan parameter, + ReadOnlySpan value, + ReadOnlySpansExceptionFactory exceptionFactory + ) + where T : IEquatable + { + if (parameter.StartsWith(value)) + { + Throw.CustomSpanException(exceptionFactory, parameter, value); + } + + return parameter; + } + + /// + /// Ensures that the span does not start with the specified value, or otherwise throws your custom exception. + /// + /// The span to be checked. + /// The other span that must not start with. + /// One of the enumeration values that specifies the rules for the search. + /// + /// The delegate that creates your custom exception. , + /// , and are passed to this delegate. + /// + /// + /// Your custom exception thrown when does not start with , + /// or when is null, + /// or when is null. + /// + /// + /// Thrown when is not a valid value. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan MustNotStartWith( + this ReadOnlySpan parameter, + ReadOnlySpan value, + StringComparison comparisonType, + ReadOnlySpansExceptionFactory exceptionFactory + ) + { + if (parameter.StartsWith(value, comparisonType)) + { + Throw.CustomSpanException(exceptionFactory, parameter, value, comparisonType); + } + + return parameter; + } } diff --git a/Code/Light.GuardClauses/Check.MustStartWith.cs b/Code/Light.GuardClauses/Check.MustStartWith.cs index ce3328e..166c903 100644 --- a/Code/Light.GuardClauses/Check.MustStartWith.cs +++ b/Code/Light.GuardClauses/Check.MustStartWith.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using JetBrains.Annotations; +using Light.GuardClauses.ExceptionFactory; using Light.GuardClauses.Exceptions; using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.Argument.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.Argument.cs new file mode 100644 index 0000000..b72caec --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.Argument.cs @@ -0,0 +1,16 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws an using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void Argument(string? parameterName = null, string? message = null) => + throw new ArgumentException(message ?? $"{parameterName ?? "The value"} is invalid.", parameterName); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.ArgumentDefault.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.ArgumentDefault.cs new file mode 100644 index 0000000..7a63b47 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.ArgumentDefault.cs @@ -0,0 +1,20 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a value is the default value of its + /// type, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ArgumentDefault(string? parameterName = null, string? message = null) => + throw new ArgumentDefaultException( + parameterName, + message ?? $"{parameterName ?? "The value"} must not be the default value." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.ArgumentNull.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.ArgumentNull.cs new file mode 100644 index 0000000..0a35f34 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.ArgumentNull.cs @@ -0,0 +1,16 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default , using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ArgumentNull(string? parameterName = null, string? message = null) => + throw new ArgumentNullException(parameterName, message ?? $"{parameterName ?? "The value"} must not be null."); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.CustomException.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.CustomException.cs new file mode 100644 index 0000000..48d97cf --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.CustomException.cs @@ -0,0 +1,113 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the exception that is returned by . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomException(Func exceptionFactory) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(); + + /// + /// Throws the exception that is returned by . is + /// passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomException(Func exceptionFactory, T parameter) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(parameter); + + /// + /// Throws the exception that is returned by . and + /// are passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomException(Func exceptionFactory, T1 first, T2 second) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(first, second); + + /// + /// Throws the exception that is returned by . , + /// , and are passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomException( + Func exceptionFactory, + T1 first, + T2 second, + T3 third + ) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(first, second, third); + + /// + /// Throws the exception that is returned by . and + /// are passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomSpanException( + SpanExceptionFactory exceptionFactory, + Span span, + T value + ) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory)).Invoke(span, value); + + /// + /// Throws the exception that is returned by . is + /// passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomSpanException( + ReadOnlySpanExceptionFactory exceptionFactory, + ReadOnlySpan span + ) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(span); + + /// + /// Throws the exception that is returned by . and + /// are passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomSpanException( + ReadOnlySpanExceptionFactory exceptionFactory, + ReadOnlySpan span, + T value + ) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(span, value); + + /// + /// Throws the exception that is returned by . and + /// are passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomSpanException( + ReadOnlySpansExceptionFactory exceptionFactory, + ReadOnlySpan first, + ReadOnlySpan second + ) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(first, second); + + /// + /// Throws the exception that is returned by . , + /// , and are passed to . + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void CustomSpanException( + ReadOnlySpansExceptionFactory exceptionFactory, + ReadOnlySpan first, + ReadOnlySpan second, + T third + ) => + throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(first, second, third); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.DateTime.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.DateTime.cs new file mode 100644 index 0000000..865b60c --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.DateTime.cs @@ -0,0 +1,61 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a date time is not using + /// , using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeUtcDateTime( + DateTime parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidDateTimeException( + parameterName, + message ?? + $"{parameterName ?? "The date time"} must use kind \"{DateTimeKind.Utc}\", but it actually uses \"{parameter.Kind}\" and is \"{parameter:O}\"." + ); + + /// + /// Throws the default indicating that a date time is not using + /// , using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeLocalDateTime( + DateTime parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidDateTimeException( + parameterName, + message ?? + $"{parameterName ?? "The date time"} must use kind \"{DateTimeKind.Local}\", but it actually uses \"{parameter.Kind}\" and is \"{parameter:O}\"." + ); + + /// + /// Throws the default indicating that a date time is not using + /// , using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeUnspecifiedDateTime( + DateTime parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidDateTimeException( + parameterName, + message ?? + $"{parameterName ?? "The date time"} must use kind \"{DateTimeKind.Unspecified}\", but it actually uses \"{parameter.Kind}\" and is \"{parameter:O}\"." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyCollection.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyCollection.cs new file mode 100644 index 0000000..fc3eb72 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyCollection.cs @@ -0,0 +1,20 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a collection has no items, using the + /// optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void EmptyCollection(string? parameterName = null, string? message = null) => + throw new EmptyCollectionException( + parameterName, + message ?? $"{parameterName ?? "The collection"} must not be an empty collection, but it actually is." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyGuid.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyGuid.cs new file mode 100644 index 0000000..1a85dee --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyGuid.cs @@ -0,0 +1,20 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a GUID is empty, using the optional + /// parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void EmptyGuid(string? parameterName = null, string? message = null) => + throw new EmptyGuidException( + parameterName, + message ?? $"{parameterName ?? "The value"} must be a valid GUID, but it actually is an empty one." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyString.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyString.cs new file mode 100644 index 0000000..03ccb67 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.EmptyString.cs @@ -0,0 +1,20 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is empty, using the optional + /// parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void EmptyString(string? parameterName = null, string? message = null) => + throw new EmptyStringException( + parameterName, + message ?? $"{parameterName ?? "The string"} must not be an empty string, but it actually is." + ); +} \ No newline at end of file diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.EnumValueNotDefined.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.EnumValueNotDefined.cs new file mode 100644 index 0000000..b484837 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.EnumValueNotDefined.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a value is not one of the + /// constants defined in an enum, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void EnumValueNotDefined( + T parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : Enum => + throw new EnumValueNotDefinedException( + parameterName, + message ?? + $"{parameterName ?? "The value"} \"{parameter}\" must be one of the defined constants of enum \"{parameter.GetType()}\", but it actually is not." + ); +} \ No newline at end of file diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.ExistingItem.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.ExistingItem.cs new file mode 100644 index 0000000..376a8f9 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.ExistingItem.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Text; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a collection contains the specified item + /// that should not be part of it, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ExistingItem( + IEnumerable parameter, + TItem item, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new ExistingItemException( + parameterName, + message ?? + new StringBuilder() + .AppendLine( + $"{parameterName ?? "The collection"} must not contain {item.ToStringOrNull()}, but it actually does." + ) + .AppendCollectionContent(parameter) + .ToString() + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidCollectionCount.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidCollectionCount.cs new file mode 100644 index 0000000..d67f05c --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidCollectionCount.cs @@ -0,0 +1,29 @@ +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a collection has an invalid + /// number of items, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidCollectionCount( + IEnumerable parameter, + int count, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The collection"} must have count {count}, but it actually has count {parameter.Count()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidEmailAddress.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidEmailAddress.cs new file mode 100644 index 0000000..ebf4c44 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidEmailAddress.cs @@ -0,0 +1,42 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws an using the optional message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidEmailAddress( + string parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidEmailAddressException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be a valid email address, but it actually is \"{parameter}\"." + ); + + /// + /// Throws an using the optional message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidEmailAddress( + ReadOnlySpan parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidEmailAddressException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be a valid email address, but it actually is \"{parameter.ToString()}\"." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidMaximumCollectionCount.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidMaximumCollectionCount.cs new file mode 100644 index 0000000..85730d7 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidMaximumCollectionCount.cs @@ -0,0 +1,29 @@ +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a collection has more than a + /// maximum number of items, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidMaximumCollectionCount( + IEnumerable parameter, + int count, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The collection"} must have at most count {count}, but it actually has count {parameter.Count()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidMinimumCollectionCount.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidMinimumCollectionCount.cs new file mode 100644 index 0000000..4914ad1 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidMinimumCollectionCount.cs @@ -0,0 +1,29 @@ +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a collection has less than a + /// minimum number of items, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidMinimumCollectionCount( + IEnumerable parameter, + int count, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The collection"} must have at least count {count}, but it actually has count {parameter.Count()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidOperation.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidOperation.cs new file mode 100644 index 0000000..0452941 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidOperation.cs @@ -0,0 +1,15 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws an using the optional message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidOperation(string? message = null) => throw new InvalidOperationException(message); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidState.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidState.cs new file mode 100644 index 0000000..cbba7f9 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidState.cs @@ -0,0 +1,15 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws an using the optional message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidState(string? message = null) => throw new InvalidStateException(message); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidTypeCast.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidTypeCast.cs new file mode 100644 index 0000000..4831b18 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.InvalidTypeCast.cs @@ -0,0 +1,29 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a reference cannot be downcast, using the + /// optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidTypeCast( + object? parameter, + Type targetType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new TypeCastException( + parameterName, + message ?? + $"{parameterName ?? "The value"} {parameter.ToStringOrNull()} cannot be cast to \"{targetType}\"." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MissingItem.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MissingItem.cs new file mode 100644 index 0000000..0d4b4cb --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MissingItem.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Text; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a collection is not containing the + /// specified item, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MissingItem( + IEnumerable parameter, + TItem item, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new MissingItemException( + parameterName, + message ?? + new StringBuilder() + .AppendLine( + $"{parameterName ?? "The collection"} must contain {item.ToStringOrNull()}, but it actually does not." + ) + .AppendCollectionContent(parameter) + .ToString() + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeGreaterThan.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeGreaterThan.cs new file mode 100644 index 0000000..cec5dcb --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeGreaterThan.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must be greater + /// than the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeGreaterThan( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must be greater than {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeGreaterThanOrEqualTo.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeGreaterThanOrEqualTo.cs new file mode 100644 index 0000000..ce70b4e --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeGreaterThanOrEqualTo.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must be greater + /// than or equal to the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeGreaterThanOrEqualTo( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must be greater than or equal to {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeLessThan.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeLessThan.cs new file mode 100644 index 0000000..9d913ed --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeLessThan.cs @@ -0,0 +1,26 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must be less + /// than the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeLessThan( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? $"{parameterName ?? "The value"} must be less than {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeLessThanOrEqualTo.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeLessThanOrEqualTo.cs new file mode 100644 index 0000000..7afb410 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustBeLessThanOrEqualTo.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must be less + /// than or equal to the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeLessThanOrEqualTo( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must be less than or equal to {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeGreaterThan.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeGreaterThan.cs new file mode 100644 index 0000000..743df30 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeGreaterThan.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must not be + /// greater than the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustNotBeGreaterThan( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must not be greater than {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeGreaterThanOrEqualTo.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeGreaterThanOrEqualTo.cs new file mode 100644 index 0000000..82df482 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeGreaterThanOrEqualTo.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must not be + /// greater than or equal to the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustNotBeGreaterThanOrEqualTo( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must not be greater than or equal to {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeLessThan.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeLessThan.cs new file mode 100644 index 0000000..e1f30f7 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeLessThan.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must not be + /// less than the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustNotBeLessThan( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must not be less than {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeLessThanOrEqualTo.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeLessThanOrEqualTo.cs new file mode 100644 index 0000000..bb53462 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.MustNotBeLessThanOrEqualTo.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a comparable value must not be + /// less than or equal to the given boundary value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustNotBeLessThanOrEqualTo( + T parameter, + T boundary, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must not be less than or equal to {boundary}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.NotFileExtension.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotFileExtension.cs new file mode 100644 index 0000000..1cc1b81 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotFileExtension.cs @@ -0,0 +1,34 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not a valid file extension. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotFileExtension(string? parameter, string? parameterName, string? message) => + throw new StringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be a valid file extension, but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string is not a valid file extension. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotFileExtension(ReadOnlySpan parameter, string? parameterName, string? message) => + throw new StringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be a valid file extension, but it actually is {parameter.ToString()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.NotNewLine.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotNewLine.cs new file mode 100644 index 0000000..e871706 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotNewLine.cs @@ -0,0 +1,21 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not equal to "\n" or "\r\n". + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotNewLine(string? parameter, string? parameterName, string? message) => + throw new StringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be either \"\\n\" or \"\\r\\n\", but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmed.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmed.cs new file mode 100644 index 0000000..a1f91e7 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmed.cs @@ -0,0 +1,21 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not trimmed. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotTrimmed(string? parameter, string? parameterName, string? message) => + throw new StringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be trimmed, but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmedAtEnd.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmedAtEnd.cs new file mode 100644 index 0000000..ba7875a --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmedAtEnd.cs @@ -0,0 +1,21 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not trimmed at the end. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotTrimmedAtEnd(string? parameter, string? parameterName, string? message) => + throw new StringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be trimmed at the end, but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmedAtStart.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmedAtStart.cs new file mode 100644 index 0000000..047fe01 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.NotTrimmedAtStart.cs @@ -0,0 +1,21 @@ +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not trimmed at the start. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotTrimmedAtStart(string? parameter, string? parameterName, string? message) => + throw new StringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be trimmed at the start, but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.NullableHasNoValue.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.NullableHasNoValue.cs new file mode 100644 index 0000000..f477ef4 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.NullableHasNoValue.cs @@ -0,0 +1,21 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a has + /// no value, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NullableHasNoValue(string? parameterName = null, string? message = null) => + throw new NullableHasNoValueException( + parameterName, + message ?? $"{parameterName ?? "The nullable"} must have a value, but it actually is null." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.Range.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.Range.cs new file mode 100644 index 0000000..a4a14ef --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.Range.cs @@ -0,0 +1,45 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a value is not within a specified + /// range, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeInRange( + T parameter, + Range range, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must be between {range.CreateRangeDescriptionText("and")}, but it actually is {parameter}." + ); + + /// + /// Throws the default indicating that a value is within a specified + /// range, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustNotBeInRange( + T parameter, + Range range, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : IComparable => + throw new ArgumentOutOfRangeException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must not be between {range.CreateRangeDescriptionText("and")}, but it actually is {parameter}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.SameObjectReference.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.SameObjectReference.cs new file mode 100644 index 0000000..8db1ebe --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.SameObjectReference.cs @@ -0,0 +1,26 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that two references point to the same + /// object, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void SameObjectReference( + T? parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) where T : class => + throw new SameObjectReferenceException( + parameterName, + message ?? + $"{parameterName ?? "The reference"} must not point to object \"{parameter}\", but it actually does." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.Span.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.Span.cs new file mode 100644 index 0000000..a3c04bf --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.Span.cs @@ -0,0 +1,100 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a span has an invalid length, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void InvalidSpanLength( + ReadOnlySpan parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The read-only span"} must have length {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a span is not longer than the + /// specified length. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void SpanMustBeLongerThan( + ReadOnlySpan parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The span"} must be longer than {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a span is not longer than and + /// not equal to the specified length. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void SpanMustBeLongerThanOrEqualTo( + ReadOnlySpan parameter, + int length, + [CallerArgumentExpression(nameof(parameter))] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The span"} must be longer than or equal to {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a span is not shorter than the + /// specified length. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void SpanMustBeShorterThan( + ReadOnlySpan parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The span"} must be shorter than {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a span is not shorter than the + /// specified length. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void SpanMustBeShorterThanOrEqualTo( + ReadOnlySpan parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidCollectionCountException( + parameterName, + message ?? + $"{parameterName ?? "The span"} must be shorter than or equal to {length}, but it actually has length {parameter.Length}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringContains.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringContains.cs new file mode 100644 index 0000000..e072d3b --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringContains.cs @@ -0,0 +1,85 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string does not contain another string as + /// a substring, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringDoesNotContain( + string parameter, + string substring, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must contain {substring.ToStringOrNull()}, but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string does not contain another string as + /// a substring, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringDoesNotContain( + string parameter, + string substring, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must contain {substring.ToStringOrNull()} ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string does contain another string as a + /// substring, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringContains( + string parameter, + string substring, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not contain {substring.ToStringOrNull()} as a substring, but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string does contain another string as a + /// substring, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringContains( + string parameter, + string substring, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not contain {substring.ToStringOrNull()} as a substring ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotEndWith.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotEndWith.cs new file mode 100644 index 0000000..6f0cece --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotEndWith.cs @@ -0,0 +1,30 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string does not end with another one, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringDoesNotEndWith( + string parameter, + string other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must end with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotMatch.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotMatch.cs new file mode 100644 index 0000000..61fc5eb --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotMatch.cs @@ -0,0 +1,28 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string does not match a regular + /// expression, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringDoesNotMatch( + string parameter, + Regex regex, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringDoesNotMatchException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must match the regular expression \"{regex}\", but it actually is \"{parameter}\"." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotStartWith.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotStartWith.cs new file mode 100644 index 0000000..e51e802 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringDoesNotStartWith.cs @@ -0,0 +1,30 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string does not start with another one, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringDoesNotStartWith( + string parameter, + string other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must start with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringEndsWith.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringEndsWith.cs new file mode 100644 index 0000000..476f035 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringEndsWith.cs @@ -0,0 +1,30 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string ends with another one, using the + /// optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringEndsWith( + string parameter, + string other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not end with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringLength.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringLength.cs new file mode 100644 index 0000000..d392362 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringLength.cs @@ -0,0 +1,117 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not shorter than the given + /// length, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringNotShorterThan( + string parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringLengthException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be shorter than {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a string is not shorter or equal to the + /// given length, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringNotShorterThanOrEqualTo( + string parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringLengthException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be shorter or equal to {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a string has a different length than the + /// specified one, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringLengthNotEqualTo( + string parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringLengthException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must have length {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a string is not longer than the given + /// length, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringNotLongerThan( + string parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringLengthException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be longer than {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a string is not longer than or equal to + /// the given length, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringNotLongerThanOrEqualTo( + string parameter, + int length, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringLengthException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be longer than or equal to {length}, but it actually has length {parameter.Length}." + ); + + /// + /// Throws the default indicating that a string's length is not in within the + /// given range, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringLengthNotInRange( + string parameter, + Range range, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new StringLengthException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must have its length in between {range.CreateRangeDescriptionText("and")}, but it actually has length {parameter.Length}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.StringStartsWith.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringStartsWith.cs new file mode 100644 index 0000000..ad19dc6 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.StringStartsWith.cs @@ -0,0 +1,49 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string does start with another one, using + /// the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringStartsWith( + string parameter, + string other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not start with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string does start with another one, using + /// the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void StringStartsWith( + ReadOnlySpan parameter, + ReadOnlySpan other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not start with \"{other.ToString()}\" ({comparisonType}), but it actually is {parameter.ToString()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.Substring.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.Substring.cs new file mode 100644 index 0000000..0d4f93d --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.Substring.cs @@ -0,0 +1,85 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string is not a substring of another one, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotSubstring( + string parameter, + string other, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be a substring of \"{other}\", but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string is not a substring of another one, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void NotSubstring( + string parameter, + string other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must be a substring of \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string is a substring of another one, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void Substring( + string parameter, + string other, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not be a substring of \"{other}\", but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that a string is a substring of another one, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void Substring( + string parameter, + string other, + StringComparison comparisonType, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new SubstringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not be a substring of \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.Uri.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.Uri.cs new file mode 100644 index 0000000..4f7a804 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.Uri.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Text; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a URI is relative instead of absolute, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeAbsoluteUri( + Uri parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new RelativeUriException( + parameterName, + message ?? $"{parameterName ?? "The URI"} must be an absolute URI, but it actually is \"{parameter}\"." + ); + + /// + /// Throws the default indicating that a URI is absolute instead of relative, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void MustBeRelativeUri( + Uri parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new AbsoluteUriException( + parameterName, + message ?? $"{parameterName ?? "The URI"} must be a relative URI, but it actually is \"{parameter}\"." + ); + + /// + /// Throws the default indicating that a URI has an unexpected scheme, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void UriMustHaveScheme( + Uri parameter, + string scheme, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidUriSchemeException( + parameterName, + message ?? + $"{parameterName ?? "The URI"} must use the scheme \"{scheme}\", but it actually is \"{parameter}\"." + ); + + /// + /// Throws the default indicating that a URI does not use one of a set of + /// expected schemes, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void UriMustHaveOneSchemeOf( + Uri parameter, + IEnumerable schemes, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new InvalidUriSchemeException( + parameterName, + message ?? + new StringBuilder().AppendLine($"{parameterName ?? "The URI"} must use one of the following schemes") + .AppendItemsWithNewLine(schemes) + .AppendLine($"but it actually is \"{parameter}\".") + .ToString() + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.ValueIsOneOf.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.ValueIsOneOf.cs new file mode 100644 index 0000000..65809d1 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.ValueIsOneOf.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Text; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a value is one of a specified collection + /// of items, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ValueIsOneOf( + TItem parameter, + IEnumerable items, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new ValueIsOneOfException( + parameterName, + message ?? + new StringBuilder().AppendLine($"{parameterName ?? "The value"} must not be one of the following items") + .AppendItemsWithNewLine(items) + .AppendLine($"but it actually is {parameter.ToStringOrNull()}.") + .ToString() + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.ValueNotOneOf.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.ValueNotOneOf.cs new file mode 100644 index 0000000..746b457 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.ValueNotOneOf.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Text; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a value is not one of a specified + /// collection of items, using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ValueNotOneOf( + TItem parameter, + IEnumerable items, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new ValueIsNotOneOfException( + parameterName, + message ?? + new StringBuilder().AppendLine($"{parameterName ?? "The value"} must be one of the following items") + .AppendItemsWithNewLine(items) + .AppendLine($"but it actually is {parameter.ToStringOrNull()}.") + .ToString() + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.ValuesEqual.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.ValuesEqual.cs new file mode 100644 index 0000000..05157e6 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.ValuesEqual.cs @@ -0,0 +1,46 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; +using Light.GuardClauses.FrameworkExtensions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that two values are not equal, using the + /// optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ValuesNotEqual( + T parameter, + T other, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new ValuesNotEqualException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must be equal to {other.ToStringOrNull()}, but it actually is {parameter.ToStringOrNull()}." + ); + + /// + /// Throws the default indicating that two values are equal, using the optional + /// parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void ValuesEqual( + T parameter, + T other, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new ValuesEqualException( + parameterName, + message ?? + $"{parameterName ?? "The value"} must not be equal to {other.ToStringOrNull()}, but it actually is {parameter.ToStringOrNull()}." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.WhiteSpaceString.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.WhiteSpaceString.cs new file mode 100644 index 0000000..bae38b2 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.WhiteSpaceString.cs @@ -0,0 +1,26 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +using Light.GuardClauses.Exceptions; + +namespace Light.GuardClauses.ExceptionFactory; + +public static partial class Throw +{ + /// + /// Throws the default indicating that a string contains only white space, + /// using the optional parameter name and message. + /// + [ContractAnnotation("=> halt")] + [DoesNotReturn] + public static void WhiteSpaceString( + string parameter, + [CallerArgumentExpression("parameter")] string? parameterName = null, + string? message = null + ) => + throw new WhiteSpaceStringException( + parameterName, + message ?? + $"{parameterName ?? "The string"} must not contain only white space, but it actually is \"{parameter}\"." + ); +} diff --git a/Code/Light.GuardClauses/ExceptionFactory/Throw.cs b/Code/Light.GuardClauses/ExceptionFactory/Throw.cs new file mode 100644 index 0000000..98a4046 --- /dev/null +++ b/Code/Light.GuardClauses/ExceptionFactory/Throw.cs @@ -0,0 +1,6 @@ +namespace Light.GuardClauses.ExceptionFactory; + +/// +/// Provides static factory methods that throw default exceptions. +/// +public static partial class Throw; diff --git a/Code/Light.GuardClauses/Exceptions/Throw.cs b/Code/Light.GuardClauses/Exceptions/Throw.cs deleted file mode 100644 index 104c6b2..0000000 --- a/Code/Light.GuardClauses/Exceptions/Throw.cs +++ /dev/null @@ -1,702 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using System.Text; -using System.Text.RegularExpressions; -using JetBrains.Annotations; -using Light.GuardClauses.FrameworkExtensions; - -namespace Light.GuardClauses.Exceptions; - -/// -/// Provides static factory methods that throw default exceptions. -/// -public static class Throw -{ - /// - /// Throws the default , using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ArgumentNull(string? parameterName = null, string? message = null) => - throw new ArgumentNullException(parameterName, message ?? $"{parameterName ?? "The value"} must not be null."); - - /// - /// Throws the default indicating that a value is the default value of its type, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ArgumentDefault(string? parameterName = null, string? message = null) => - throw new ArgumentDefaultException(parameterName, message ?? $"{parameterName ?? "The value"} must not be the default value."); - - /// - /// Throws the default indicating that a reference cannot be downcast, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidTypeCast(object? parameter, Type targetType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new TypeCastException(parameterName, message ?? $"{parameterName ?? "The value"} {parameter.ToStringOrNull()} cannot be cast to \"{targetType}\"."); - - /// - /// Throws the default indicating that a value is not one of the constants defined in an enum, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void EnumValueNotDefined(T parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : Enum => - throw new EnumValueNotDefinedException(parameterName, message ?? $"{parameterName ?? "The value"} \"{parameter}\" must be one of the defined constants of enum \"{parameter.GetType()}\", but it actually is not."); - - /// - /// Throws the default indicating that a GUID is empty, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void EmptyGuid(string? parameterName = null, string? message = null) => - throw new EmptyGuidException(parameterName, message ?? $"{parameterName ?? "The value"} must be a valid GUID, but it actually is an empty one."); - - /// - /// Throws an using the optional message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidOperation(string? message = null) => throw new InvalidOperationException(message); - - /// - /// Throws an using the optional message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidState(string? message = null) => throw new InvalidStateException(message); - - /// - /// Throws an using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void Argument(string? parameterName = null, string? message = null) => - throw new ArgumentException(message ?? $"{parameterName ?? "The value"} is invalid.", parameterName); - - /// - /// Throws an using the optional message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidEmailAddress(string parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidEmailAddressException(parameterName, message ?? $"{parameterName ?? "The string"} must be a valid email address, but it actually is \"{parameter}\"."); - - /// - /// Throws an using the optional message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidEmailAddress(ReadOnlySpan parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidEmailAddressException(parameterName, message ?? $"{parameterName ?? "The string"} must be a valid email address, but it actually is \"{parameter.ToString()}\"."); - - /// - /// Throws the default indicating that a has no value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NullableHasNoValue(string? parameterName = null, string? message = null) => - throw new NullableHasNoValueException(parameterName, message ?? $"{parameterName ?? "The nullable"} must have a value, but it actually is null."); - - /// - /// Throws the default indicating that a comparable value must not be less than the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustNotBeLessThan(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must not be less than {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must be less than the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeLessThan(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must be less than {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must not be less than or equal to the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustNotBeLessThanOrEqualTo(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must not be less than or equal to {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must not be greater than or equal to the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustNotBeGreaterThanOrEqualTo(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must not be greater than or equal to {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must be greater than or equal to the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeGreaterThanOrEqualTo(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must be greater than or equal to {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must be greater than the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeGreaterThan(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must be greater than {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must not be greater than the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustNotBeGreaterThan(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must not be greater than {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a comparable value must be less than or equal to the given boundary value, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeLessThanOrEqualTo(T parameter, T boundary, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must be less than or equal to {boundary}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a value is not within a specified range, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeInRange(T parameter, Range range, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must be between {range.CreateRangeDescriptionText("and")}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that a value is within a specified range, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustNotBeInRange(T parameter, Range range, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : IComparable => - throw new ArgumentOutOfRangeException(parameterName, message ?? $"{parameterName ?? "The value"} must not be between {range.CreateRangeDescriptionText("and")}, but it actually is {parameter}."); - - /// - /// Throws the default indicating that two references point to the same object, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SameObjectReference(T? parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : class => - throw new SameObjectReferenceException(parameterName, message ?? $"{parameterName ?? "The reference"} must not point to object \"{parameter}\", but it actually does."); - - /// - /// Throws the default indicating that a string is empty, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void EmptyString(string? parameterName = null, string? message = null) => - throw new EmptyStringException(parameterName, message ?? $"{parameterName ?? "The string"} must not be an empty string, but it actually is."); - - /// - /// Throws the default indicating that a string contains only white space, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void WhiteSpaceString(string parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new WhiteSpaceStringException(parameterName, message ?? $"{parameterName ?? "The string"} must not contain only white space, but it actually is \"{parameter}\"."); - - /// - /// Throws the default indicating that a string does not match a regular expression, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringDoesNotMatch(string parameter, Regex regex, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringDoesNotMatchException(parameterName, message ?? $"{parameterName ?? "The string"} must match the regular expression \"{regex}\", but it actually is \"{parameter}\"."); - - /// - /// Throws the default indicating that a string does not contain another string as a substring, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringDoesNotContain(string parameter, string substring, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must contain {substring.ToStringOrNull()}, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string does not contain another string as a substring, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringDoesNotContain(string parameter, string substring, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must contain {substring.ToStringOrNull()} ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string does contain another string as a substring, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringContains(string parameter, string substring, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must not contain {substring.ToStringOrNull()} as a substring, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string does contain another string as a substring, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringContains(string parameter, string substring, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must not contain {substring.ToStringOrNull()} as a substring ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not a substring of another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotSubstring(string parameter, string other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must be a substring of \"{other}\", but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not a substring of another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotSubstring(string parameter, string other, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must be a substring of \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is a substring of another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void Substring(string parameter, string other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must not be a substring of \"{other}\", but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is a substring of another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void Substring(string parameter, string other, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must not be a substring of \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string does not start with another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringDoesNotStartWith(string parameter, string other, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must start with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string does start with another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringStartsWith(string parameter, string other, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must not start with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string does not end with another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringDoesNotEndWith(string parameter, string other, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must end with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string ends with another one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringEndsWith(string parameter, string other, StringComparison comparisonType, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new SubstringException(parameterName, message ?? $"{parameterName ?? "The string"} must not end with \"{other}\" ({comparisonType}), but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not shorter than the given length, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringNotShorterThan(string parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringLengthException(parameterName, message ?? $"{parameterName ?? "The string"} must be shorter than {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a string is not shorter or equal to the given length, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringNotShorterThanOrEqualTo(string parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringLengthException(parameterName, message ?? $"{parameterName ?? "The string"} must be shorter or equal to {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a string has a different length than the specified one, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringLengthNotEqualTo(string parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringLengthException(parameterName, message ?? $"{parameterName ?? "The string"} must have length {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a string is not longer than the given length, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringNotLongerThan(string parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringLengthException(parameterName, message ?? $"{parameterName ?? "The string"} must be longer than {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a string is not longer or equal to the given length, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringNotLongerThanOrEqualTo(string parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringLengthException(parameterName, message ?? $"{parameterName ?? "The string"} must be longer than or equal to {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a string's length is not in between the given range, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void StringLengthNotInRange(string parameter, Range range, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new StringLengthException(parameterName, message ?? $"{parameterName ?? "The string"} must have its length in between {range.CreateRangeDescriptionText("and")}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a string is not equal to "\n" or "\r\n". - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotNewLine(string? parameter, string? parameterName, string? message) => - throw new StringException(parameterName, message ?? $"{parameterName ?? "The string"} must be either \"\\n\" or \"\\r\\n\", but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not trimmed. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotTrimmed(string? parameter, string? parameterName, string? message) => - throw new StringException(parameterName, message ?? $"{parameterName ?? "The string"} must be trimmed, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not trimmed at the start. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotTrimmedAtStart(string? parameter, string? parameterName, string? message) => - throw new StringException(parameterName, message ?? $"{parameterName ?? "The string"} must be trimmed at the start, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not trimmed at the end. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotTrimmedAtEnd(string? parameter, string? parameterName, string? message) => - throw new StringException(parameterName, message ?? $"{parameterName ?? "The string"} must be trimmed at the end, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not a valid file extension. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotFileExtension(string? parameter, string? parameterName, string? message) => - throw new StringException(parameterName, message ?? $"{parameterName ?? "The string"} must be a valid file extension, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a string is not a valid file extension. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void NotFileExtension(ReadOnlySpan parameter, string? parameterName, string? message) => - throw new StringException(parameterName, message ?? $"{parameterName ?? "The string"} must be a valid file extension, but it actually is {parameter.ToString()}."); - - /// - /// Throws the default indicating that two values are not equal, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ValuesNotEqual(T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new ValuesNotEqualException(parameterName, message ?? $"{parameterName ?? "The value"} must be equal to {other.ToStringOrNull()}, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that two values are equal, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ValuesEqual(T parameter, T other, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new ValuesEqualException(parameterName, message ?? $"{parameterName ?? "The value"} must not be equal to {other.ToStringOrNull()}, but it actually is {parameter.ToStringOrNull()}."); - - /// - /// Throws the default indicating that a collection has an invalid number of items, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidCollectionCount(IEnumerable parameter, int count, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The collection"} must have count {count}, but it actually has count {parameter.Count()}."); - - /// - /// Throws the default indicating that a span has an invalid length, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidSpanLength(in Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must have length {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span has an invalid length, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidSpanLength(in ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The read-only span"} must have length {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a collection has less than a minimum number of items, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidMinimumCollectionCount(IEnumerable parameter, int count, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The collection"} must have at least count {count}, but it actually has count {parameter.Count()}."); - - /// - /// Throws the default indicating that a span is not longer than the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeLongerThan(in Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be longer than {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not longer than the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeLongerThan(in ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be longer than {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not longer than and not equal to the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeLongerThanOrEqualTo(in Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be longer than or equal to {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not longer than and not equal to the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeLongerThanOrEqualTo(in ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be longer than or equal to {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not shorter than the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeShorterThan(in Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be shorter than {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not shorter than the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeShorterThanOrEqualTo(in Span parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be shorter than or equal to {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not shorter than the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeShorterThanOrEqualTo(in ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be shorter than or equal to {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a span is not shorter than the specified length. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void SpanMustBeShorterThan(in ReadOnlySpan parameter, int length, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The span"} must be shorter than {length}, but it actually has length {parameter.Length}."); - - /// - /// Throws the default indicating that a collection has more than a maximum number of items, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void InvalidMaximumCollectionCount(IEnumerable parameter, int count, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidCollectionCountException(parameterName, message ?? $"{parameterName ?? "The collection"} must have at most count {count}, but it actually has count {parameter.Count()}."); - - /// - /// Throws the default indicating that a collection has no items, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void EmptyCollection(string? parameterName = null, string? message = null) => - throw new EmptyCollectionException(parameterName, message ?? $"{parameterName ?? "The collection"} must not be an empty collection, but it actually is."); - - /// - /// Throws the default indicating that a collection is not containing the specified item, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MissingItem(IEnumerable parameter, TItem item, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new MissingItemException(parameterName, - message ?? - new StringBuilder().AppendLine($"{parameterName ?? "The collection"} must contain {item.ToStringOrNull()}, but it actually does not.") - .AppendCollectionContent(parameter) - .ToString()); - - /// - /// Throws the default indicating that a collection contains the specified item that should not be part of it, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ExistingItem(IEnumerable parameter, TItem item, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new ExistingItemException(parameterName, - message ?? - new StringBuilder().AppendLine($"{parameterName ?? "The collection"} must not contain {item.ToStringOrNull()}, but it actually does.") - .AppendCollectionContent(parameter) - .ToString()); - - /// - /// Throws the default indicating that a value is not one of a specified collection of items, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ValueNotOneOf(TItem parameter, IEnumerable items, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new ValueIsNotOneOfException(parameterName, - message ?? - new StringBuilder().AppendLine($"{parameterName ?? "The value"} must be one of the following items") - .AppendItemsWithNewLine(items) - .AppendLine($"but it actually is {parameter.ToStringOrNull()}.") - .ToString()); - - /// - /// Throws the default indicating that a value is one of a specified collection of items, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void ValueIsOneOf(TItem parameter, IEnumerable items, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new ValueIsOneOfException(parameterName, - message ?? - new StringBuilder().AppendLine($"{parameterName ?? "The value"} must not be one of the following items") - .AppendItemsWithNewLine(items) - .AppendLine($"but it actually is {parameter.ToStringOrNull()}.") - .ToString()); - - /// - /// Throws the default indicating that a URI is relative instead of absolute, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeAbsoluteUri(Uri parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new RelativeUriException(parameterName, message ?? $"{parameterName ?? "The URI"} must be an absolute URI, but it actually is \"{parameter}\"."); - - /// - /// Throws the default indicating that a URI is absolute instead of relative, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeRelativeUri(Uri parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new AbsoluteUriException(parameterName, message ?? $"{parameterName ?? "The URI"} must be a relative URI, but it actually is \"{parameter}\"."); - - /// - /// Throws the default indicating that a URI has an unexpected scheme, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void UriMustHaveScheme(Uri parameter, string scheme, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidUriSchemeException(parameterName, message ?? $"{parameterName ?? "The URI"} must use the scheme \"{scheme}\", but it actually is \"{parameter}\"."); - - /// - /// Throws the default indicating that a URI does not use one of a set of expected schemes, using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void UriMustHaveOneSchemeOf(Uri parameter, IEnumerable schemes, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidUriSchemeException(parameterName, - message ?? - new StringBuilder().AppendLine($"{parameterName ?? "The URI"} must use one of the following schemes") - .AppendItemsWithNewLine(schemes) - .AppendLine($"but it actually is \"{parameter}\".") - .ToString()); - - /// - /// Throws the default indicating that a date time is not using , using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeUtcDateTime(DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidDateTimeException(parameterName, message ?? $"{parameterName ?? "The date time"} must use kind \"{DateTimeKind.Utc}\", but it actually uses \"{parameter.Kind}\" and is \"{parameter:O}\"."); - - /// - /// Throws the default indicating that a date time is not using , using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeLocalDateTime(DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidDateTimeException(parameterName, message ?? $"{parameterName ?? "The date time"} must use kind \"{DateTimeKind.Local}\", but it actually uses \"{parameter.Kind}\" and is \"{parameter:O}\"."); - - /// - /// Throws the default indicating that a date time is not using , using the optional parameter name and message. - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void MustBeUnspecifiedDateTime(DateTime parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) => - throw new InvalidDateTimeException(parameterName, message ?? $"{parameterName ?? "The date time"} must use kind \"{DateTimeKind.Unspecified}\", but it actually uses \"{parameter.Kind}\" and is \"{parameter:O}\"."); - - /// - /// Throws the exception that is returned by . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomException(Func exceptionFactory) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(); - - /// - /// Throws the exception that is returned by . is passed to . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomException(Func exceptionFactory, T parameter) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(parameter); - - /// - /// Throws the exception that is returned by . and are passed to . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomException(Func exceptionFactory, T1 first, T2 second) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(first, second); - - /// - /// Throws the exception that is returned by . , , and are passed to . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomException(Func exceptionFactory, T1 first, T2 second, T3 third) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(first, second, third); - - /// - /// Throws the exception that is returned by . and are passed to . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomSpanException(SpanExceptionFactory exceptionFactory, in Span span, T value) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory)).Invoke(span, value); - - /// - /// Throws the exception that is returned by . is passed to . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomSpanException(ReadOnlySpanExceptionFactory exceptionFactory, in ReadOnlySpan span) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(span); - - /// - /// Throws the exception that is returned by . and are passed to . - /// - [ContractAnnotation("=> halt")] - [DoesNotReturn] - public static void CustomSpanException(ReadOnlySpanExceptionFactory exceptionFactory, in ReadOnlySpan span, T value) => - throw exceptionFactory.MustNotBeNull(nameof(exceptionFactory))(span, value); -} \ No newline at end of file diff --git a/Code/Light.GuardClauses/SpanDelegates.cs b/Code/Light.GuardClauses/SpanDelegates.cs index 0d4c018..0eecc93 100644 --- a/Code/Light.GuardClauses/SpanDelegates.cs +++ b/Code/Light.GuardClauses/SpanDelegates.cs @@ -15,4 +15,18 @@ namespace Light.GuardClauses; /// /// Represents a delegate that receives a read-only span and a value as parameters and that produces an exception. /// -public delegate Exception ReadOnlySpanExceptionFactory(ReadOnlySpan span, T value); \ No newline at end of file +public delegate Exception ReadOnlySpanExceptionFactory(ReadOnlySpan span, T value); + +/// +/// Represents a delegate that receives two spans and produces an exception. +/// +public delegate Exception ReadOnlySpansExceptionFactory(ReadOnlySpan span1, ReadOnlySpan span2); + +/// +/// Represents a delegate that receives two spans and a value as parameters and that produces an exception. +/// +public delegate Exception ReadOnlySpansExceptionFactory( + ReadOnlySpan span1, + ReadOnlySpan span2, + T value +);