use std::vec::Vec; use failure::Error; use log::trace; 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; impl Serialize for VariantList { fn serialize(&self) -> Result, Error> { let len: i32 = self.len().try_into()?; let mut res: Vec = 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), Error> { 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 crate::message::NetworkMap for Vec where S: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, >::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 crate::message::NetworkList for Vec where S: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, >::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() } }