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
|
use std::vec::Vec;
use log::trace;
use crate::error::ProtocolError;
use crate::{deserialize::*, serialize::*};
use crate::primitive::Variant;
/// VariantLists are represented as a Vec of Variants.
///
/// They are serialized as the amount of entries as a i32 and then a Variant for each entry
pub type VariantList = Vec<Variant>;
impl Serialize for VariantList {
fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
let len: i32 = self.len().try_into()?;
let mut res: Vec<u8> = Vec::new();
res.extend(len.to_be_bytes().iter());
for v in self {
res.extend(v.serialize()?.iter());
}
return Ok(res);
}
}
impl Deserialize for VariantList {
fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
let (_, len) = i32::parse(&b[0..4])?;
trace!(target: "primitive::VariantList", "Parsing VariantList with {:?} elements", len);
let mut res: VariantList = VariantList::new();
let mut pos: usize = 4;
for i in 0..len {
trace!(target: "primitive::VariantList", "Parsing VariantList element: {:?}", i);
let (vlen, val) = Variant::parse(&b[pos..])?;
trace!("parsed variant: {:?}", val);
res.push(val);
pos += vlen;
}
return Ok((pos, res));
}
}
impl<S> crate::message::NetworkMap for Vec<S>
where
S: std::convert::TryFrom<Variant> + Into<Variant> + Clone + std::hash::Hash + std::cmp::Eq,
<S as TryFrom<Variant>>::Error: std::fmt::Debug,
{
type Item = VariantList;
fn to_network_map(&self) -> VariantList {
self.iter().map(|i| i.clone().into()).collect()
}
fn from_network_map(input: &mut VariantList) -> Self {
input.iter().map(|i| i.clone().try_into().unwrap()).collect()
}
}
impl<S> crate::message::NetworkList for Vec<S>
where
S: std::convert::TryFrom<Variant> + Into<Variant> + Clone + std::hash::Hash + std::cmp::Eq,
<S as TryFrom<Variant>>::Error: std::fmt::Debug,
{
fn to_network_list(&self) -> VariantList {
self.iter().map(|i| i.clone().into()).collect()
}
fn from_network_list(input: &mut VariantList) -> Self {
input.iter().map(|i| i.clone().try_into().unwrap()).collect()
}
}
|