From 036b34d3c08fc85acb934195688e9fff110d1d52 Mon Sep 17 00:00:00 2001 From: JustFixMe Date: Fri, 8 Dec 2023 00:07:24 +0400 Subject: [PATCH] added basic Try extensions --- Railway/Result.cs | 26 ++++++++++- Railway/Try.cs | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 Railway/Try.cs diff --git a/Railway/Result.cs b/Railway/Result.cs index 7731858..bfcab13 100644 --- a/Railway/Result.cs +++ b/Railway/Result.cs @@ -38,9 +38,16 @@ public readonly partial struct Result : IEquatable [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)] 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)] 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)] public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error))); [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -54,6 +61,23 @@ public readonly partial struct Result : IEquatable [Pure] public bool IsSuccess => Error is 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) { if (IsSuccess) @@ -125,7 +149,7 @@ public readonly struct Result : IEquatable> [Pure] public bool IsSuccess => State == ResultState.Success; [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) { diff --git a/Railway/Try.cs b/Railway/Try.cs new file mode 100644 index 0000000..1d598c6 --- /dev/null +++ b/Railway/Try.cs @@ -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 Run(Func action) + { + try + { + await action().ConfigureAwait(false); + return Result.Success(); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + public static async ValueTask Run(Func action) + { + try + { + await action().ConfigureAwait(false); + return Result.Success(); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + public static Result Run(Func func) + { + try + { + return Result.Success(func()); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + public static async Task> Run(Func> func) + { + try + { + return Result.Success(await func().ConfigureAwait(false)); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + public static async ValueTask> Run(Func> func) + { + try + { + return Result.Success(await func().ConfigureAwait(false)); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + + public static Result Run(Func> func) + { + try + { + return func(); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + public static async Task> Run(Func>> func) + { + try + { + return await func().ConfigureAwait(false); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } + public static async ValueTask> Run(Func>> func) + { + try + { + return await func().ConfigureAwait(false); + } + catch (Exception ex) + { + return Result.Failure(ex); + } + } +}