use rusmpp_macros::Rusmpp;
use crate::{
decode::{
DecodeError, DecodeResultExt,
borrowed::{Decode, DecodeExt},
},
encode::Length,
types::borrowed::COctetString,
values::{dest_address::DestFlag, npi::Npi, ton::Ton},
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
pub enum DestAddress<'a> {
SmeAddress(SmeAddress<'a>),
DistributionListName(DistributionListName<'a>),
}
impl Length for DestAddress<'_> {
fn length(&self) -> usize {
match self {
Self::SmeAddress(sa) => sa.length(),
Self::DistributionListName(dlm) => dlm.length(),
}
}
}
impl crate::encode::Encode for DestAddress<'_> {
fn encode(&self, dst: &mut [u8]) -> usize {
match self {
Self::SmeAddress(sa) => sa.encode(dst),
Self::DistributionListName(dlm) => dlm.encode(dst),
}
}
}
#[cfg(feature = "alloc")]
impl crate::encode::owned::Encode for DestAddress<'_> {
fn encode(&self, dst: &mut bytes::BytesMut) {
match self {
Self::SmeAddress(sa) => sa.encode(dst),
Self::DistributionListName(dlm) => dlm.encode(dst),
}
}
}
impl<'a> Decode<'a> for DestAddress<'a> {
fn decode(src: &'a [u8]) -> Result<(Self, usize), DecodeError> {
let size = 0;
let (flag, size) = DestFlag::decode_move(src, size)?;
match flag {
DestFlag::SmeAddress => {
SmeAddress::decode_move(src, size).map_decoded(Self::SmeAddress)
}
DestFlag::DistributionListName => {
DistributionListName::decode_move(src, size).map_decoded(Self::DistributionListName)
}
DestFlag::Other(flag) => Err(DecodeError::unsupported_key(flag.into())),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Rusmpp)]
#[rusmpp(decode = borrowed, test = skip)]
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
pub struct SmeAddress<'a> {
#[rusmpp(skip_decode)]
dest_flag: DestFlag,
pub dest_addr_ton: Ton,
pub dest_addr_npi: Npi,
pub destination_addr: COctetString<'a, 1, 21>,
}
impl<'a> SmeAddress<'a> {
pub const fn new(
dest_addr_ton: Ton,
dest_addr_npi: Npi,
destination_addr: COctetString<'a, 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
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Rusmpp)]
#[rusmpp(decode = borrowed, test = skip)]
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
pub struct DistributionListName<'a> {
#[rusmpp(skip_decode)]
dest_flag: DestFlag,
pub dl_name: COctetString<'a, 1, 21>,
}
impl<'a> DistributionListName<'a> {
pub fn new(dl_name: COctetString<'a, 1, 21>) -> Self {
Self {
dest_flag: DestFlag::DistributionListName,
dl_name,
}
}
pub fn dest_flag(&self) -> DestFlag {
self.dest_flag
}
}
#[cfg(test)]
mod tests {
use super::*;
impl crate::tests::TestInstance for DestAddress<'static> {
fn instances() -> alloc::vec::Vec<Self> {
alloc::vec![
Self::SmeAddress(SmeAddress::new(
Ton::International,
Npi::Isdn,
COctetString::new(b"1234567890123456789\0").unwrap(),
)),
Self::DistributionListName(DistributionListName::new(
COctetString::new(b"1234567890123456789\0").unwrap(),
)),
]
}
}
#[test]
fn encode_decode() {
crate::tests::borrowed::encode_decode_test_instances::<DestAddress>();
}
}