using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Numerics; namespace Geekeey.Extensions.Result; public readonly partial struct Result : IEquatable>, IEquatable, IEqualityOperators, Result, bool>, IEqualityOperators, T, bool> { /// /// Checks whether the result is equal to another result. Results are equal if both results are success values and /// the success values are equal, or if both results are failures. /// /// The result to check for equality with the current result. [Pure] public bool Equals(Result other) => Equals(this, other, EqualityComparer.Default); /// /// Checks whether the result is equal to another result. Results are equal if both results are success values and /// the success values are equal, or if both results are failures. /// /// The result to check for equality with the current result. /// The equality comparer to use for comparing values. [Pure] public bool Equals(Result other, IEqualityComparer comparer) => Equals(this, other, comparer); /// /// Checks whether the result is a success value and the success value is equal to another value. /// /// The value to check for equality with the success value of the result. [Pure] public bool Equals(T? other) => Equals(this, other, EqualityComparer.Default); /// /// Checks whether the result is a success value and the success value is equal to another value using a specified /// equality comparer. /// /// The value to check for equality with the success value of the result. /// The equality comparer to use for comparing values. [Pure] public bool Equals(T? other, IEqualityComparer comparer) => Equals(this, other, comparer); /// [Pure] public override bool Equals(object? other) => other is T x && Equals(x) || other is Result r && Equals(r); /// [Pure] public override int GetHashCode() => GetHashCode(this, EqualityComparer.Default); internal static bool Equals(Result a, Result b, IEqualityComparer comparer) { if (!a._success || !b._success) return !a._success && !b._success; if (a.Value is null || b.Value is null) return a.Value is null && b.Value is null; return comparer.Equals(a.Value, b.Value); } internal static bool Equals(Result a, T? b, IEqualityComparer comparer) { if (!a._success) return false; if (a.Value is null || b is null) return a.Value is null && b is null; return comparer.Equals(a.Value, b); } internal static int GetHashCode(Result result, IEqualityComparer comparer) { if (result is { _success: true, Value: not null }) return comparer.GetHashCode(result.Value); return 0; } /// /// Checks whether two results are equal. Results are equal if both results are success values and the success /// values are equal, or if both results are failures. /// /// The first result to compare. /// The second result to compare. [Pure] [ExcludeFromCodeCoverage] public static bool operator ==(Result a, Result b) => a.Equals(b); /// /// Checks whether two results are not equal. Results are equal if both results are success values and the success /// values are equal, or if both results are failures. /// /// The first result to compare. /// The second result to compare. [Pure] [ExcludeFromCodeCoverage] public static bool operator !=(Result a, Result b) => !a.Equals(b); /// /// Checks whether a result is a success value and the success value is equal to another value. /// /// The result to compare. /// The value to check for equality with the success value in the result. [Pure] [ExcludeFromCodeCoverage] public static bool operator ==(Result a, T? b) => a.Equals(b); /// /// Checks whether a result either does not have a value, or the value is not equal to another value. /// /// The result to compare. /// The value to check for inequality with the success value in the result. [Pure] [ExcludeFromCodeCoverage] public static bool operator !=(Result a, T? b) => !a.Equals(b); }