using System; namespace FruityFoundation.Base.Structures; public readonly record struct Result { private readonly Maybe _successVal; private readonly Maybe _failureVal; private Result(Maybe successVal, Maybe failureVal) { _successVal = successVal; _failureVal = failureVal; } public static Result CreateSuccess(TSuccess val) => new(successVal: val, failureVal: Maybe.Empty()); public static Result CreateFailure(TFailure val) => new(successVal: Maybe.Empty(), failureVal: val); public bool IsSuccess => _successVal.HasValue; public bool IsFailure => _failureVal.HasValue; public TSuccess SuccessVal => _successVal.Value; public TFailure FailureVal => _failureVal.Value; 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(Func onSuccess, Func onFailure) => IsSuccess ? onSuccess(_successVal.Value) : onFailure(_failureVal.Value); }