< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Serialization.ValueObjects.ChainHashTypeSerializer
Assembly: NLightning.Infrastructure.Serialization
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/ValueObjects/ChainHashTypeSerializer.cs
Tag: 36_15743069263
Line coverage
72%
Covered lines: 8
Uncovered lines: 3
Coverable lines: 11
Total lines: 54
Line coverage: 72.7%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SerializeAsync()50%2.06275%
DeserializeAsync()100%11100%
NLightning-Domain-Serialization-Interfaces-IValueObjectTypeSerializer-DeserializeAsync()100%210%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/ValueObjects/ChainHashTypeSerializer.cs

#LineLine coverage
 1using System.Buffers;
 2using NLightning.Domain.Serialization.Interfaces;
 3
 4namespace NLightning.Infrastructure.Serialization.ValueObjects;
 5
 6using Domain.Crypto.Constants;
 7using Domain.Interfaces;
 8using Domain.Protocol.ValueObjects;
 9
 10public class ChainHashTypeSerializer : IValueObjectTypeSerializer<ChainHash>
 11{
 12    /// <summary>
 13    /// Serializes a ChainHash value into a stream.
 14    /// </summary>
 15    /// <param name="valueObject">The ChainHash value to serialize.</param>
 16    /// <param name="stream">The stream where the serialized value will be written.</param>
 17    /// <returns>A task that represents the asynchronous serialization operation.</returns>
 18    /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception>
 19    /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception>
 20    public async Task SerializeAsync(IValueObject valueObject, Stream stream)
 21    {
 1222        if (valueObject is not ChainHash chainHash)
 023            throw new ArgumentException("Value object must be of type ChainHash.", nameof(valueObject));
 24
 1225        await stream.WriteAsync(chainHash);
 1226    }
 27
 28    /// <summary>
 29    /// Deserializes a ChainHash value from a stream.
 30    /// </summary>
 31    /// <param name="stream">The stream from which the ChainHash value will be deserialized.</param>
 32    /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized ChainHas
 33    /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ
 34    /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception>
 35    public async Task<ChainHash> DeserializeAsync(Stream stream)
 36    {
 2037        var buffer = ArrayPool<byte>.Shared.Rent(CryptoConstants.Sha256HashLen);
 38
 39        try
 40        {
 2041            await stream.ReadExactlyAsync(buffer.AsMemory()[..CryptoConstants.Sha256HashLen]);
 42
 1643            return new ChainHash(buffer);
 44        }
 45        finally
 46        {
 2047            ArrayPool<byte>.Shared.Return(buffer);
 48        }
 1649    }
 50    async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream)
 51    {
 052        return await DeserializeAsync(stream);
 053    }
 54}