1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::{npi::Npi, ton::Ton};
use crate::{
    ende::{
        decode::{Decode, DecodeError},
        encode::{Encode, EncodeError},
        length::Length,
    },
    tri,
    types::{c_octet_string::COctetString, u8::EndeU8},
};

#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum DestFlag {
    #[default]
    SmeAddress = 0x01,
    DistributionListName = 0x02,
    Other(u8),
}

impl From<u8> for DestFlag {
    fn from(value: u8) -> Self {
        match value {
            0x01 => DestFlag::SmeAddress,
            0x02 => DestFlag::DistributionListName,
            value => DestFlag::Other(value),
        }
    }
}

impl From<DestFlag> for u8 {
    fn from(value: DestFlag) -> Self {
        match value {
            DestFlag::SmeAddress => 0x01,
            DestFlag::DistributionListName => 0x02,
            DestFlag::Other(value) => value,
        }
    }
}

impl From<DestFlag> for u32 {
    fn from(value: DestFlag) -> Self {
        u8::from(value).into()
    }
}

impl EndeU8 for DestFlag {}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum DestAddress {
    /// SME Format Destination Address.
    SmeAddress(SmeAddress),
    /// Distribution List Format Destination Address.
    DistributionListName(DistributionListName),
}

impl Length for DestAddress {
    fn length(&self) -> usize {
        match self {
            Self::SmeAddress(sa) => sa.length(),
            Self::DistributionListName(dlm) => dlm.length(),
        }
    }
}

impl Encode for DestAddress {
    fn encode_to<W: std::io::Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
        match self {
            Self::SmeAddress(sa) => sa.encode_to(writer),
            Self::DistributionListName(dlm) => dlm.encode_to(writer),
        }
    }
}

impl Decode for DestAddress {
    fn decode_from<R: std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
        let flag = tri!(DestFlag::decode_from(reader));

        match flag {
            DestFlag::SmeAddress => {
                let sa = tri!(SmeAddress::decode_from(reader));

                Ok(Self::SmeAddress(sa))
            }
            DestFlag::DistributionListName => {
                let dlm = tri!(DistributionListName::decode_from(reader));

                Ok(Self::DistributionListName(dlm))
            }
            DestFlag::Other(flag) => Err(DecodeError::UnsupportedKey { key: flag.into() }),
        }
    }
}

/// SME Format Destination Address.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SmeAddress {
    /// 0x01 (SME Address).
    ///
    /// Can't and shouldn't be updated
    dest_flag: DestFlag,
    /// Type of Number for destination.
    pub dest_addr_ton: Ton,
    /// Numbering Plan Indicator for destination.
    pub dest_addr_npi: Npi,
    /// Destination address of this short message. For mobile
    /// terminated messages, this is the directory number of the
    /// recipient MS.
    pub destination_addr: COctetString<1, 21>,
}

impl SmeAddress {
    pub fn new(
        dest_addr_ton: Ton,
        dest_addr_npi: Npi,
        destination_addr: COctetString<1, 21>,
    ) -> Self {
        Self {
            dest_flag: DestFlag::SmeAddress,
            dest_addr_ton,
            dest_addr_npi,
            destination_addr,
        }
    }

    pub fn dest_flag(&self) -> DestFlag {
        self.dest_flag
    }
}

impl Length for SmeAddress {
    fn length(&self) -> usize {
        self.dest_flag.length()
            + self.dest_addr_ton.length()
            + self.dest_addr_npi.length()
            + self.destination_addr.length()
    }
}

impl Encode for SmeAddress {
    fn encode_to<W: std::io::Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
        tri!(self.dest_flag.encode_to(writer));
        tri!(self.dest_addr_ton.encode_to(writer));
        tri!(self.dest_addr_npi.encode_to(writer));
        tri!(self.destination_addr.encode_to(writer));

        Ok(())
    }
}

impl Decode for SmeAddress {
    fn decode_from<R: std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
        // flag is already read
        let dest_addr_ton = tri!(Ton::decode_from(reader));
        let dest_addr_npi = tri!(Npi::decode_from(reader));
        let destination_addr = tri!(COctetString::decode_from(reader));

        Ok(Self::new(dest_addr_ton, dest_addr_npi, destination_addr))
    }
}

/// Distribution List Format Destination Address.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DistributionListName {
    /// 0x02 (Distribution List).
    ///
    /// Can't and shouldn't be updated.
    dest_flag: DestFlag,
    /// Name of Distribution List.
    pub dl_name: COctetString<1, 21>,
}

impl DistributionListName {
    pub fn new(dl_name: COctetString<1, 21>) -> Self {
        Self {
            dest_flag: DestFlag::DistributionListName,
            dl_name,
        }
    }

    pub fn dest_flag(&self) -> DestFlag {
        self.dest_flag
    }
}

impl Length for DistributionListName {
    fn length(&self) -> usize {
        self.dest_flag.length() + self.dl_name.length()
    }
}

impl Encode for DistributionListName {
    fn encode_to<W: std::io::Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
        tri!(self.dest_flag.encode_to(writer));
        tri!(self.dl_name.encode_to(writer));

        Ok(())
    }
}

impl Decode for DistributionListName {
    fn decode_from<R: std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
        // flag is already read
        let dl_name = tri!(COctetString::decode_from(reader));

        Ok(Self::new(dl_name))
    }
}