// Copyright (c) The Geekeey Authors // SPDX-License-Identifier: EUPL-1.2 using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Numerics; namespace Geekeey.Request.Result; public partial class Result : IEquatable { /// [Pure] public bool Equals(Result? other) { return Equals(this, other); } /// [Pure] public override bool Equals(object? obj) { return obj is Result r && Equals(r); } /// [Pure] public override int GetHashCode() { return GetHashCode(this); } internal static bool Equals(Result? a, Result? b) { if (a is null || b is null) { return a is null && b is null; } return a.IsSuccess == b.IsSuccess; } internal static int GetHashCode(Result? result) { return result?.IsSuccess.GetHashCode() ?? 0; } } public partial class Result : IEqualityOperators { /// /// Checks whether two results are equal. Results are equal if they are both success or both failure. /// /// The first result to compare. /// The second result to compare. [Pure] [ExcludeFromCodeCoverage] public static bool operator ==(Result? a, Result? b) { return Equals(a, b); } /// /// Checks whether two results are not equal. Results are equal if they are both success or both failure. /// /// The first result to compare. /// The second result to compare. [Pure] [ExcludeFromCodeCoverage] public static bool operator !=(Result? a, Result? b) { return !Equals(a, b); } } public partial class Result : IEquatable>, IEquatable { /// /// 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) { return 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) { return 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) { return 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) { return Equals(this, other, comparer); } /// [Pure] public override bool Equals(object? obj) { return (obj is T x && Equals(x)) || (obj is Result r && Equals(r)); } /// [Pure] public override int GetHashCode() { return GetHashCode(this, EqualityComparer.Default); } internal static bool Equals(Result? a, Result? b, IEqualityComparer comparer) { if (a is null || b is null) { return a is null && b is null; } if (!a.IsSuccess || !b.IsSuccess) { return !a.IsSuccess && !b.IsSuccess; } 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 is null) { return b is null; } if (!a.IsSuccess) { 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 { IsSuccess: true, Value: not null }) { return comparer.GetHashCode(result.Value); } return 0; } } public partial class Result : IEqualityOperators, Result, bool>, IEqualityOperators, T, bool> { /// /// 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) { return Equals(a, b, EqualityComparer.Default); } /// /// 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) { return !Equals(a, b, EqualityComparer.Default); } /// /// 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) { return Equals(a, b, EqualityComparer.Default); } /// /// 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) { return !Equals(a, b, EqualityComparer.Default); } }