| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Protocol.Tlv.Converters; |
| | | 4 | | |
| | | 5 | | using Domain.Protocol.Constants; |
| | | 6 | | using Domain.Protocol.Interfaces; |
| | | 7 | | using Domain.Protocol.Tlv; |
| | | 8 | | |
| | | 9 | | public class ChannelTypeTlvConverter : ITlvConverter<ChannelTypeTlv> |
| | | 10 | | { |
| | | 11 | | public BaseTlv ConvertToBase(ChannelTypeTlv tlv) |
| | | 12 | | { |
| | 12 | 13 | | tlv.Value = tlv.ChannelType; |
| | | 14 | | |
| | 12 | 15 | | return tlv; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public ChannelTypeTlv ConvertFromBase(BaseTlv baseTlv) |
| | | 19 | | { |
| | 12 | 20 | | if (baseTlv.Type != TlvConstants.ChannelType) |
| | | 21 | | { |
| | 0 | 22 | | throw new InvalidCastException("Invalid TLV type"); |
| | | 23 | | } |
| | | 24 | | |
| | 12 | 25 | | if (baseTlv.Length == 0) |
| | | 26 | | { |
| | 0 | 27 | | throw new InvalidCastException("Invalid length"); |
| | | 28 | | } |
| | | 29 | | |
| | 12 | 30 | | return new ChannelTypeTlv(baseTlv.Value); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | [ExcludeFromCodeCoverage] |
| | | 34 | | BaseTlv ITlvConverter.ConvertFromBase(BaseTlv tlv) |
| | | 35 | | { |
| | | 36 | | return ConvertFromBase(tlv); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | [ExcludeFromCodeCoverage] |
| | | 40 | | BaseTlv ITlvConverter.ConvertToBase(BaseTlv tlv) |
| | | 41 | | { |
| | | 42 | | return ConvertToBase(tlv as ChannelTypeTlv |
| | | 43 | | ?? throw new InvalidCastException($"Error converting BaseTlv to {nameof(ChannelTypeTlv)}")); |
| | | 44 | | } |
| | | 45 | | } |