fix: add checks for conditional compilation

This commit is contained in:
Stevan Freeborn
2026-03-30 17:25:21 -05:00
parent c0c62b9dc6
commit e1974948ef
2 changed files with 248 additions and 124 deletions
+88 -44
View File
@@ -53,15 +53,23 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when func or errorHandler is null.</exception>
public static Result<T, TError> Try<T, TError>(Func<T> func, Func<Exception, TError> errorHandler) where TError : IError
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(func);
#else
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
#endif
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(errorHandler);
#else
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
#endif
try
{
@@ -142,10 +150,14 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when mapper is null.</exception>
public Result<TNew, TError> Map<TNew>(Func<T, TNew> mapper)
{
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(mapper);
#else
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#endif
return IsSuccess ? Result.Ok<TNew, TError>(mapper(Value)) : Result.Fail<TNew, TError>(Error);
}
@@ -158,10 +170,14 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when mapper is null.</exception>
public Result<T, TNewError> MapError<TNewError>(Func<TError, TNewError> mapper) where TNewError : IError
{
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(mapper);
#else
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#endif
return IsFailure ? Result.Fail<T, TNewError>(mapper(Error)) : Result.Ok<T, TNewError>(Value);
}
@@ -175,10 +191,14 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when binder is null.</exception>
public Result<TNew, TError> Bind<TNew>(Func<T, Result<TNew, TError>> binder)
{
if (binder is null)
{
throw new ArgumentNullException(nameof(binder));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(binder);
#else
if (binder is null)
{
throw new ArgumentNullException(nameof(binder));
}
#endif
return IsSuccess ? binder(Value) : Result.Fail<TNew, TError>(Error);
}
@@ -193,15 +213,23 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when onSuccess or onFailure is null.</exception>
public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<TError, TResult> onFailure)
{
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onSuccess);
#else
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#endif
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onFailure);
#else
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#endif
return IsSuccess ? onSuccess(Value) : onFailure(Error);
}
@@ -214,15 +242,23 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when onSuccess or onFailure is null.</exception>
public void Match(Action<T> onSuccess, Action<TError> onFailure)
{
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onSuccess);
#else
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#endif
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onFailure);
#else
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#endif
if (IsSuccess)
{
@@ -242,10 +278,14 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when action is null.</exception>
public Result<T, TError> Map(Action<T> action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(action);
#else
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
#endif
if (IsSuccess)
{
@@ -262,10 +302,14 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when action is null.</exception>
public Result<T, TError> Map(Action action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(action);
#else
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
#endif
if (IsSuccess)
{
@@ -19,15 +19,23 @@ namespace StevanFreeborn.Results
public static async Task<Result<TNew, TError>> MapAsync<T, TNew, TError>(this Result<T, TError> result, Func<T, Task<TNew>> mapper)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(mapper);
#else
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#endif
return result.IsSuccess ? await mapper(result.Value).ConfigureAwait(false) : result.Error;
}
@@ -46,15 +54,23 @@ namespace StevanFreeborn.Results
where TError : IError
where TNewError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(mapper);
#else
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
#endif
return result.IsFailure ? await mapper(result.Error).ConfigureAwait(false) : Result.Ok<T, TNewError>(result.Value);
}
@@ -70,15 +86,23 @@ namespace StevanFreeborn.Results
public static async Task<Result<Unit, TError>> MapAsync<TError>(this Result<Unit, TError> result, Func<Unit, Task> onSuccess)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onSuccess);
#else
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#endif
if (result.IsSuccess)
{
@@ -99,15 +123,23 @@ namespace StevanFreeborn.Results
public static async Task<Result<Unit, TError>> BindAsync<TError>(this Result<Unit, TError> result, Func<Unit, Task<Result<Unit, TError>>> onSuccess)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onSuccess);
#else
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#endif
return result.IsSuccess ? await onSuccess(result.Value).ConfigureAwait(false) : result;
}
@@ -125,20 +157,32 @@ namespace StevanFreeborn.Results
public static async Task<TResult> MatchAsync<TResult, TError>(this Result<Unit, TError> result, Func<Unit, Task<TResult>> onSuccess, Func<TError, Task<TResult>> onFailure)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onSuccess);
#else
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#endif
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onFailure);
#else
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#endif
return result.IsSuccess
? await onSuccess(result.Value).ConfigureAwait(false)
@@ -164,15 +208,23 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when action or errorHandler is null.</exception>
public static async Task<Result<Unit, Error>> TryAsync(Func<Task> action, Func<Exception, Error> errorHandler)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(action);
#else
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
#endif
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(errorHandler);
#else
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
#endif
try
{
@@ -198,15 +250,23 @@ namespace StevanFreeborn.Results
public static async Task<Result<TNew, TError>> BindAsync<T, TNew, TError>(this Result<T, TError> result, Func<T, Task<Result<TNew, TError>>> binder)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (binder is null)
{
throw new ArgumentNullException(nameof(binder));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(binder);
#else
if (binder is null)
{
throw new ArgumentNullException(nameof(binder));
}
#endif
return result.IsSuccess ? await binder(result.Value).ConfigureAwait(false) : Result.Fail<TNew, TError>(result.Error);
}
@@ -225,20 +285,32 @@ namespace StevanFreeborn.Results
public static async Task<TResult> MatchAsync<T, TResult, TError>(this Result<T, TError> result, Func<T, Task<TResult>> onSuccess, Func<TError, Task<TResult>> onFailure)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
#endif
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onSuccess);
#else
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
#endif
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(onFailure);
#else
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
#endif
return result.IsSuccess
? await onSuccess(result.Value).ConfigureAwait(false)
@@ -266,15 +338,23 @@ namespace StevanFreeborn.Results
/// <exception cref="ArgumentNullException">Thrown when func or errorHandler is null.</exception>
public static async Task<Result<T, Error>> TryAsync<T>(Func<Task<T>> func, Func<Exception, Error> errorHandler)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(func);
#else
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
#endif
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(errorHandler);
#else
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
#endif
try
{