This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
313 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using JetBrains.Annotations; | ||
|
||
namespace Dawn | ||
{ | ||
/// <content>Provides preconditions for <see cref="DateTime" /> arguments.</content> | ||
public static partial class Guard | ||
{ | ||
/// <summary> | ||
/// Requires the date-time argument to have its <see cref="DateTime.Kind" /> specified, | ||
/// i.e. not to have it as <see cref="DateTimeKind.Unspecified" />. | ||
/// </summary> | ||
/// <param name="argument">The argument.</param> | ||
/// <param name="message"> | ||
/// The message of the exception that will be thrown if the precondition is not satisfied. | ||
/// </param> | ||
/// <returns><paramref name="argument" />.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is | ||
/// <see cref="DateTimeKind.Unspecified" />. | ||
/// </exception> | ||
[AssertionMethod] | ||
[DebuggerStepThrough] | ||
[GuardFunction("DateTime", "gtks")] | ||
public static ref readonly ArgumentInfo<DateTime> KindSpecified( | ||
in this ArgumentInfo<DateTime> argument, Func<DateTime, string>? message = null) | ||
{ | ||
if (argument.Value.Kind == DateTimeKind.Unspecified) | ||
{ | ||
var m = message?.Invoke(argument.Value) ?? Messages.KindSpecified(argument); | ||
throw Fail(new ArgumentException(m, argument.Name)); | ||
} | ||
|
||
return ref argument; | ||
} | ||
|
||
/// <summary> | ||
/// Requires the date-time argument to either be <c>null</c> or have its | ||
/// <see cref="DateTime.Kind" /> specified, i.e. not to have it as | ||
/// <see cref="DateTimeKind.Unspecified" />. | ||
/// </summary> | ||
/// <param name="argument">The argument.</param> | ||
/// <param name="message"> | ||
/// The message of the exception that will be thrown if the precondition is not satisfied. | ||
/// </param> | ||
/// <returns><paramref name="argument" />.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is | ||
/// <see cref="DateTimeKind.Unspecified" />. | ||
/// </exception> | ||
[AssertionMethod] | ||
[DebuggerStepThrough] | ||
[GuardFunction("DateTime", "gtks")] | ||
public static ref readonly ArgumentInfo<DateTime?> KindSpecified( | ||
in this ArgumentInfo<DateTime?> argument, Func<DateTime, string>? message = null) | ||
{ | ||
if (argument.HasValue()) | ||
{ | ||
var value = argument.GetValueOrDefault(); | ||
if (value.Kind == DateTimeKind.Unspecified) | ||
{ | ||
var m = message?.Invoke(value) ?? Messages.KindSpecified(argument); | ||
throw Fail(new ArgumentException(m, argument.Name)); | ||
} | ||
} | ||
|
||
return ref argument; | ||
} | ||
|
||
/// <summary> | ||
/// Requires the date-time argument not to have its <see cref="DateTime.Kind" /> | ||
/// specified, i.e. to have it as <see cref="DateTimeKind.Unspecified" />. | ||
/// </summary> | ||
/// <param name="argument">The argument.</param> | ||
/// <param name="message"> | ||
/// The message of the exception that will be thrown if the precondition is not satisfied. | ||
/// </param> | ||
/// <returns><paramref name="argument" />.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is not | ||
/// <see cref="DateTimeKind.Unspecified" />. | ||
/// </exception> | ||
[AssertionMethod] | ||
[DebuggerStepThrough] | ||
[GuardFunction("DateTime", "gtku")] | ||
public static ref readonly ArgumentInfo<DateTime> KindUnspecified( | ||
in this ArgumentInfo<DateTime> argument, Func<DateTime, string>? message = null) | ||
{ | ||
if (argument.Value.Kind != DateTimeKind.Unspecified) | ||
{ | ||
var m = message?.Invoke(argument.Value) ?? Messages.KindUnspecified(argument); | ||
throw Fail(new ArgumentException(m, argument.Name)); | ||
} | ||
|
||
return ref argument; | ||
} | ||
|
||
/// <summary> | ||
/// Requires the date-time argument either to be <c>null</c> or not to have its | ||
/// <see cref="DateTime.Kind" /> specified, i.e. to have it as | ||
/// <see cref="DateTimeKind.Unspecified" />. | ||
/// </summary> | ||
/// <param name="argument">The argument.</param> | ||
/// <param name="message"> | ||
/// The message of the exception that will be thrown if the precondition is not satisfied. | ||
/// </param> | ||
/// <returns><paramref name="argument" />.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is not | ||
/// <see cref="DateTimeKind.Unspecified" />. | ||
/// </exception> | ||
[AssertionMethod] | ||
[DebuggerStepThrough] | ||
[GuardFunction("DateTime", "gtku")] | ||
public static ref readonly ArgumentInfo<DateTime?> KindUnspecified( | ||
in this ArgumentInfo<DateTime?> argument, Func<DateTime, string>? message = null) | ||
{ | ||
if (argument.HasValue()) | ||
{ | ||
var value = argument.GetValueOrDefault(); | ||
if (value.Kind != DateTimeKind.Unspecified) | ||
{ | ||
var m = message?.Invoke(value) ?? Messages.KindUnspecified(argument); | ||
throw Fail(new ArgumentException(m, argument.Name)); | ||
} | ||
} | ||
|
||
return ref argument; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.