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
use std::io::{Read, Write, Cursor};
use crate::{
encoding::*,
string::XmlElement,
node_id::NodeId,
byte_string::ByteString,
status_codes::StatusCode,
};
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub enum ExtensionObjectEncoding {
None,
ByteString(ByteString),
XmlElement(XmlElement),
}
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionObject {
pub node_id: NodeId,
pub body: ExtensionObjectEncoding,
}
impl BinaryEncoder<ExtensionObject> for ExtensionObject {
fn byte_len(&self) -> usize {
let mut size = self.node_id.byte_len();
size += match self.body {
ExtensionObjectEncoding::None => 1,
ExtensionObjectEncoding::ByteString(ref value) => {
1 + value.byte_len()
}
ExtensionObjectEncoding::XmlElement(ref value) => {
1 + value.byte_len()
}
};
size
}
fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
let mut size = 0;
size += self.node_id.encode(stream)?;
match self.body {
ExtensionObjectEncoding::None => {
size += write_u8(stream, 0x0)?;
}
ExtensionObjectEncoding::ByteString(ref value) => {
size += write_u8(stream, 0x1)?;
size += value.encode(stream)?;
}
ExtensionObjectEncoding::XmlElement(ref value) => {
size += write_u8(stream, 0x2)?;
size += value.encode(stream)?;
}
}
assert_eq!(size, self.byte_len());
Ok(size)
}
fn decode<S: Read>(stream: &mut S, decoding_limits: &DecodingLimits) -> EncodingResult<Self> {
let node_id = NodeId::decode(stream, decoding_limits)?;
let encoding_type = u8::decode(stream, decoding_limits)?;
let body = match encoding_type {
0x0 => {
ExtensionObjectEncoding::None
}
0x1 => {
ExtensionObjectEncoding::ByteString(ByteString::decode(stream, decoding_limits)?)
}
0x2 => {
ExtensionObjectEncoding::XmlElement(XmlElement::decode(stream, decoding_limits)?)
}
_ => {
error!("Invalid encoding type {} in stream", encoding_type);
return Err(StatusCode::BadDecodingError);
}
};
Ok(ExtensionObject {
node_id,
body,
})
}
}
impl ExtensionObject {
pub fn null() -> ExtensionObject {
ExtensionObject {
node_id: NodeId::null(),
body: ExtensionObjectEncoding::None,
}
}
pub fn is_null(&self) -> bool {
self.node_id.is_null()
}
pub fn is_empty(&self) -> bool {
match self.body {
ExtensionObjectEncoding::None => true,
_ => false
}
}
pub fn from_encodable<N, T>(node_id: N, encodable: &T) -> ExtensionObject where N: 'static + Into<NodeId>,
T: BinaryEncoder<T> {
let mut stream = Cursor::new(vec![0u8; encodable.byte_len()]);
let _ = encodable.encode(&mut stream);
ExtensionObject {
node_id: node_id.into(),
body: ExtensionObjectEncoding::ByteString(ByteString::from(stream.into_inner())),
}
}
pub fn decode_inner<T>(&self, decoding_limits: &DecodingLimits) -> EncodingResult<T> where T: BinaryEncoder<T> {
match self.body {
ExtensionObjectEncoding::ByteString(ref byte_string) => {
if let Some(ref value) = byte_string.value {
let mut stream = Cursor::new(value);
T::decode(&mut stream, decoding_limits)
} else {
Err(StatusCode::BadDecodingError)
}
}
_ => {
error!("decode_inner called on an unsupported ExtensionObject type");
Err(StatusCode::BadDecodingError)
}
}
}
}