-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switched to structs + interface for errors
- Loading branch information
Showing
2 changed files
with
108 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
#nullable enable | ||
using System; | ||
|
||
namespace Basis.Contrib.Auth.DecentralizedIds.Result | ||
namespace Basis.Contrib.Auth.DecentralizedIds | ||
{ | ||
public readonly struct Success { } | ||
|
||
/// Analagous to rust's Result type. | ||
public abstract record Result<T, E> | ||
public readonly struct Result<T, E> | ||
{ | ||
private Result() { } | ||
private readonly bool isOk; | ||
private readonly T? ok; | ||
private readonly E? err; | ||
|
||
private Result(T? ok, E? err, bool isOk) | ||
{ | ||
this.ok = ok; | ||
this.err = err; | ||
this.isOk = isOk; | ||
} | ||
|
||
public bool IsOk => isOk; | ||
public bool IsErr => !isOk; | ||
|
||
public T GetOk => ok ?? throw new InvalidVariantExeption(); | ||
|
||
public E GetErr => err ?? throw new InvalidVariantExeption(); | ||
|
||
public sealed record Ok(T Ok) : Result<T, E> { } | ||
public static Result<T, E> Ok(T v) | ||
{ | ||
return new(v, default, true); | ||
} | ||
|
||
public sealed record Err(E Err) : Result<T, E> { } | ||
public static Result<T, E> Err(E e) | ||
{ | ||
return new(default, e, false); | ||
} | ||
|
||
public static implicit operator Result<T, E>(T v) => new(v, default, true); | ||
|
||
public static implicit operator Result<T, E>(E e) => new(default, e, false); | ||
} | ||
|
||
public class InvalidVariantExeption : System.Exception | ||
{ | ||
public InvalidVariantExeption() | ||
: base("wrong result variant") { } | ||
} | ||
} |