|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Globalization; |
| 4 | +using System.Numerics; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | +using Backdash.Core; |
| 8 | + |
| 9 | +namespace Backdash; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Value representation of a Checksum |
| 13 | +/// </summary> |
| 14 | +[Serializable] |
| 15 | +[DebuggerDisplay("{ToString()}")] |
| 16 | +[JsonConverter(typeof(JsonConverter))] |
| 17 | +public readonly record struct Checksum : |
| 18 | + IComparable<Checksum>, |
| 19 | + IComparable<uint>, |
| 20 | + IEquatable<uint>, |
| 21 | + IUtf8SpanFormattable, |
| 22 | + ISpanFormattable, |
| 23 | + IComparisonOperators<Checksum, Checksum, bool>, |
| 24 | + IComparisonOperators<Checksum, uint, bool> |
| 25 | +{ |
| 26 | + /// <summary>Return frame value <c>0</c></summary> |
| 27 | + public static readonly Checksum Empty = new(0); |
| 28 | + |
| 29 | + /// <summary>Returns the <see cref="uint" /> value for the current <see cref="Checksum" />.</summary> |
| 30 | + public readonly uint Value; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initialize new <see cref="Checksum" /> for frame <paramref name="value" />. |
| 34 | + /// </summary> |
| 35 | + /// <param name="value"></param> |
| 36 | + public Checksum(uint value) => Value = value; |
| 37 | + |
| 38 | + /// <inheritdoc /> |
| 39 | + public int CompareTo(Checksum other) => Value.CompareTo(other.Value); |
| 40 | + |
| 41 | + /// <inheritdoc /> |
| 42 | + public int CompareTo(uint other) => Value.CompareTo(other); |
| 43 | + |
| 44 | + /// <inheritdoc /> |
| 45 | + public bool Equals(uint other) => Value == other; |
| 46 | + |
| 47 | + /// <summary>Return true if current value is <c>0</c></summary> |
| 48 | + public bool IsEmpty => Value is 0u; |
| 49 | + |
| 50 | + const string DefaultFormat = "x8"; |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public string ToString( |
| 54 | + [StringSyntax(StringSyntaxAttribute.NumericFormat)] |
| 55 | + string? format, |
| 56 | + IFormatProvider? formatProvider |
| 57 | + ) => |
| 58 | + Value.ToString(format ?? DefaultFormat, formatProvider); |
| 59 | + |
| 60 | + /// <inheritdoc /> |
| 61 | + public override string ToString() => ToString(null, null); |
| 62 | + |
| 63 | + /// <inheritdoc cref="ToString(string, IFormatProvider)" /> |
| 64 | + public string ToString( |
| 65 | + [StringSyntax(StringSyntaxAttribute.NumericFormat)] |
| 66 | + string format) => ToString(format, null); |
| 67 | + |
| 68 | + /// <inheritdoc /> |
| 69 | + public bool TryFormat( |
| 70 | + Span<byte> utf8Destination, out int bytesWritten, |
| 71 | + ReadOnlySpan<char> format, |
| 72 | + IFormatProvider? provider |
| 73 | + ) |
| 74 | + { |
| 75 | + bytesWritten = 0; |
| 76 | + if (format.IsEmpty) format = DefaultFormat; |
| 77 | + Utf8StringBuilder writer = new(in utf8Destination, ref bytesWritten); |
| 78 | + return writer.Write(Value, format); |
| 79 | + } |
| 80 | + |
| 81 | + /// <inheritdoc /> |
| 82 | + public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, |
| 83 | + IFormatProvider? provider) |
| 84 | + { |
| 85 | + if (format.IsEmpty) format = DefaultFormat; |
| 86 | + return Value.TryFormat(destination, out charsWritten, format, provider); |
| 87 | + } |
| 88 | + |
| 89 | + /// <inheritdoc /> |
| 90 | + public static bool operator >(Checksum left, Checksum right) => left.Value > right.Value; |
| 91 | + |
| 92 | + /// <inheritdoc /> |
| 93 | + public static bool operator >=(Checksum left, Checksum right) => left.Value >= right.Value; |
| 94 | + |
| 95 | + /// <inheritdoc /> |
| 96 | + public static bool operator <(Checksum left, Checksum right) => left.Value < right.Value; |
| 97 | + |
| 98 | + /// <inheritdoc /> |
| 99 | + public static bool operator <=(Checksum left, Checksum right) => left.Value <= right.Value; |
| 100 | + |
| 101 | + /// <inheritdoc /> |
| 102 | + public static bool operator ==(Checksum left, uint right) => left.Value == right; |
| 103 | + |
| 104 | + /// <inheritdoc /> |
| 105 | + public static bool operator !=(Checksum left, uint right) => left.Value != right; |
| 106 | + |
| 107 | + /// <inheritdoc /> |
| 108 | + public static bool operator >(Checksum left, uint right) => left.Value > right; |
| 109 | + |
| 110 | + /// <inheritdoc /> |
| 111 | + public static bool operator >=(Checksum left, uint right) => left.Value >= right; |
| 112 | + |
| 113 | + /// <inheritdoc /> |
| 114 | + public static bool operator <(Checksum left, uint right) => left.Value < right; |
| 115 | + |
| 116 | + /// <inheritdoc /> |
| 117 | + public static bool operator <=(Checksum left, uint right) => left.Value <= right; |
| 118 | + |
| 119 | + /// <inheritdoc cref="Value" /> |
| 120 | + public static implicit operator uint(Checksum frame) => frame.Value; |
| 121 | + |
| 122 | + /// <inheritdoc cref="Checksum(uint)" /> |
| 123 | + public static explicit operator Checksum(uint frame) => new(frame); |
| 124 | + |
| 125 | + internal sealed class JsonConverter : JsonConverter<Checksum> |
| 126 | + { |
| 127 | + public override Checksum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 128 | + { |
| 129 | + var value = reader.GetString(); |
| 130 | + return string.IsNullOrWhiteSpace(value) ? Empty : new(uint.Parse(value, NumberStyles.HexNumber, null)); |
| 131 | + } |
| 132 | + |
| 133 | + public override void Write(Utf8JsonWriter writer, Checksum value, JsonSerializerOptions options) |
| 134 | + { |
| 135 | + Span<char> buffer = stackalloc char[8]; |
| 136 | + if (value.TryFormat(buffer, out var charCount, DefaultFormat, null)) |
| 137 | + { |
| 138 | + writer.WriteStringValue(buffer[..charCount]); |
| 139 | + } |
| 140 | + else |
| 141 | + writer.WriteStringValue(value.ToString(DefaultFormat, null)); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments