#region ========================================================================= USING =====================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Domain.Common.Primitives;
///
/// Primitive struct for explicitly handling success and failure cases in a functional manner.
///
/// The type of the success value.
public readonly struct Result : IEquatable>
{
private readonly TValue? _value;
private readonly List? _errors;
///
/// Gets a value indicating whether this result represents a success.
///
public bool IsSuccess { get; }
///
/// Gets a value indicating whether this result represents a failure.
///
public bool IsFailure => !IsSuccess;
///
/// Gets the success value. Only valid when IsSuccess is true.
///
public TValue Value => IsSuccess ? _value! : throw new InvalidOperationException("Cannot access Value of a failed result.");
///
/// Gets the list of errors. Only valid when IsFailure is true.
///
public List Errors => IsFailure
? _errors ?? []
: throw new InvalidOperationException("Cannot access Errors of a successful result.");
///
/// Returns the first error, if present.
///
public Error FirstError => Errors[0];
///
/// Initializes a new instance of the structure.
///
/// The success value.
/// The list of errors.
/// Whether this represents a success or failure.
private Result(TValue? value, List? errors, bool isSuccess)
{
_value = value;
_errors = errors;
IsSuccess = isSuccess;
}
///
/// Creates a successful result with the specified value.
///
/// The success value.
/// A successful result containing the specified value.
public static Result Success(TValue value)
{
return new Result(value, default, true);
}
///
/// Creates a failed result with the specified errors.
///
/// The errors value.
/// A failed result containing the specified error.
public static Result Failure(List errors)
{
return new Result(default, errors, false);
}
///
/// Creates a failed result with the specified errors.
///
/// The errors value.
/// A failed result containing the specified error.
public static Result Failure(params Error[] errors)
{
return new(default, [.. errors], false);
}
///
/// Custom implementation operator for converting to a .
///
/// The value to be converted.
public static implicit operator Result(TValue value)
{
return Success(value);
}
///
/// Custom implementation operator for converting to a failed .
///
/// The error to be converted.
public static implicit operator Result(Error error)
{
return Failure(error);
}
///
/// Custom implementation operator for converting to a failed .
///
/// The errors to be converted.
public static implicit operator Result(List errors)
{
return Failure(errors);
}
///
/// Maps the success value to a new type using the provided function.
///
/// The type of the new success value.
/// The function to transform the success value.
/// A new result with the transformed value, or the original errors.
public Result Map(Func mapper)
{
return IsSuccess
? Result.Success(mapper(Value))
: Result.Failure(Errors);
}
///
/// Chains another operation that returns a Result, allowing for monadic composition.
///
/// The type of the new success value.
/// The function that returns a new Results.
/// The result of the chained operation, or the original error.
public Result Bind(Func> binder)
{
return IsSuccess ? binder(Value) : Result.Failure(Errors);
}
///
/// Executes an action if the result is successful, otherwise does nothing.
///
/// The action to execute on success.
/// The original result for chaining.
public Result OnSuccess(Action action)
{
if (IsSuccess)
action(Value);
return this;
}
///
/// Executes an action if the result is a failure, otherwise does nothing.
///
/// The action to execute on failure.
/// The original result for chaining.
public Result OnFailure(Action> action)
{
if (IsFailure)
action(Errors);
return this;
}
///
/// Matches the result and executes the appropriate function.
///
/// The type of the result.
/// Function to execute on success.
/// Function to execute on failure.
/// The result of the executed function.
public TResult Match(Func onSuccess, Func, TResult> onFailure)
{
return IsSuccess ? onSuccess(Value) : onFailure(Errors);
}
///
/// Determines whether the specified Result is equal to the current Results.
///
/// The Result to compare with the current Results.
/// true if the specified Result is equal to the current Result; otherwise, false.
public bool Equals(Result other)
{
if (IsSuccess != other.IsSuccess)
return false;
return IsSuccess
? EqualityComparer.Default.Equals(Value, other.Value)
: Errors.SequenceEqual(other.Errors);
}
///
/// Determines whether the specified object is equal to the current Results.
///
/// The object to compare with the current Results.
/// true if the specified object is equal to the current Result; otherwise, false.
public override bool Equals(object? obj)
{
return obj is Result other && Equals(other);
}
///
/// Determines whether two specified Result instances are equal.
///
/// The first Result to compare.
/// The second Result to compare.
/// true if the two Result instances are equal; otherwise, false.
public static bool operator ==(Result left, Result right)
{
return left.Equals(right);
}
///
/// Determines whether two specified Result instances are not equal.
///
/// The first Result to compare.
/// The second Result to compare.
/// true if the two Result instances are not equal; otherwise, false.
public static bool operator !=(Result left, Result right)
{
return !(left == right);
}
///
/// Returns the hash code for this Results.
///
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return IsSuccess
? HashCode.Combine(IsSuccess, Value)
: HashCode.Combine(IsSuccess, Errors);
}
///
/// Customized ToString() method.
///
/// Custom string value showing relevant data for current result.
public override string ToString()
{
return IsSuccess
? $"Success: {Value?.ToString()}"
: $"Failure: [{string.Join(", ", Errors.Select(error => error.ToString()))}]";
}
}