| | | 1 | | namespace Anichron.API.Services; |
| | | 2 | | |
| | | 3 | | public sealed class ServiceResult<T, TError> |
| | | 4 | | where TError : class, IServiceError |
| | | 5 | | { |
| | | 6 | | private readonly T? value; |
| | | 7 | | private readonly TError? error; |
| | | 8 | | |
| | | 9 | | private ServiceResult(T? value, TError? error) |
| | | 10 | | { |
| | | 11 | | this.value = value; |
| | | 12 | | this.error = error; |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | public bool IsSuccess => error is null; |
| | | 16 | | |
| | | 17 | | public static implicit operator ServiceResult<T, TError>(T value) |
| | | 18 | | { |
| | | 19 | | return new(value, null); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public static implicit operator ServiceResult<T, TError>(TError error) |
| | | 23 | | { |
| | | 24 | | return new(default, error); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public TResult Match<TResult>(Func<T, TResult> ok, Func<TError, TResult> failed) |
| | | 28 | | => error is null ? ok(value!) : failed(error); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public static class ServiceResult |
| | | 32 | | { |
| | | 33 | | public static ServiceResult<T, TError> Fail<T, TError>(TError error) |
| | | 34 | | where TError : class, IServiceError |
| | 40 | 35 | | => error; |
| | | 36 | | } |