rusmpp_core/pdus/owned/
broadcast_sm_resp.rs

1use rusmpp_macros::Rusmpp;
2
3use crate::{
4    pdus::owned::Pdu,
5    tlvs::owned::{BroadcastResponseTlvValue, Tlv},
6    types::owned::COctetString,
7};
8
9#[derive(Default, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Rusmpp)]
10#[rusmpp(decode = owned, test = skip)]
11#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
12#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
13#[cfg_attr(feature = "serde-deserialize-unchecked", derive(::serde::Deserialize))]
14pub struct BroadcastSmResp {
15    /// This field contains the MC message ID of the submitted
16    /// message. It may be used at a later stage to perform
17    /// subsequent operations on the message.
18    pub message_id: COctetString<1, 65>,
19    /// Broadcast response TLVs ([`BroadcastResponseTlvValue`]).
20    #[rusmpp(length = "unchecked")]
21    tlvs: alloc::vec::Vec<Tlv>,
22}
23
24impl BroadcastSmResp {
25    pub fn new(
26        message_id: COctetString<1, 65>,
27        tlvs: alloc::vec::Vec<BroadcastResponseTlvValue>,
28    ) -> Self {
29        let tlvs = tlvs.into_iter().map(From::from).collect();
30
31        Self { message_id, tlvs }
32    }
33
34    pub fn tlvs(&self) -> &[Tlv] {
35        &self.tlvs
36    }
37
38    pub fn set_tlvs(&mut self, tlvs: alloc::vec::Vec<BroadcastResponseTlvValue>) {
39        self.tlvs = tlvs.into_iter().map(From::from).collect();
40    }
41
42    pub fn clear_tlvs(&mut self) {
43        self.tlvs.clear();
44    }
45
46    pub fn push_tlv(&mut self, tlv: impl Into<BroadcastResponseTlvValue>) {
47        self.tlvs.push(Tlv::from(tlv.into()));
48    }
49
50    pub fn builder() -> BroadcastSmRespBuilder {
51        BroadcastSmRespBuilder::new()
52    }
53}
54
55impl From<BroadcastSmResp> for Pdu {
56    fn from(value: BroadcastSmResp) -> Self {
57        Self::BroadcastSmResp(value)
58    }
59}
60
61#[derive(Debug, Default)]
62pub struct BroadcastSmRespBuilder {
63    inner: BroadcastSmResp,
64}
65
66impl BroadcastSmRespBuilder {
67    pub fn new() -> Self {
68        Self::default()
69    }
70
71    pub fn message_id(mut self, message_id: COctetString<1, 65>) -> Self {
72        self.inner.message_id = message_id;
73        self
74    }
75
76    pub fn tlvs(mut self, tlvs: alloc::vec::Vec<BroadcastResponseTlvValue>) -> Self {
77        self.inner.set_tlvs(tlvs);
78        self
79    }
80
81    pub fn clear_tlvs(mut self) -> Self {
82        self.inner.clear_tlvs();
83        self
84    }
85
86    pub fn push_tlv(mut self, tlv: impl Into<BroadcastResponseTlvValue>) -> Self {
87        self.inner.push_tlv(tlv);
88        self
89    }
90
91    pub fn build(self) -> BroadcastSmResp {
92        self.inner
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use std::str::FromStr;
99
100    use crate::{
101        CommandStatus,
102        tests::TestInstance,
103        types::owned::AnyOctetString,
104        values::{owned::*, *},
105    };
106
107    use super::*;
108
109    impl TestInstance for BroadcastSmResp {
110        fn instances() -> alloc::vec::Vec<Self> {
111            alloc::vec![
112                Self::default(),
113                Self::builder()
114                    .message_id(
115                        COctetString::from_str("12345678901234567890123456789012345678901234")
116                            .unwrap(),
117                    )
118                    .build(),
119                Self::builder()
120                    .message_id(
121                        COctetString::from_str("12345678901234567890123456789012345678901234")
122                            .unwrap(),
123                    )
124                    .push_tlv(BroadcastResponseTlvValue::BroadcastErrorStatus(
125                        CommandStatus::EsmeRalybnd,
126                    ))
127                    .build(),
128                Self::builder()
129                    .message_id(
130                        COctetString::from_str("12345678901234567890123456789012345678901234")
131                            .unwrap(),
132                    )
133                    .tlvs(alloc::vec![
134                        BroadcastResponseTlvValue::BroadcastErrorStatus(
135                            CommandStatus::EsmeRbcastcancelfail,
136                        ),
137                        BroadcastResponseTlvValue::BroadcastAreaIdentifier(
138                            BroadcastAreaIdentifier::new(
139                                BroadcastAreaFormat::Polygon,
140                                AnyOctetString::from_static_slice(b"Polygon Area"),
141                            ),
142                        ),
143                    ])
144                    .build(),
145            ]
146        }
147    }
148
149    #[test]
150    fn encode_decode() {
151        crate::tests::owned::encode_decode_with_length_test_instances::<BroadcastSmResp>();
152    }
153}