This commit is contained in:
@@ -38,9 +38,16 @@ public readonly partial struct Result : IEquatable<Result>
|
|||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Result Failure(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
public static Result Failure(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result Failure(Exception exception) => new(Error.New(exception) ?? throw new ArgumentNullException(nameof(exception)));
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Result<T> Failure<T>(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
public static Result<T> Failure<T>(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<T> Failure<T>(Exception exception) => new(Error.New(exception) ?? throw new ArgumentNullException(nameof(exception)));
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -54,6 +61,23 @@ public readonly partial struct Result : IEquatable<Result>
|
|||||||
[Pure] public bool IsSuccess => Error is null;
|
[Pure] public bool IsSuccess => Error is null;
|
||||||
[Pure] public bool IsFailure => Error is not null;
|
[Pure] public bool IsFailure => Error is not null;
|
||||||
|
|
||||||
|
[Pure] public bool Success([MaybeNullWhen(false)]out SuccessUnit? u, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
||||||
|
{
|
||||||
|
switch (State)
|
||||||
|
{
|
||||||
|
case ResultState.Success:
|
||||||
|
u = new SuccessUnit();
|
||||||
|
error = default;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case ResultState.Error:
|
||||||
|
u = default;
|
||||||
|
error = Error!;
|
||||||
|
return false;
|
||||||
|
|
||||||
|
default: throw new ResultNotInitializedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
[Pure] public bool TryGetError([MaybeNullWhen(false)]out Error error)
|
[Pure] public bool TryGetError([MaybeNullWhen(false)]out Error error)
|
||||||
{
|
{
|
||||||
if (IsSuccess)
|
if (IsSuccess)
|
||||||
@@ -125,7 +149,7 @@ public readonly struct Result<T> : IEquatable<Result<T>>
|
|||||||
[Pure] public bool IsSuccess => State == ResultState.Success;
|
[Pure] public bool IsSuccess => State == ResultState.Success;
|
||||||
[Pure] public bool IsFailure => State == ResultState.Error;
|
[Pure] public bool IsFailure => State == ResultState.Error;
|
||||||
|
|
||||||
[Pure] public bool Unwrap([MaybeNullWhen(false)]out T value, [MaybeNullWhen(true)]out Error error)
|
[Pure] public bool Success([MaybeNullWhen(false)]out T value, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
||||||
{
|
{
|
||||||
switch (State)
|
switch (State)
|
||||||
{
|
{
|
||||||
|
|||||||
108
Railway/Try.cs
Normal file
108
Railway/Try.cs
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
public static class Try
|
||||||
|
{
|
||||||
|
public static Result Run(Action action)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
action();
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<Result> Run(Func<Task> action)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await action().ConfigureAwait(false);
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async ValueTask<Result> Run(Func<ValueTask> action)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await action().ConfigureAwait(false);
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static Result<T> Run<T>(Func<T> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Result.Success(func());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<Result<T>> Run<T>(Func<Task<T>> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Result.Success(await func().ConfigureAwait(false));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async ValueTask<Result<T>> Run<T>(Func<ValueTask<T>> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Result.Success(await func().ConfigureAwait(false));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result<T> Run<T>(Func<Result<T>> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return func();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<Result<T>> Run<T>(Func<Task<Result<T>>> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await func().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async ValueTask<Result<T>> Run<T>(Func<ValueTask<Result<T>>> func)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await func().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Failure<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user