2023-08-01 22:12:54 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace FruityFoundation.Base.Structures;
|
|
|
|
|
|
2024-05-03 17:06:55 -04:00
|
|
|
|
public readonly record struct Result<TSuccess, TFailure>
|
2023-08-01 22:12:54 -04:00
|
|
|
|
{
|
|
|
|
|
private readonly Maybe<TSuccess> _successVal;
|
|
|
|
|
private readonly Maybe<TFailure> _failureVal;
|
|
|
|
|
|
|
|
|
|
private Result(Maybe<TSuccess> successVal, Maybe<TFailure> failureVal)
|
|
|
|
|
{
|
|
|
|
|
_successVal = successVal;
|
|
|
|
|
_failureVal = failureVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Result<TSuccess, TFailure> CreateSuccess(TSuccess val) =>
|
|
|
|
|
new(successVal: val, failureVal: Maybe.Empty<TFailure>());
|
|
|
|
|
|
|
|
|
|
public static Result<TSuccess, TFailure> CreateFailure(TFailure val) =>
|
|
|
|
|
new(successVal: Maybe.Empty<TSuccess>(), failureVal: val);
|
|
|
|
|
|
|
|
|
|
public bool IsSuccess => _successVal.HasValue;
|
|
|
|
|
public bool IsFailure => _failureVal.HasValue;
|
|
|
|
|
|
2023-08-01 22:55:30 -04:00
|
|
|
|
public TSuccess SuccessVal => _successVal.Value;
|
|
|
|
|
|
|
|
|
|
public TFailure FailureVal => _failureVal.Value;
|
|
|
|
|
|
2023-08-01 22:12:54 -04:00
|
|
|
|
public bool TrySuccess(out TSuccess output)
|
|
|
|
|
{
|
|
|
|
|
if (!_successVal.Try(out output))
|
|
|
|
|
{
|
|
|
|
|
output = default!;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryFailure(out TFailure output)
|
|
|
|
|
{
|
|
|
|
|
if (!_failureVal.Try(out output))
|
|
|
|
|
{
|
|
|
|
|
output = default!;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Merge<T>(Func<TSuccess, T> onSuccess, Func<TFailure, T> onFailure) =>
|
2024-05-03 17:06:55 -04:00
|
|
|
|
IsSuccess
|
|
|
|
|
? onSuccess(_successVal.Value)
|
|
|
|
|
: onFailure(_failureVal.Value);
|
2023-08-01 22:12:54 -04:00
|
|
|
|
}
|