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
use std::io;
use std::io::Read;
use crate::pdu::formats::{COctetString, WriteStream};
use crate::pdu::pduparseerror::fld;
use crate::pdu::{PduParseError, PduParseErrorBody};
const MAX_LENGTH_MESSAGE_ID: usize = 65;
#[derive(Debug, PartialEq)]
pub struct SubmitSmRespPdu {
pub message_id: Option<COctetString>,
}
impl SubmitSmRespPdu {
pub fn new(message_id: &str) -> Result<Self, PduParseError> {
Ok(Self {
message_id: Some(COctetString::from_str(
message_id,
MAX_LENGTH_MESSAGE_ID,
)?),
})
}
pub fn new_error() -> Self {
Self { message_id: None }
}
pub async fn write(&self, stream: &mut WriteStream) -> io::Result<()> {
if let Some(message_id) = &self.message_id {
message_id.write(stream).await?
}
Ok(())
}
pub fn parse(
bytes: &mut dyn io::BufRead,
command_status: u32,
) -> Result<SubmitSmRespPdu, PduParseError> {
if command_status == 0x00000000 {
let message_id = Some(fld(
"message_id",
COctetString::read(bytes, MAX_LENGTH_MESSAGE_ID),
)?);
Ok(Self { message_id })
} else {
if let Some(_) = bytes.bytes().next() {
return Err(PduParseError::new(
PduParseErrorBody::BodyNotAllowedWhenStatusIsNotZero,
));
}
Ok(Self { message_id: None })
}
}
pub fn validate_command_status(
self,
command_status: u32,
) -> Result<Self, PduParseError> {
match (&self.message_id, command_status) {
(Some(_), 0) => Ok(self),
(None, 0) => Err(PduParseError::new(
PduParseErrorBody::BodyNotAllowedWhenStatusIsNotZero,
)),
(Some(_), _) => Err(PduParseError::new(
PduParseErrorBody::BodyRequiredWhenStatusIsZero,
)),
(None, _) => Ok(self),
}
}
pub fn message_id(&self) -> Option<String> {
self.message_id.as_ref().map(|s| s.value.to_string())
}
}