Skip to content

Commit

Permalink
added reference to TEnum, for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfriend77 committed Sep 11, 2024
1 parent f4ee76b commit 2c67928
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
43 changes: 19 additions & 24 deletions Substrate.NetApi.Test/TypeConverters/EnumRustTest.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,16 +19,15 @@ public class BaseEnumRustTests
[Test]
public void ExtEnumEncodingTest()
{
var typeDecoderMap = new Dictionary<byte, Type>
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ 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<PhaseState>(typeDecoderMap);


int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);

Expand All @@ -39,15 +36,14 @@ public void ExtEnumEncodingTest()
Assert.AreEqual(1, (extEnumType.Value2 as U8).Value);
}


[Test]
public void ExtEnumDencodingTest()
{
var typeDecoderMap = new Dictionary<byte, Type>
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ 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<PhaseState>(typeDecoderMap);
Expand All @@ -65,27 +61,27 @@ public void ExtEnumDencodingTest()
[Test]
public void ExtEnumCreateTest()
{
var typeDecoderMap = new Dictionary<byte, Type>
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ 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);
var byValue = new BaseEnumRust<PhaseState>(typeDecoderMap);
byValue.Create(PhaseState.None, u8);

var byArray = new BaseEnumRust<PhaseState>();
byArray.AddTypeDecoder<U8>(0x00);
byArray.AddTypeDecoder<BaseVoid>(0x01);
byArray.AddTypeDecoder<BaseVoid>(0x02);
byArray.AddTypeDecoder<U8>(PhaseState.None);
byArray.AddTypeDecoder<BaseVoid>(PhaseState.Finalization);
byArray.AddTypeDecoder<BaseVoid>(PhaseState.Initialization);
byArray.Create(new byte[] { 0, 1 });

var byHex = new BaseEnumRust<PhaseState>();
byHex.AddTypeDecoder<U8>(0x00);
byHex.AddTypeDecoder<BaseVoid>(0x01);
byHex.AddTypeDecoder<BaseVoid>(0x02);
byHex.AddTypeDecoder<U8>(PhaseState.None);
byHex.AddTypeDecoder<BaseVoid>(PhaseState.Finalization);
byHex.AddTypeDecoder<BaseVoid>(PhaseState.Initialization);
byHex.Create("0x0001");

Assert.That(byValue.Bytes, Is.EqualTo(byArray.Bytes));
Expand All @@ -95,5 +91,4 @@ public void ExtEnumCreateTest()
Assert.That(byValue.Value, Is.EqualTo(byHex.Value));
}
}

}
}
33 changes: 20 additions & 13 deletions Substrate.NetApi/Model/Types/Base/BaseEnumRust.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ namespace Substrate.NetApi.Model.Types.Base
/// Next version of BaseEnumExt to support Rust enums
/// </summary>
/// <typeparam name="TEnum"></typeparam>
public class BaseEnumRust<TEnum> : BaseEnumType where TEnum : Enum
public class BaseEnumRust<TEnum> : BaseType where TEnum : Enum
{
private readonly Dictionary<byte, Func<byte[], int, Tuple<IType, int>>> _typeDecoders;
private readonly Dictionary<TEnum, Func<byte[], int, Tuple<IType, int>>> _typeDecoders;

/// <summary>
/// Constructor
/// </summary>
public BaseEnumRust()
{
_typeDecoders = new Dictionary<byte, Func<byte[], int, Tuple<IType, int>>>();
_typeDecoders = new Dictionary<TEnum, Func<byte[], int, Tuple<IType, int>>>();
}

/// <summary>
/// Constructor
/// </summary>
public BaseEnumRust(Dictionary<byte, Type> typeDecoderMap)
public BaseEnumRust(Dictionary<TEnum, Type> typeDecoderMap)
{
_typeDecoders = new Dictionary<byte, Func<byte[], int, Tuple<IType, int>>>();
_typeDecoders = new Dictionary<TEnum, Func<byte[], int, Tuple<IType, int>>>();
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);
Expand All @@ -43,10 +43,10 @@ public BaseEnumRust(Dictionary<byte, Type> typeDecoderMap)
/// Add a type decoder
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <param name="enumByte"></param>
public void AddTypeDecoder<TType>(byte enumByte) where TType : IType, new()
/// <param name="enumValue"></param>
public void AddTypeDecoder<TType>(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);
Expand All @@ -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;
Expand Down Expand Up @@ -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.");
}
Expand Down

0 comments on commit 2c67928

Please sign in to comment.