| | | 1 | | using System.Buffers; |
| | | 2 | | using NLightning.Domain.Channels.ValueObjects; |
| | | 3 | | using NLightning.Domain.Interfaces; |
| | | 4 | | using NLightning.Domain.Serialization.Interfaces; |
| | | 5 | | |
| | | 6 | | namespace NLightning.Infrastructure.Serialization.ValueObjects; |
| | | 7 | | |
| | | 8 | | public class ChannelFlagTypeSerializer : IValueObjectTypeSerializer<ChannelFlags> |
| | | 9 | | { |
| | | 10 | | public async Task SerializeAsync(IValueObject valueObject, Stream stream) |
| | | 11 | | { |
| | 12 | 12 | | if (valueObject is not ChannelFlags channelFlags) |
| | 0 | 13 | | throw new ArgumentException("Value object must be of type ChannelFlags.", nameof(valueObject)); |
| | | 14 | | |
| | 12 | 15 | | await stream.WriteAsync(new ReadOnlyMemory<byte>([channelFlags])); |
| | 12 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task<ChannelFlags> DeserializeAsync(Stream stream) |
| | | 19 | | { |
| | 20 | 20 | | var buffer = ArrayPool<byte>.Shared.Rent(1); |
| | | 21 | | |
| | | 22 | | try |
| | | 23 | | { |
| | 20 | 24 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..1]); |
| | 16 | 25 | | return new ChannelFlags(buffer[0]); |
| | | 26 | | } |
| | | 27 | | finally |
| | | 28 | | { |
| | 20 | 29 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 30 | | } |
| | 16 | 31 | | } |
| | | 32 | | |
| | | 33 | | async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream) |
| | | 34 | | { |
| | 0 | 35 | | return await DeserializeAsync(stream); |
| | 0 | 36 | | } |
| | | 37 | | } |