< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Serialization.Tlv.TlvSerializer
Assembly: NLightning.Infrastructure.Serialization
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/Tlv/TlvSerializer.cs
Tag: 30_15166811759
Line coverage
94%
Covered lines: 18
Uncovered lines: 1
Coverable lines: 19
Total lines: 67
Line coverage: 94.7%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
SerializeAsync()100%11100%
DeserializeAsync()75%4.01490.91%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/Tlv/TlvSerializer.cs

#LineLine coverage
 1using System.Buffers;
 2
 3namespace NLightning.Infrastructure.Serialization.Tlv;
 4
 5using Domain.Protocol.Tlv;
 6using Domain.Serialization.Factories;
 7using Domain.Serialization.Tlv;
 8using Domain.Serialization.ValueObjects;
 9using Domain.ValueObjects;
 10
 11public class TlvSerializer : ITlvSerializer
 12{
 13    private readonly IValueObjectTypeSerializer<BigSize> _bigSizeSerializer;
 14
 1615    public TlvSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory)
 16    {
 1617        _bigSizeSerializer = valueObjectSerializerFactory.GetSerializer<BigSize>()
 1618                             ?? throw new ArgumentNullException(nameof(valueObjectSerializerFactory));
 1619    }
 20
 21    /// <summary>
 22    /// Serializes a BaseTlv value into a stream.
 23    /// </summary>
 24    /// <param name="baseTlv">The BaseTlv value to serialize.</param>
 25    /// <param name="stream">The stream where the serialized value will be written.</param>
 26    /// <returns>A task that represents the asynchronous serialization operation.</returns>
 27    /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception>
 28    /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception>
 29    public async Task SerializeAsync(BaseTlv baseTlv, Stream stream)
 30    {
 7231        await _bigSizeSerializer.SerializeAsync(baseTlv.Type, stream);
 7232        await _bigSizeSerializer.SerializeAsync(baseTlv.Length, stream);
 33
 7234        await stream.WriteAsync(baseTlv.Value);
 7235    }
 36
 37    /// <summary>
 38    /// Deserializes a BaseTlv value from a stream.
 39    /// </summary>
 40    /// <param name="stream">The stream from which the BaseTlv value will be deserialized.</param>
 41    /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized BaseTlv 
 42    /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ
 43    /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception>
 44    public async Task<BaseTlv?> DeserializeAsync(Stream stream)
 45    {
 11246        if (stream.Position == stream.Length)
 047            return null;
 48
 11249        byte[]? value = null;
 50
 51        try
 52        {
 11253            var type = await _bigSizeSerializer.DeserializeAsync(stream);
 11254            var length = await _bigSizeSerializer.DeserializeAsync(stream);
 55
 10856            value = ArrayPool<byte>.Shared.Rent(length);
 10857            await stream.ReadExactlyAsync(value.AsMemory()[..(int)length]);
 58
 7259            return new BaseTlv(type, length, value[..(int)length]);
 60        }
 61        finally
 62        {
 11263            if (value is not null)
 10864                ArrayPool<byte>.Shared.Return(value);
 65        }
 7266    }
 67}