rusmpp_core/command/
owned.rs1use rusmpp_macros::Rusmpp;
2
3use crate::{CommandId, CommandStatus, pdus::owned::Pdu};
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Rusmpp)]
39#[rusmpp(decode = owned)]
40#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
41#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
42#[cfg_attr(feature = "serde-deserialize-unchecked", derive(::serde::Deserialize))]
43pub struct Command {
44 id: CommandId,
46 pub status: CommandStatus,
48 pub sequence_number: u32,
52 #[rusmpp(key = id, length = "unchecked")]
56 pdu: Option<Pdu>,
57}
58
59impl Default for Command {
60 fn default() -> Self {
61 Self {
62 id: CommandId::EnquireLink,
63 status: CommandStatus::EsmeRok,
64 sequence_number: 0,
65 pdu: Some(Pdu::EnquireLink),
66 }
67 }
68}
69
70impl Command {
71 pub fn new(status: CommandStatus, sequence_number: u32, pdu: impl Into<Pdu>) -> Self {
72 Self::new_const(status, sequence_number, pdu.into())
73 }
74
75 pub const fn new_const(status: CommandStatus, sequence_number: u32, pdu: Pdu) -> Self {
76 let id = pdu.command_id();
77
78 Self {
79 id,
80 status,
81 sequence_number,
82 pdu: Some(pdu),
83 }
84 }
85
86 #[inline]
87 pub const fn id(&self) -> CommandId {
88 self.id
89 }
90
91 #[inline]
92 pub const fn status(&self) -> CommandStatus {
93 self.status
94 }
95
96 #[inline]
97 pub const fn sequence_number(&self) -> u32 {
98 self.sequence_number
99 }
100
101 #[inline]
102 pub const fn pdu(&self) -> Option<&Pdu> {
103 self.pdu.as_ref()
104 }
105
106 #[inline]
107 pub fn set_pdu(&mut self, pdu: impl Into<Pdu>) {
108 let pdu = pdu.into();
109
110 self.id = pdu.command_id();
111
112 self.pdu = Some(pdu);
113 }
114
115 #[inline]
116 pub fn builder() -> CommandStatusBuilder {
117 Default::default()
118 }
119}
120
121#[derive(Debug, Default)]
122pub struct CommandStatusBuilder {
123 inner: Command,
124}
125
126impl CommandStatusBuilder {
127 #[inline]
128 pub fn status(mut self, status: CommandStatus) -> SequenceNumberBuilder {
129 self.inner.status = status;
130
131 SequenceNumberBuilder { inner: self.inner }
132 }
133}
134
135#[derive(Debug)]
136pub struct SequenceNumberBuilder {
137 inner: Command,
138}
139
140impl SequenceNumberBuilder {
141 #[inline]
142 pub fn sequence_number(mut self, sequence_number: u32) -> PduBuilder {
143 self.inner.sequence_number = sequence_number;
144
145 PduBuilder { inner: self.inner }
146 }
147}
148
149#[derive(Debug)]
150pub struct PduBuilder {
151 inner: Command,
152}
153
154impl PduBuilder {
155 #[inline]
156 pub fn pdu(mut self, pdu: impl Into<Pdu>) -> Command {
157 self.inner.set_pdu(pdu);
158 self.inner
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn encode_decode() {
168 crate::tests::owned::encode_decode_with_length_test_instances::<Command>();
169 }
170}