| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.Generators.Models; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A normalized constructor guard to emit for a generated-constructor parameter, |
| | | 8 | | /// either a built-in kind or a resolved custom guard type and method call. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Implements value equality so generated-constructor and record-overload models that |
| | | 12 | | /// contain arrays of guards remain cacheable across incremental generator runs. |
| | | 13 | | /// Positional literals forwarded from a parameterized alias are compared |
| | | 14 | | /// element-by-element rather than by array reference. |
| | | 15 | | /// </remarks> |
| | | 16 | | internal readonly struct ConstructorGuardModel : IEquatable<ConstructorGuardModel> |
| | | 17 | | { |
| | | 18 | | public ConstructorGuardModel( |
| | | 19 | | GeneratedConstructorGuardKind kind, |
| | | 20 | | string? customGuardTypeName, |
| | | 21 | | string? customGuardMethodName, |
| | | 22 | | string[]? forwardedArgumentLiterals) |
| | | 23 | | { |
| | 69 | 24 | | Kind = kind; |
| | 69 | 25 | | CustomGuardTypeName = customGuardTypeName; |
| | 69 | 26 | | CustomGuardMethodName = customGuardMethodName; |
| | 69 | 27 | | ForwardedArgumentLiterals = forwardedArgumentLiterals ?? Array.Empty<string>(); |
| | 69 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The guard kind. <see cref="GeneratedConstructorGuardKind.Custom"/> indicates a |
| | | 32 | | /// resolved custom guard type and method call. |
| | | 33 | | /// </summary> |
| | 279 | 34 | | public GeneratedConstructorGuardKind Kind { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The fully qualified custom guard type name, or <see langword="null"/> for a |
| | | 38 | | /// built-in guard. |
| | | 39 | | /// </summary> |
| | 91 | 40 | | public string? CustomGuardTypeName { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// The resolved custom guard method name, or <see langword="null"/> for a built-in |
| | | 44 | | /// guard. |
| | | 45 | | /// </summary> |
| | 91 | 46 | | public string? CustomGuardMethodName { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Rendered C# literals forwarded from a parameterized alias attribute usage, in |
| | | 50 | | /// declared order. |
| | | 51 | | /// </summary> |
| | 112 | 52 | | public string[] ForwardedArgumentLiterals { get; } |
| | | 53 | | |
| | | 54 | | public bool Equals(ConstructorGuardModel other) |
| | | 55 | | { |
| | 3 | 56 | | return Kind == other.Kind && |
| | 3 | 57 | | string.Equals(CustomGuardTypeName, other.CustomGuardTypeName, StringComparison.Ordinal) && |
| | 3 | 58 | | string.Equals(CustomGuardMethodName, other.CustomGuardMethodName, StringComparison.Ordinal) && |
| | 3 | 59 | | ForwardedArgumentLiteralsEqual(ForwardedArgumentLiterals, other.ForwardedArgumentLiterals); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | public override bool Equals(object? obj) => obj is ConstructorGuardModel other && Equals(other); |
| | | 63 | | |
| | | 64 | | public override int GetHashCode() |
| | | 65 | | { |
| | | 66 | | unchecked |
| | | 67 | | { |
| | 0 | 68 | | var hash = (int)Kind; |
| | 0 | 69 | | hash = (hash * 397) ^ (CustomGuardTypeName?.GetHashCode() ?? 0); |
| | 0 | 70 | | hash = (hash * 397) ^ (CustomGuardMethodName?.GetHashCode() ?? 0); |
| | | 71 | | |
| | 0 | 72 | | foreach (var literal in ForwardedArgumentLiterals) |
| | | 73 | | { |
| | 0 | 74 | | hash = (hash * 397) ^ literal.GetHashCode(); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return hash; |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | public static bool operator ==(ConstructorGuardModel left, ConstructorGuardModel right) => left.Equals(right); |
| | | 82 | | |
| | 0 | 83 | | public static bool operator !=(ConstructorGuardModel left, ConstructorGuardModel right) => !left.Equals(right); |
| | | 84 | | |
| | | 85 | | private static bool ForwardedArgumentLiteralsEqual(string[] left, string[] right) |
| | | 86 | | { |
| | 3 | 87 | | if (ReferenceEquals(left, right)) |
| | 0 | 88 | | return true; |
| | | 89 | | |
| | 3 | 90 | | if (left.Length != right.Length) |
| | 0 | 91 | | return false; |
| | | 92 | | |
| | 3 | 93 | | return left.SequenceEqual(right, StringComparer.Ordinal); |
| | | 94 | | } |
| | | 95 | | } |