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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::fmt;
use std::marker::PhantomData;
use bytes::{Buf, BufMut};
use crate::wire_format::WireFormat;
#[derive(Debug, Clone)]
pub struct SbpString<T, E = Unterminated> {
data: T,
encoding: PhantomData<E>,
}
impl<T, E> SbpString<T, E>
where
T: AsRef<[u8]>,
{
pub fn new(data: T) -> Self {
Self {
data,
encoding: PhantomData,
}
}
pub fn as_bytes(&self) -> &[u8] {
self.data.as_ref()
}
pub fn to_vec(&self) -> Vec<u8> {
self.data.as_ref().to_vec()
}
}
impl<E> From<String> for SbpString<Vec<u8>, E> {
fn from(s: String) -> Self {
SbpString::new(s.into_bytes())
}
}
impl<E> From<Vec<u8>> for SbpString<Vec<u8>, E> {
fn from(v: Vec<u8>) -> Self {
SbpString::new(v)
}
}
impl<T, E> fmt::Display for SbpString<T, E>
where
T: AsRef<[u8]>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", String::from_utf8_lossy(self.as_bytes()))
}
}
#[cfg(feature = "serde")]
impl<T, E> serde::Serialize for SbpString<T, E>
where
T: AsRef<[u8]>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = String::from_utf8_lossy(self.as_bytes());
serializer.serialize_str(&s)
}
}
impl<E, const LEN: usize> WireFormat for SbpString<[u8; LEN], E> {
const MIN_LEN: usize = LEN;
fn len(&self) -> usize {
LEN
}
fn write<B: BufMut>(&self, buf: &mut B) {
self.data.write(buf)
}
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
let data: [u8; LEN] = WireFormat::parse_unchecked(buf);
SbpString::new(data)
}
}
macro_rules! forward_payload_vec {
($encoding:ty, $min_len:expr) => {
impl WireFormat for SbpString<Vec<u8>, $encoding> {
const MIN_LEN: usize = $min_len;
fn len(&self) -> usize {
self.data.len()
}
fn write<B: BufMut>(&self, buf: &mut B) {
self.data.write(buf)
}
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
let data: Vec<u8> = WireFormat::parse_unchecked(buf);
SbpString::new(data)
}
}
};
}
#[derive(Debug, Clone, Copy)]
pub struct Unterminated;
forward_payload_vec!(Unterminated, 0);
#[derive(Debug, Clone, Copy)]
pub struct NullTerminated;
forward_payload_vec!(NullTerminated, 1);
#[derive(Debug, Clone, Copy)]
pub struct Multipart;
forward_payload_vec!(Multipart, 0);
#[derive(Debug, Clone, Copy)]
pub struct DoubleNullTerminated;
forward_payload_vec!(DoubleNullTerminated, 2);
#[cfg(test)]
mod tests {
use bytes::BytesMut;
use super::*;
#[test]
fn test_parse_sbp_string_vec() {
let mut buf = BytesMut::from(&b"hi, imma string"[..]);
let actual: SbpString<Vec<u8>> = WireFormat::parse_unchecked(&mut buf);
assert_eq!(actual.to_string(), "hi, imma string".to_string());
assert!(buf.is_empty());
let actual: SbpString<Vec<u8>> = WireFormat::parse_unchecked(&mut buf);
assert_eq!(actual.to_string(), "".to_string());
}
#[test]
fn test_parse_sbp_string_array() {
let mut buf = BytesMut::from(&b"hi, imma string"[..]);
let actual: SbpString<[u8; 8]> = WireFormat::parse_unchecked(&mut buf);
assert_eq!(actual.to_string(), "hi, imma".to_string());
assert_eq!(buf.len(), 7);
let actual: SbpString<[u8; 7]> = WireFormat::parse_unchecked(&mut buf);
assert_eq!(actual.to_string(), " string".to_string());
assert!(buf.is_empty());
}
#[test]
fn test_parse_sbp_string_invalid_utf8() {
let mut buf = BytesMut::from(
&[
0x73, 0x6f, 0x6c, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb6,
][..],
);
let sbp_string: SbpString<Vec<u8>> = WireFormat::parse_unchecked(&mut buf);
assert_eq!(sbp_string.to_string().len(), 69 + 3);
assert_eq!(sbp_string.to_vec().len(), 70);
}
}