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 crate::config::ConfigParam;
use chain_core::{
    packer::Codec,
    property::{DeserializeFromSlice, ReadError, Serialize, WriteError},
};

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ConfigParams(pub(crate) Vec<ConfigParam>);

impl ConfigParams {
    pub fn new() -> Self {
        ConfigParams(Vec::new())
    }

    pub fn push(&mut self, config: ConfigParam) {
        self.0.push(config)
    }

    pub fn iter(&self) -> std::slice::Iter<ConfigParam> {
        self.0.iter()
    }
}

impl Serialize for ConfigParams {
    fn serialized_size(&self) -> usize {
        let mut res = Codec::u16_size();
        for config in &self.0 {
            res += config.serialized_size();
        }
        res
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        // FIXME: put params in canonical order (e.g. sorted by tag)?
        codec.put_be_u16(self.0.len() as u16)?;
        for config in &self.0 {
            config.serialize(codec)?
        }
        Ok(())
    }
}

impl DeserializeFromSlice for ConfigParams {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        // FIXME: check canonical order?
        let len = codec.get_be_u16()?;
        let mut configs: Vec<ConfigParam> = Vec::with_capacity(len as usize);
        for _ in 0..len {
            configs.push(ConfigParam::deserialize_from_slice(codec)?);
        }
        Ok(ConfigParams(configs))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    quickcheck! {
        fn config_params_serialize(params: ConfigParams) -> bool {
            use chain_core::property::{Serialize as _,};
            let bytes = params.serialize_as_vec().unwrap();
            let decoded = ConfigParams::deserialize_from_slice(&mut Codec::new(bytes.as_slice())).unwrap();

            params == decoded
        }

        fn config_params_serialize_readable(params: ConfigParams) -> bool {
            use chain_core::property::Serialize as _;
            let bytes = params.serialize_as_vec().unwrap();
            let decoded = ConfigParams::deserialize_from_slice(&mut Codec::new(bytes.as_slice())).unwrap();

            params == decoded
        }
    }
}