|
| 1 | +using System.Globalization; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | + |
| 6 | +namespace Coder.Desktop.CoderSdk; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A simplistic UUIDv4 class that wraps a 16-byte array. We don't use the |
| 10 | +/// native Guid class because it has some weird reordering behavior due to |
| 11 | +/// legacy Windows stuff. This class is not guaranteed to provide full RFC |
| 12 | +/// 4122 compliance, but it should provide enough coverage for Coder |
| 13 | +/// Desktop. |
| 14 | +/// </summary> |
| 15 | +[JsonConverter(typeof(UuidJsonConverter))] |
| 16 | +public class Uuid |
| 17 | +{ |
| 18 | + private readonly byte[] _bytes; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// The (invalid) zero UUID. |
| 22 | + /// </summary> |
| 23 | + public static Uuid Zero { get; } = new(); |
| 24 | + |
| 25 | + public ReadOnlySpan<byte> Bytes => _bytes; |
| 26 | + |
| 27 | + private Uuid() |
| 28 | + { |
| 29 | + _bytes = new byte[16]; |
| 30 | + } |
| 31 | + |
| 32 | + public Uuid(ReadOnlySpan<byte> bytes) |
| 33 | + { |
| 34 | + if (bytes.Length != 16) |
| 35 | + throw new ArgumentException($"UUID must be 16 bytes, but was {bytes.Length} bytes", nameof(bytes)); |
| 36 | + if (bytes[6] >> 4 != 0x4) |
| 37 | + throw new ArgumentException("ID does not seem like a valid UUIDv4", nameof(bytes)); |
| 38 | + _bytes = bytes.ToArray(); |
| 39 | + } |
| 40 | + |
| 41 | + public Uuid(string str) |
| 42 | + { |
| 43 | + if (str.Length != 36) |
| 44 | + throw new ArgumentException($"UUID string must be 36 characters, but was {str.Length} characters", |
| 45 | + nameof(str)); |
| 46 | + |
| 47 | + var currentIndex = 0; |
| 48 | + _bytes = new byte[16]; |
| 49 | + |
| 50 | + for (var i = 0; i < 36; i++) |
| 51 | + { |
| 52 | + if (i is 8 or 13 or 18 or 23) |
| 53 | + { |
| 54 | + if (str[i] != '-') |
| 55 | + throw new ArgumentException("UUID string must have dashes at positions 8, 13, 18, and 23", |
| 56 | + nameof(str)); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + // Take two hex digits and convert them to a byte. |
| 61 | + var hex = str[i..(i + 2)]; |
| 62 | + if (!byte.TryParse(hex, NumberStyles.HexNumber, null, out var b)) |
| 63 | + throw new ArgumentException($"UUID string has invalid hex digits at position {i}", nameof(str)); |
| 64 | + _bytes[currentIndex] = b; |
| 65 | + currentIndex++; |
| 66 | + |
| 67 | + // Advance the loop index by 1 as we processed two characters. |
| 68 | + i++; |
| 69 | + } |
| 70 | + |
| 71 | + if (currentIndex != 16) |
| 72 | + throw new ArgumentException($"UUID string must have 16 bytes, but was {currentIndex} bytes", nameof(str)); |
| 73 | + if (_bytes[6] >> 4 != 0x4) |
| 74 | + throw new ArgumentException("ID does not seem like a valid UUIDv4", nameof(str)); |
| 75 | + } |
| 76 | + |
| 77 | + public static bool TryFrom(ReadOnlySpan<byte> bytes, out Uuid uuid) |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + uuid = new Uuid(bytes); |
| 82 | + return true; |
| 83 | + } |
| 84 | + catch |
| 85 | + { |
| 86 | + uuid = Zero; |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static bool TryParse(string str, out Uuid uuid) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + uuid = new Uuid(str); |
| 96 | + return true; |
| 97 | + } |
| 98 | + catch |
| 99 | + { |
| 100 | + uuid = Zero; |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public override string ToString() |
| 106 | + { |
| 107 | + if (_bytes.Length != 16) |
| 108 | + throw new ArgumentException($"UUID must be 16 bytes, but was {_bytes.Length} bytes", nameof(_bytes)); |
| 109 | + |
| 110 | + // Print every byte as hex, with dashes in the right places. |
| 111 | + var sb = new StringBuilder(36); |
| 112 | + for (var i = 0; i < 16; i++) |
| 113 | + { |
| 114 | + if (i is 4 or 6 or 8 or 10) |
| 115 | + sb.Append('-'); |
| 116 | + sb.Append(_bytes[i].ToString("x2")); |
| 117 | + } |
| 118 | + |
| 119 | + return sb.ToString(); |
| 120 | + } |
| 121 | + |
| 122 | + #region Uuid equality |
| 123 | + |
| 124 | + public override bool Equals(object? obj) |
| 125 | + { |
| 126 | + return obj is Uuid other && Equals(other); |
| 127 | + } |
| 128 | + |
| 129 | + public bool Equals(Uuid? other) |
| 130 | + { |
| 131 | + return other is not null && _bytes.SequenceEqual(other._bytes); |
| 132 | + } |
| 133 | + |
| 134 | + public override int GetHashCode() |
| 135 | + { |
| 136 | + return _bytes.GetHashCode(); |
| 137 | + } |
| 138 | + |
| 139 | + public static bool operator ==(Uuid left, Uuid right) |
| 140 | + { |
| 141 | + return left.Equals(right); |
| 142 | + } |
| 143 | + |
| 144 | + public static bool operator !=(Uuid left, Uuid right) |
| 145 | + { |
| 146 | + return !left.Equals(right); |
| 147 | + } |
| 148 | + |
| 149 | + #endregion |
| 150 | +} |
| 151 | + |
| 152 | +public class UuidJsonConverter : JsonConverter<Uuid> |
| 153 | +{ |
| 154 | + public override Uuid? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 155 | + { |
| 156 | + if (reader.TokenType == JsonTokenType.Null) |
| 157 | + return null; |
| 158 | + |
| 159 | + if (reader.TokenType != JsonTokenType.String) |
| 160 | + throw new JsonException("Expected string token type for UUID"); |
| 161 | + |
| 162 | + var str = reader.GetString(); |
| 163 | + if (str == null) |
| 164 | + return null; |
| 165 | + |
| 166 | + try |
| 167 | + { |
| 168 | + return new Uuid(str); |
| 169 | + } |
| 170 | + catch (Exception ex) |
| 171 | + { |
| 172 | + throw new JsonException($"Invalid UUID string '{str}'", ex); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public override void Write(Utf8JsonWriter writer, Uuid value, JsonSerializerOptions options) |
| 177 | + { |
| 178 | + writer.WriteStringValue(value.ToString()); |
| 179 | + } |
| 180 | +} |
0 commit comments