Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions src/Octokit.Webhooks/Extensions/StringEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Octokit.Webhooks.Extensions;
using JetBrains.Annotations;

[PublicAPI]
public readonly struct StringEnum<TEnum> : IEquatable<StringEnum<TEnum>>
public sealed record StringEnum<TEnum> : IEquatable<StringEnum<TEnum>>
where TEnum : struct, Enum
{
private readonly TEnum? parsedValue;
Expand Down Expand Up @@ -49,23 +49,6 @@ public StringEnum(TEnum parsedValue)

public static implicit operator StringEnum<TEnum>(TEnum parsedValue) => new(parsedValue);

public static bool operator ==(StringEnum<TEnum>? left, StringEnum<TEnum>? right)
{
if (left is null && right is null)
{
return true;
}

if (left is null || right is null)
{
return false;
}

return left.Value.Equals(right.Value);
}

public static bool operator !=(StringEnum<TEnum>? left, StringEnum<TEnum>? right) => !(left == right);

public bool TryParse([NotNullWhen(true)] out TEnum? value)
{
if (this.isValidEnum && this.parsedValue is not null)
Expand All @@ -78,20 +61,20 @@ public bool TryParse([NotNullWhen(true)] out TEnum? value)
return false;
}

public override bool Equals(object? obj) => obj is StringEnum<TEnum> other && this.Equals(other);

public bool Equals(StringEnum<TEnum> other)
public bool Equals(StringEnum<TEnum>? other)
{
var canParseThis = this.TryParse(out var thisValue);
var canParseOther = other.TryParse(out var otherValue);

TEnum? otherValue = null;
var canParseOther = other != null && other.TryParse(out otherValue);

return canParseThis switch
{
// If both can be parsed to enum values, compare the enum values
true when canParseOther => thisValue.Equals(otherValue),

// If neither can be parsed, compare string values
false when !canParseOther => this.StringValue.Equals(other.StringValue, StringComparison.OrdinalIgnoreCase),
false when !canParseOther => this.StringValue.Equals(other?.StringValue, StringComparison.OrdinalIgnoreCase),

// If one can parse and one cannot, they're not equal
_ => false,
Expand Down
Loading