This commit is contained in:
+94
-11
@@ -4,6 +4,83 @@ namespace Just.Railway;
|
||||
|
||||
public static partial class ResultExtensions
|
||||
{
|
||||
#region MapError
|
||||
|
||||
public static Result MapError(this in Result result, Func<Error, Error> func) => result.State switch
|
||||
{
|
||||
ResultState.Error => func(result.Error!),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
public static Result<T> MapError<T>(this in Result<T> result, Func<Error, Error> func) => result.State switch
|
||||
{
|
||||
ResultState.Error => func(result.Error!),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
|
||||
public static async Task<Result> MapError(this Task<Result> resultTask, Func<Error, Error> func)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
return result.State switch
|
||||
{
|
||||
ResultState.Error => func(result.Error!),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
}
|
||||
public static async Task<Result> MapError(this Result result, Func<Error, Task<Error>> func)
|
||||
{
|
||||
return result.State switch
|
||||
{
|
||||
ResultState.Error => await func(result.Error!).ConfigureAwait(false),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
}
|
||||
public static async Task<Result> MapError(this Task<Result> resultTask, Func<Error, Task<Error>> func)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
return result.State switch
|
||||
{
|
||||
ResultState.Error => await func(result.Error!),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
}
|
||||
|
||||
public static async Task<Result<T>> MapError<T>(this Task<Result<T>> resultTask, Func<Error, Error> func)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
return result.State switch
|
||||
{
|
||||
ResultState.Error => func(result.Error!),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
}
|
||||
public static async Task<Result<T>> MapError<T>(this Result<T> result, Func<Error, Task<Error>> func)
|
||||
{
|
||||
return result.State switch
|
||||
{
|
||||
ResultState.Error => await func(result.Error!).ConfigureAwait(false),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
}
|
||||
public static async Task<Result<T>> MapError<T>(this Task<Result<T>> resultTask, Func<Error, Task<Error>> func)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
return result.State switch
|
||||
{
|
||||
ResultState.Error => await func(result.Error!),
|
||||
ResultState.Success => result,
|
||||
_ => throw new ResultNotInitializedException(nameof(result)),
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Finally
|
||||
|
||||
public static Result Finally(this in Result result, Action action)
|
||||
@@ -142,20 +219,23 @@ public static partial class ResultExtensions
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
break;
|
||||
continue;
|
||||
|
||||
case ResultState.Error:
|
||||
errors ??= ImmutableArray.CreateBuilder<Error>();
|
||||
ManyErrors.AppendSanitized(errors, result.Error!);
|
||||
break;
|
||||
continue;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(results));
|
||||
}
|
||||
}
|
||||
|
||||
return errors is null
|
||||
? new(null)
|
||||
: new(new ManyErrors(errors.ToImmutable()));
|
||||
|
||||
return errors switch
|
||||
{
|
||||
null => new Result(null),
|
||||
{ Count: 1 } => errors[0],
|
||||
_ => new(new ManyErrors(errors.ToImmutable()))
|
||||
};
|
||||
}
|
||||
public static async Task<Result> Merge(this IEnumerable<Task<Result>> tasks)
|
||||
{
|
||||
@@ -192,20 +272,23 @@ public static partial class ResultExtensions
|
||||
case ResultState.Success:
|
||||
values ??= ImmutableArray.CreateBuilder<T>();
|
||||
values.Add(result.Value);
|
||||
break;
|
||||
continue;
|
||||
|
||||
case ResultState.Error:
|
||||
errors ??= ImmutableArray.CreateBuilder<Error>();
|
||||
ManyErrors.AppendSanitized(errors, result.Error!);
|
||||
break;
|
||||
continue;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(results));
|
||||
}
|
||||
}
|
||||
|
||||
return errors is null
|
||||
? new(values?.ToImmutable() ?? Enumerable.Empty<T>())
|
||||
: new(new ManyErrors(errors.ToImmutable()));
|
||||
return errors switch
|
||||
{
|
||||
null => new Result<IEnumerable<T>>(values?.ToImmutable() ?? Enumerable.Empty<T>()),
|
||||
{ Count: 1 } => errors[0],
|
||||
_ => new(new ManyErrors(errors.ToImmutable()))
|
||||
};
|
||||
}
|
||||
public static async Task<Result<IEnumerable<T>>> Merge<T>(this IEnumerable<Task<Result<T>>> tasks)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user