Skip to content
Merged

NRT #34

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions src/SharedInfrastructure/DebugGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace SixLabors;
Expand All @@ -22,33 +23,25 @@ internal static partial class DebugGuard
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
[Conditional("DEBUG")]
public static void NotNull<TValue>(TValue value, string parameterName)
where TValue : class
{
if (value is null)
{
ThrowArgumentNullException(parameterName);
}
}
public static void NotNull<TValue>([NotNull] TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
where TValue : class =>
ArgumentNullException.ThrowIfNull(value, parameterName);

/// <summary>
/// Ensures that the target value is not null, empty, or whitespace.
/// </summary>
/// <param name="value">The target string, which should be checked against being null or empty.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <param name="paramName">Name of the parameter.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
[Conditional("DEBUG")]
public static void NotNullOrWhiteSpace(string value, string parameterName)
public static void NotNullOrWhiteSpace([NotNull] string? value, [CallerArgumentExpression("value")] string? paramName = null)
{
if (value is null)
{
ThrowArgumentNullException(parameterName);
}
ArgumentNullException.ThrowIfNull(value);

if (string.IsNullOrWhiteSpace(value))
{
ThrowArgumentException("Must not be empty or whitespace.", parameterName);
ThrowArgumentException("Must not be empty or whitespace.", paramName!);
}
}

Expand Down Expand Up @@ -155,7 +148,9 @@ public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TVal
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
ThrowArgumentOutOfRangeException(
parameterName,
$"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
}
}

Expand Down Expand Up @@ -282,8 +277,4 @@ private static void ThrowArgumentException(string message, string parameterName)
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentOutOfRangeException(string parameterName, string message) =>
throw new ArgumentOutOfRangeException(parameterName, message);

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentNullException(string parameterName) =>
throw new ArgumentNullException(parameterName);
}
14 changes: 4 additions & 10 deletions src/SharedInfrastructure/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace SixLabors;
Expand All @@ -20,16 +21,9 @@ internal static partial class Guard
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void NotNull<TValue>(TValue value, string parameterName)
where TValue : class
{
if (value is not null)
{
return;
}

ThrowHelper.ThrowArgumentNullExceptionForNotNull(parameterName);
}
public static void NotNull<TValue>([NotNull]TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
where TValue : class =>
ArgumentNullException.ThrowIfNull(value, parameterName);

/// <summary>
/// Ensures that the target value is not null, empty, or whitespace.
Expand Down
10 changes: 1 addition & 9 deletions src/SharedInfrastructure/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ namespace SixLabors;
internal static partial class ThrowHelper
#pragma warning restore RCS1043 // Remove 'partial' modifier from type with a single part.
{
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> when <see cref="Guard.NotNull{TValue}"/> fails.
/// </summary>
/// <param name="name">The argument name.</param>
[DoesNotReturn]
public static void ThrowArgumentNullExceptionForNotNull(string name)
=> ThrowArgumentNullException(name, $"Parameter \"{name}\" must be not null.");

/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.NotNullOrWhiteSpace"/> fails.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="name">The argument name.</param>
[DoesNotReturn]
public static void ThrowArgumentExceptionForNotNullOrWhitespace(string value, string name)
public static void ThrowArgumentExceptionForNotNullOrWhitespace(string? value, string name)
{
if (value is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net6.0;</TargetFrameworks>
<AssemblyName>SharedInfrastructure.Tests</AssemblyName>
<RootNamespace>SharedInfrastructure.Tests</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down