request/src/request.result/Result.Equality.cs

239 lines
6.4 KiB
C#
Raw Normal View History

// 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<Result>
{
/// <inheritdoc/>
[Pure]
public bool Equals(Result? other)
{
return Equals(this, other);
}
/// <inheritdoc/>
[Pure]
public override bool Equals(object? obj)
{
return obj is Result r && Equals(r);
}
/// <inheritdoc/>
[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<Result, Result, bool>
{
/// <summary>
/// Checks whether two results are equal. Results are equal if they are both success or both failure.
/// </summary>
/// <param name="a">The first result to compare.</param>
/// <param name="b">The second result to compare.</param>
[Pure]
[ExcludeFromCodeCoverage]
public static bool operator ==(Result? a, Result? b)
{
return Equals(a, b);
}
/// <summary>
/// Checks whether two results are not equal. Results are equal if they are both success or both failure.
/// </summary>
/// <param name="a">The first result to compare.</param>
/// <param name="b">The second result to compare.</param>
[Pure]
[ExcludeFromCodeCoverage]
public static bool operator !=(Result? a, Result? b)
{
return !Equals(a, b);
}
}
public partial class Result<T> : IEquatable<Result<T>>, IEquatable<T>
{
/// <summary>
/// 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.
/// </summary>
/// <param name="other">The result to check for equality with the current result.</param>
[Pure]
public bool Equals(Result<T>? other)
{
return Equals(this, other, EqualityComparer<T>.Default);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="other">The result to check for equality with the current result.</param>
/// <param name="comparer">The equality comparer to use for comparing values.</param>
[Pure]
public bool Equals(Result<T> other, IEqualityComparer<T> comparer)
{
return Equals(this, other, comparer);
}
/// <summary>
/// Checks whether the result is a success value and the success value is equal to another value.
/// </summary>
/// <param name="other">The value to check for equality with the success value of the result.</param>
[Pure]
public bool Equals(T? other)
{
return Equals(this, other, EqualityComparer<T>.Default);
}
/// <summary>
/// Checks whether the result is a success value and the success value is equal to another value using a specified
/// equality comparer.
/// </summary>
/// <param name="other">The value to check for equality with the success value of the result.</param>
/// <param name="comparer">The equality comparer to use for comparing values.</param>
[Pure]
public bool Equals(T? other, IEqualityComparer<T> comparer)
{
return Equals(this, other, comparer);
}
/// <inheritdoc/>
[Pure]
public override bool Equals(object? obj)
{
return (obj is T x && Equals(x)) || (obj is Result<T> r && Equals(r));
}
/// <inheritdoc/>
[Pure]
public override int GetHashCode()
{
return GetHashCode(this, EqualityComparer<T>.Default);
}
internal static bool Equals(Result<T>? a, Result<T>? b, IEqualityComparer<T> 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<T>? a, T? b, IEqualityComparer<T> 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<T> result, IEqualityComparer<T> comparer)
{
if (result is { IsSuccess: true, Value: not null })
{
return comparer.GetHashCode(result.Value);
}
return 0;
}
}
public partial class Result<T> : IEqualityOperators<Result<T>, Result<T>, bool>, IEqualityOperators<Result<T>, T, bool>
{
/// <summary>
/// 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.
/// </summary>
/// <param name="a">The first result to compare.</param>
/// <param name="b">The second result to compare.</param>
[Pure]
[ExcludeFromCodeCoverage]
public static bool operator ==(Result<T>? a, Result<T>? b)
{
return Equals(a, b, EqualityComparer<T>.Default);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="a">The first result to compare.</param>
/// <param name="b">The second result to compare.</param>
[Pure]
[ExcludeFromCodeCoverage]
public static bool operator !=(Result<T>? a, Result<T>? b)
{
return !Equals(a, b, EqualityComparer<T>.Default);
}
/// <summary>
/// Checks whether a result is a success value and the success value is equal to another value.
/// </summary>
/// <param name="a">The result to compare.</param>
/// <param name="b">The value to check for equality with the success value in the result.</param>
[Pure]
[ExcludeFromCodeCoverage]
public static bool operator ==(Result<T>? a, T? b)
{
return Equals(a, b, EqualityComparer<T>.Default);
}
/// <summary>
/// Checks whether a result either does not have a value, or the value is not equal to another value.
/// </summary>
/// <param name="a">The result to compare.</param>
/// <param name="b">The value to check for inequality with the success value in the result.</param>
[Pure]
[ExcludeFromCodeCoverage]
public static bool operator !=(Result<T>? a, T? b)
{
return !Equals(a, b, EqualityComparer<T>.Default);
}
}