Skip to content

Commit

Permalink
feat(net): add missing .NET primitives (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich authored Nov 1, 2024
1 parent 9d3e18d commit ea8923f
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 0 deletions.
52 changes: 52 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Base/BaseNonZero.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Substrate.NetApi.Model.Types.Base;

namespace Substrate.Gear.Client.Model.Types.Base;

public class BaseNonZero<T> : BaseType
where T : BaseType, new()
{
public static explicit operator BaseNonZero<T>(T value) => new(value);

/// <summary>
/// >> value
/// </summary>
public required T Value { get; set; }

public BaseNonZero()
{
}

[SetsRequiredMembers]
public BaseNonZero(T value)
{
var span = value.Bytes.AsSpan();
if (span.IsZero())
{
throw new InvalidOperationException($"Unable to create a {this.TypeName()} instance while value is zero");
}
this.TypeSize = value.TypeSize;
this.Bytes = span.ToArray();
this.Value = value;
}

/// <inheritdoc/>
public override byte[] Encode() => this.Value.Encode();

/// <inheritdoc/>
public override void Decode(byte[] byteArray, ref int p)
{
var start = p;
this.Value = new();
this.Value.Decode(byteArray, ref p);
var bytesLength = p - start;
if (byteArray.AsSpan().Slice(p, bytesLength).IsZero())
{
throw new InvalidOperationException($"Unable to create a {this.TypeName()} instance while value is zero");
}
this.TypeSize = bytesLength;
this.Bytes = new byte[bytesLength];
Array.Copy(byteArray, start, this.Bytes, 0, bytesLength);
}
}
39 changes: 39 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Base/BaseResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;

namespace Substrate.Gear.Client.Model.Types.Base;

/// <summary>
/// Result
/// </summary>
public enum BaseResult
{

/// <summary>
/// >> Ok
/// </summary>
Ok = 0,

/// <summary>
/// >> Err
/// </summary>
Err = 1,
}

/// <summary>
/// EnumResult
/// </summary>
public sealed class EnumBaseResult<T1, T2> : BaseEnumRust<BaseResult>
where T1 : IType, new()
where T2 : IType, new()
{

/// <summary>
/// Initializes a new instance of the class.
/// </summary>
public EnumBaseResult()
{
this.AddTypeDecoder<T1>(BaseResult.Ok);
this.AddTypeDecoder<T2>(BaseResult.Err);
}
}
22 changes: 22 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Base/SpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Substrate.Gear.Client.Model.Types.Base;

internal static class SpanExtensions
{

/// <summary>
/// Returns true if all bytes are zero
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static bool IsZero(this Span<byte> bytes)
{
byte sum = 0;
foreach (var b in bytes)
{
sum |= b;
}
return sum == 0;
}
}
37 changes: 37 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Primitive/H160.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Substrate.Gear.Api.Generated.Types.Base;
using Substrate.NetApi.Attributes;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Metadata.Base;

namespace Substrate.Gear.Client.Model.Types.Primitive;

/// <summary>
/// H160
/// </summary>
[SubstrateNodeType(TypeDefEnum.Composite)]
public sealed class H160 : BaseType
{
/// <summary>
/// >> value
/// </summary>
public required Arr20U8 Value { get; set; }

/// <inheritdoc/>
public override string TypeName() => nameof(H160);

/// <inheritdoc/>
public override byte[] Encode() => this.Value.Encode();

/// <inheritdoc/>
public override void Decode(byte[] byteArray, ref int p)
{
var start = p;
this.Value = new();
this.Value.Decode(byteArray, ref p);
var bytesLength = p - start;
this.TypeSize = bytesLength;
this.Bytes = new byte[bytesLength];
Array.Copy(byteArray, start, this.Bytes, 0, bytesLength);
}
}
16 changes: 16 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU128.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Substrate.Gear.Client.Model.Types.Base;
using Substrate.NetApi.Attributes;
using Substrate.NetApi.Model.Types.Metadata.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.Gear.Client.Model.Types.Primitive;

/// <summary>
/// NonZeroU128
/// </summary>
[SubstrateNodeType(TypeDefEnum.Composite)]
public sealed class NonZeroU128 : BaseNonZero<U128>
{
/// <inheritdoc/>
public override string TypeName() => nameof(NonZeroU128);
}
16 changes: 16 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU16.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Substrate.Gear.Client.Model.Types.Base;
using Substrate.NetApi.Attributes;
using Substrate.NetApi.Model.Types.Metadata.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.Gear.Client.Model.Types.Primitive;

/// <summary>
/// NonZeroU16
/// </summary>
[SubstrateNodeType(TypeDefEnum.Composite)]
public sealed class NonZeroU16 : BaseNonZero<U16>
{
/// <inheritdoc/>
public override string TypeName() => nameof(NonZeroU16);
}
16 changes: 16 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU256.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Substrate.Gear.Client.Model.Types.Base;
using Substrate.NetApi.Attributes;
using Substrate.NetApi.Model.Types.Metadata.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.Gear.Client.Model.Types.Primitive;

/// <summary>
/// NonZeroU256
/// </summary>
[SubstrateNodeType(TypeDefEnum.Composite)]
public sealed class NonZeroU256 : BaseNonZero<U256>
{
/// <inheritdoc/>
public override string TypeName() => nameof(NonZeroU256);
}
16 changes: 16 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Substrate.Gear.Client.Model.Types.Base;
using Substrate.NetApi.Attributes;
using Substrate.NetApi.Model.Types.Metadata.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.Gear.Client.Model.Types.Primitive;

/// <summary>
/// NonZeroU64
/// </summary>
[SubstrateNodeType(TypeDefEnum.Composite)]
public sealed class NonZeroU64 : BaseNonZero<U64>
{
/// <inheritdoc/>
public override string TypeName() => nameof(NonZeroU64);
}
16 changes: 16 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU8.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Substrate.Gear.Client.Model.Types.Base;
using Substrate.NetApi.Attributes;
using Substrate.NetApi.Model.Types.Metadata.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.Gear.Client.Model.Types.Primitive;

/// <summary>
/// NonZeroU8
/// </summary>
[SubstrateNodeType(TypeDefEnum.Composite)]
public sealed class NonZeroU8 : BaseNonZero<U8>
{
/// <inheritdoc/>
public override string TypeName() => nameof(NonZeroU8);
}

0 comments on commit ea8923f

Please sign in to comment.