< 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: 36_15743069263
Line coverage
94%
Covered lines: 18
Uncovered lines: 1
Coverable lines: 19
Total lines: 65
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;
 2using NLightning.Domain.Protocol.ValueObjects;
 3using NLightning.Domain.Serialization.Interfaces;
 4
 5namespace NLightning.Infrastructure.Serialization.Tlv;
 6
 7using Domain.Protocol.Tlv;
 8
 9public class TlvSerializer : ITlvSerializer
 10{
 11    private readonly IValueObjectTypeSerializer<BigSize> _bigSizeSerializer;
 12
 1613    public TlvSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory)
 14    {
 1615        _bigSizeSerializer = valueObjectSerializerFactory.GetSerializer<BigSize>()
 1616                          ?? throw new ArgumentNullException(nameof(valueObjectSerializerFactory));
 1617    }
 18
 19    /// <summary>
 20    /// Serializes a BaseTlv value into a stream.
 21    /// </summary>
 22    /// <param name="baseTlv">The BaseTlv value to serialize.</param>
 23    /// <param name="stream">The stream where the serialized value will be written.</param>
 24    /// <returns>A task that represents the asynchronous serialization operation.</returns>
 25    /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception>
 26    /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception>
 27    public async Task SerializeAsync(BaseTlv baseTlv, Stream stream)
 28    {
 7229        await _bigSizeSerializer.SerializeAsync(baseTlv.Type, stream);
 7230        await _bigSizeSerializer.SerializeAsync(baseTlv.Length, stream);
 31
 7232        await stream.WriteAsync(baseTlv.Value);
 7233    } //2102C93CA7DCA44D2E45E3CC5419D92750F7FB3A0F180852B73A621F4051C0193A75AC
 34
 35    /// <summary>
 36    /// Deserializes a BaseTlv value from a stream.
 37    /// </summary>
 38    /// <param name="stream">The stream from which the BaseTlv value will be deserialized.</param>
 39    /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized BaseTlv 
 40    /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ
 41    /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception>
 42    public async Task<BaseTlv?> DeserializeAsync(Stream stream)
 43    {
 11244        if (stream.Position == stream.Length)
 045            return null;
 46
 11247        byte[]? value = null;
 48
 49        try
 50        {
 11251            var type = await _bigSizeSerializer.DeserializeAsync(stream);
 11252            var length = await _bigSizeSerializer.DeserializeAsync(stream);
 53
 10854            value = ArrayPool<byte>.Shared.Rent(length);
 10855            await stream.ReadExactlyAsync(value.AsMemory()[..(int)length]);
 56
 7257            return new BaseTlv(type, length, value[..(int)length]);
 58        }
 59        finally
 60        {
 11261            if (value is not null)
 10862                ArrayPool<byte>.Shared.Return(value);
 63        }
 7264    }
 65}