55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
using System.Text.Json;
|
|
|
|
namespace ZB.MOM.NatsNet.Server;
|
|
|
|
/// <summary>
|
|
/// String/JSON helpers for store enums that mirror Go enum methods.
|
|
/// </summary>
|
|
public static class StoreEnumExtensions
|
|
{
|
|
public static string String(this StoreCipher cipher)
|
|
=> cipher switch
|
|
{
|
|
StoreCipher.ChaCha => "ChaCha20-Poly1305",
|
|
StoreCipher.Aes => "AES-GCM",
|
|
StoreCipher.NoCipher => "None",
|
|
_ => "Unknown StoreCipher",
|
|
};
|
|
|
|
public static string String(this StoreCompression alg)
|
|
=> alg switch
|
|
{
|
|
StoreCompression.NoCompression => "None",
|
|
StoreCompression.S2Compression => "S2",
|
|
_ => "Unknown StoreCompression",
|
|
};
|
|
|
|
public static byte[] MarshalJSON(this StoreCompression alg)
|
|
=> alg switch
|
|
{
|
|
StoreCompression.NoCompression => JsonSerializer.SerializeToUtf8Bytes("none"),
|
|
StoreCompression.S2Compression => JsonSerializer.SerializeToUtf8Bytes("s2"),
|
|
_ => throw new InvalidOperationException("unknown compression algorithm"),
|
|
};
|
|
|
|
public static void UnmarshalJSON(this ref StoreCompression alg, ReadOnlySpan<byte> b)
|
|
{
|
|
var parsed = JsonSerializer.Deserialize<string>(b);
|
|
if (parsed == null)
|
|
throw new InvalidDataException("compression value must be a JSON string");
|
|
|
|
alg = parsed switch
|
|
{
|
|
"none" => StoreCompression.NoCompression,
|
|
"s2" => StoreCompression.S2Compression,
|
|
_ => throw new InvalidOperationException("unknown compression algorithm"),
|
|
};
|
|
}
|
|
|
|
public static void UnmarshalJSON(this ref StoreCompression alg, byte[] b)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(b);
|
|
UnmarshalJSON(ref alg, b.AsSpan());
|
|
}
|
|
}
|