Class ResultFilterExtensions

Namespace
REslava.Result.Extensions
Assembly
REslava.Result.dll

Extension methods for filtering Result<TValue> by a predicate on the success value.

public static class ResultFilterExtensions
Inheritance
ResultFilterExtensions
Inherited Members

Methods

FilterAsync<T>(Result<T>, Func<T, Task<bool>>, Func<T, IError>, CancellationToken)

Asynchronously filters a result by an async predicate on the success value. Converts a successful result to a failure when predicate returns false. If the result is already a failure, it is returned unchanged.

public static Task<Result<T>> FilterAsync<T>(this Result<T> result, Func<T, Task<bool>> predicate, Func<T, IError> errorFactory, CancellationToken cancellationToken = default)

Parameters

result Result<T>

The result to filter.

predicate Func<T, Task<bool>>

The async condition the value must satisfy.

errorFactory Func<T, IError>

Produces the error when the predicate fails; receives the value.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task<Result<T>>

The original result if successful and predicate passes; otherwise a failed result.

Type Parameters

T

The result value type.

Examples

Result<Order> valid = await orderResult
    .FilterAsync(async o => await validator.IsValidAsync(o),
                 o => new ValidationError("Order", o.Id.ToString(), "failed validation"));

FilterAsync<T>(Task<Result<T>>, Func<T, Task<bool>>, Func<T, IError>, CancellationToken)

Awaits the result task and filters the result by an async predicate on the success value.

public static Task<Result<T>> FilterAsync<T>(this Task<Result<T>> resultTask, Func<T, Task<bool>> predicate, Func<T, IError> errorFactory, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>
predicate Func<T, Task<bool>>
errorFactory Func<T, IError>
cancellationToken CancellationToken

Returns

Task<Result<T>>

Type Parameters

T

Filter<T>(Result<T>, Func<T, bool>, IError)

Converts a successful result to a failure when predicate returns false. If the result is already a failure, it is returned unchanged.

public static Result<T> Filter<T>(this Result<T> result, Func<T, bool> predicate, IError error)

Parameters

result Result<T>

The result to filter.

predicate Func<T, bool>

The condition the value must satisfy.

error IError

The error to use when the predicate fails.

Returns

Result<T>

Type Parameters

T

Filter<T>(Result<T>, Func<T, bool>, Func<T, IError>)

Converts a successful result to a failure when predicate returns false. The error is built from the value, enabling contextual error messages. If the result is already a failure, it is returned unchanged.

public static Result<T> Filter<T>(this Result<T> result, Func<T, bool> predicate, Func<T, IError> errorFactory)

Parameters

result Result<T>

The result to filter.

predicate Func<T, bool>

The condition the value must satisfy.

errorFactory Func<T, IError>

Produces the error when the predicate fails; receives the value.

Returns

Result<T>

The original result if successful and predicate passes; otherwise a failed result.

Type Parameters

T

The result value type.

Examples

Result<User> activeUser = userResult
    .Filter(u => u.IsActive, u => new Error($"User '{u.Name}' is not active."));

Filter<T>(Result<T>, Func<T, bool>, string)

Converts a successful result to a failure when predicate returns false. If the result is already a failure, it is returned unchanged.

public static Result<T> Filter<T>(this Result<T> result, Func<T, bool> predicate, string errorMessage)

Parameters

result Result<T>

The result to filter.

predicate Func<T, bool>

The condition the value must satisfy.

errorMessage string

The error message to use when the predicate fails.

Returns

Result<T>

Type Parameters

T

Filter<T>(Task<Result<T>>, Func<T, bool>, IError, CancellationToken)

Awaits the result task and filters the result by a predicate on the success value.

public static Task<Result<T>> Filter<T>(this Task<Result<T>> resultTask, Func<T, bool> predicate, IError error, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>
predicate Func<T, bool>
error IError
cancellationToken CancellationToken

Returns

Task<Result<T>>

Type Parameters

T

Filter<T>(Task<Result<T>>, Func<T, bool>, Func<T, IError>, CancellationToken)

Awaits the result task and filters the result by a predicate on the success value.

public static Task<Result<T>> Filter<T>(this Task<Result<T>> resultTask, Func<T, bool> predicate, Func<T, IError> errorFactory, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>
predicate Func<T, bool>
errorFactory Func<T, IError>
cancellationToken CancellationToken

Returns

Task<Result<T>>

Type Parameters

T