blob: d2902f28b77adccc7590f812e3f4456a8491e2bd (
plain)
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
|
extern crate byteorder;
use std::convert::TryInto;
use std::result::Result;
use std::vec::Vec;
use failure::Error;
use log::trace;
use crate::protocol::primitive::{deserialize, serialize};
pub type StringList = Vec<String>;
impl serialize::Serialize for StringList {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let len: i32 = self.len().try_into()?;
let mut res: Vec<u8> = Vec::new();
res.extend(len.to_be_bytes().iter());
for x in self {
res.extend(x.serialize()?);
}
return Ok(res);
}
}
impl deserialize::Deserialize for StringList {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (_, len) = i32::parse(&b[0..4])?;
trace!(target: "protocol::primitive::StringList", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]);
let mut res: StringList = StringList::new();
let mut pos = 4;
if len > 0 {
for _ in 0..len {
let (lpos, val) = String::parse(&b[pos..])?;
pos += lpos;
res.push(val);
}
}
return Ok((pos, res));
}
}
|