From 2c6792803d6e466871a8893ddbacd959c53e3db1 Mon Sep 17 00:00:00 2001 From: Cedric Decoster Date: Wed, 11 Sep 2024 12:16:04 +0200 Subject: [PATCH] added reference to TEnum, for better readability --- .../TypeConverters/EnumRustTest.cs | 43 ++++++++----------- .../Model/Types/Base/BaseEnumRust.cs | 33 ++++++++------ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/Substrate.NetApi.Test/TypeConverters/EnumRustTest.cs b/Substrate.NetApi.Test/TypeConverters/EnumRustTest.cs index cce1598..3650e19 100644 --- a/Substrate.NetApi.Test/TypeConverters/EnumRustTest.cs +++ b/Substrate.NetApi.Test/TypeConverters/EnumRustTest.cs @@ -1,8 +1,6 @@ using NUnit.Framework; -using Substrate.NetApi.Model.Types; using Substrate.NetApi.Model.Types.Base; using Substrate.NetApi.Model.Types.Primitive; -using Substrate.NetApi.Test; using System; using System.Collections.Generic; @@ -21,16 +19,15 @@ public class BaseEnumRustTests [Test] public void ExtEnumEncodingTest() { - var typeDecoderMap = new Dictionary + var typeDecoderMap = new Dictionary { - { 0x00, typeof(U8) }, - { 0x01, typeof(BaseVoid) }, - { 0x02, typeof(BaseVoid) } + { PhaseState.None, typeof(U8) }, + { PhaseState.Finalization, typeof(BaseVoid) }, + { PhaseState.Initialization, typeof(BaseVoid) } }; var extEnumType = new BaseEnumRust(typeDecoderMap); - int p = 0; extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p); @@ -39,15 +36,14 @@ public void ExtEnumEncodingTest() Assert.AreEqual(1, (extEnumType.Value2 as U8).Value); } - [Test] public void ExtEnumDencodingTest() { - var typeDecoderMap = new Dictionary + var typeDecoderMap = new Dictionary { - { 0x00, typeof(U8) }, - { 0x01, typeof(BaseVoid) }, - { 0x02, typeof(BaseVoid) } + { PhaseState.None, typeof(U8) }, + { PhaseState.Finalization, typeof(BaseVoid) }, + { PhaseState.Initialization, typeof(BaseVoid) } }; var extEnumType = new BaseEnumRust(typeDecoderMap); @@ -65,11 +61,11 @@ public void ExtEnumDencodingTest() [Test] public void ExtEnumCreateTest() { - var typeDecoderMap = new Dictionary + var typeDecoderMap = new Dictionary { - { 0x00, typeof(U8) }, - { 0x01, typeof(BaseVoid) }, - { 0x02, typeof(BaseVoid) } + { PhaseState.None, typeof(U8) }, + { PhaseState.Finalization, typeof(BaseVoid) }, + { PhaseState.Initialization, typeof(BaseVoid) } }; var u8 = new U8(1); @@ -77,15 +73,15 @@ public void ExtEnumCreateTest() byValue.Create(PhaseState.None, u8); var byArray = new BaseEnumRust(); - byArray.AddTypeDecoder(0x00); - byArray.AddTypeDecoder(0x01); - byArray.AddTypeDecoder(0x02); + byArray.AddTypeDecoder(PhaseState.None); + byArray.AddTypeDecoder(PhaseState.Finalization); + byArray.AddTypeDecoder(PhaseState.Initialization); byArray.Create(new byte[] { 0, 1 }); var byHex = new BaseEnumRust(); - byHex.AddTypeDecoder(0x00); - byHex.AddTypeDecoder(0x01); - byHex.AddTypeDecoder(0x02); + byHex.AddTypeDecoder(PhaseState.None); + byHex.AddTypeDecoder(PhaseState.Finalization); + byHex.AddTypeDecoder(PhaseState.Initialization); byHex.Create("0x0001"); Assert.That(byValue.Bytes, Is.EqualTo(byArray.Bytes)); @@ -95,5 +91,4 @@ public void ExtEnumCreateTest() Assert.That(byValue.Value, Is.EqualTo(byHex.Value)); } } - -} +} \ No newline at end of file diff --git a/Substrate.NetApi/Model/Types/Base/BaseEnumRust.cs b/Substrate.NetApi/Model/Types/Base/BaseEnumRust.cs index 0e6af24..d26c455 100644 --- a/Substrate.NetApi/Model/Types/Base/BaseEnumRust.cs +++ b/Substrate.NetApi/Model/Types/Base/BaseEnumRust.cs @@ -7,30 +7,30 @@ namespace Substrate.NetApi.Model.Types.Base /// Next version of BaseEnumExt to support Rust enums /// /// - public class BaseEnumRust : BaseEnumType where TEnum : Enum + public class BaseEnumRust : BaseType where TEnum : Enum { - private readonly Dictionary>> _typeDecoders; + private readonly Dictionary>> _typeDecoders; /// /// Constructor /// public BaseEnumRust() { - _typeDecoders = new Dictionary>>(); + _typeDecoders = new Dictionary>>(); } /// /// Constructor /// - public BaseEnumRust(Dictionary typeDecoderMap) + public BaseEnumRust(Dictionary typeDecoderMap) { - _typeDecoders = new Dictionary>>(); + _typeDecoders = new Dictionary>>(); foreach (var decoder in typeDecoderMap) { - var enumByte = decoder.Key; + var enumValue = decoder.Key; var type = decoder.Value; - _typeDecoders.Add(enumByte, (byteArray, p) => + _typeDecoders.Add(enumValue, (byteArray, p) => { var typeInstance = (IType)Activator.CreateInstance(type); typeInstance.Decode(byteArray, ref p); @@ -43,10 +43,10 @@ public BaseEnumRust(Dictionary typeDecoderMap) /// Add a type decoder /// /// - /// - public void AddTypeDecoder(byte enumByte) where TType : IType, new() + /// + public void AddTypeDecoder(TEnum enumValue) where TType : IType, new() { - _typeDecoders.Add(enumByte, (byteArray, p) => + _typeDecoders.Add(enumValue, (byteArray, p) => { var typeInstance = new TType(); typeInstance.Decode(byteArray, ref p); @@ -61,9 +61,16 @@ public override void Decode(byte[] byteArray, ref int p) var enumByte = byteArray[p]; p += 1; - Value = (TEnum)Enum.Parse(typeof(TEnum), enumByte.ToString(), true); + try + { + Value = (TEnum)Enum.Parse(typeof(TEnum), enumByte.ToString(), true); + } + catch (ArgumentException ex) + { + throw new Exception($"Invalid enum value: {enumByte}", ex); + } - if (_typeDecoders.TryGetValue(enumByte, out var decoder)) + if (_typeDecoders.TryGetValue(Value, out var decoder)) { var result = decoder(byteArray, p); Value2 = result.Item1; @@ -94,7 +101,7 @@ public void Create(TEnum t, IType iType) { var enumByte = Convert.ToByte(t); - if (!_typeDecoders.ContainsKey(enumByte)) + if (!_typeDecoders.ContainsKey(t)) { throw new Exception($"No decoder found for enum byte {enumByte}, make sure to use BaseVoid, if there is no value."); }