| | | 1 | | using System.Buffers; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Serialization.ValueObjects; |
| | | 4 | | |
| | | 5 | | using Domain.Channels.ValueObjects; |
| | | 6 | | using Domain.Interfaces; |
| | | 7 | | using Domain.Serialization.Interfaces; |
| | | 8 | | |
| | | 9 | | public class ShortChannelIdTypeSerializer : IValueObjectTypeSerializer<ShortChannelId> |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Serializes a ShortChannelId value into a stream. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="valueObject">The ShortChannelId value to serialize.</param> |
| | | 15 | | /// <param name="stream">The stream where the serialized value will be written.</param> |
| | | 16 | | /// <returns>A task that represents the asynchronous serialization operation.</returns> |
| | | 17 | | /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception> |
| | | 18 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception> |
| | | 19 | | public async Task SerializeAsync(IValueObject valueObject, Stream stream) |
| | | 20 | | { |
| | 4 | 21 | | if (valueObject is not ShortChannelId shortChannelId) |
| | 0 | 22 | | throw new ArgumentException("Value object must be of type ShortChannelId.", nameof(valueObject)); |
| | | 23 | | |
| | 4 | 24 | | await stream.WriteAsync(shortChannelId); |
| | 4 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Deserializes a ShortChannelId value from a stream. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="stream">The stream from which the ShortChannelId value will be deserialized.</param> |
| | | 31 | | /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized ShortCha |
| | | 32 | | /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ |
| | | 33 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception> |
| | | 34 | | public async Task<ShortChannelId> DeserializeAsync(Stream stream) |
| | | 35 | | { |
| | 4 | 36 | | var buffer = ArrayPool<byte>.Shared.Rent(ShortChannelId.Length); |
| | | 37 | | |
| | | 38 | | try |
| | | 39 | | { |
| | 4 | 40 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..ShortChannelId.Length]); |
| | | 41 | | |
| | 4 | 42 | | return new ShortChannelId(buffer[..ShortChannelId.Length]); |
| | | 43 | | } |
| | | 44 | | finally |
| | | 45 | | { |
| | 4 | 46 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 47 | | } |
| | 4 | 48 | | } |
| | | 49 | | |
| | | 50 | | async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream) |
| | | 51 | | { |
| | 0 | 52 | | return await DeserializeAsync(stream); |
| | 0 | 53 | | } |
| | | 54 | | } |