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
use std::{collections::BTreeMap, future::Future};

use super::{NixDeserialize, NixRead};

#[allow(clippy::manual_async_fn)]
impl<T> NixDeserialize for Vec<T>
where
    T: NixDeserialize + Send,
{
    fn try_deserialize<R>(
        reader: &mut R,
    ) -> impl Future<Output = Result<Option<Self>, R::Error>> + Send + '_
    where
        R: ?Sized + NixRead + Send,
    {
        async move {
            if let Some(len) = reader.try_read_value::<usize>().await? {
                let mut ret = Vec::with_capacity(len);
                for _ in 0..len {
                    ret.push(reader.read_value().await?);
                }
                Ok(Some(ret))
            } else {
                Ok(None)
            }
        }
    }
}

#[allow(clippy::manual_async_fn)]
impl<K, V> NixDeserialize for BTreeMap<K, V>
where
    K: NixDeserialize + Ord + Send,
    V: NixDeserialize + Send,
{
    fn try_deserialize<R>(
        reader: &mut R,
    ) -> impl Future<Output = Result<Option<Self>, R::Error>> + Send + '_
    where
        R: ?Sized + NixRead + Send,
    {
        async move {
            if let Some(len) = reader.try_read_value::<usize>().await? {
                let mut ret = BTreeMap::new();
                for _ in 0..len {
                    let key = reader.read_value().await?;
                    let value = reader.read_value().await?;
                    ret.insert(key, value);
                }
                Ok(Some(ret))
            } else {
                Ok(None)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::collections::BTreeMap;
    use std::fmt;

    use hex_literal::hex;
    use rstest::rstest;
    use tokio_test::io::Builder;

    use crate::wire::de::{NixDeserialize, NixRead, NixReader};

    #[rstest]
    #[case::empty(vec![], &hex!("0000 0000 0000 0000"))]
    #[case::one(vec![0x29], &hex!("0100 0000 0000 0000 2900 0000 0000 0000"))]
    #[case::two(vec![0x7469, 10], &hex!("0200 0000 0000 0000 6974 0000 0000 0000 0A00 0000 0000 0000"))]
    #[tokio::test]
    async fn test_read_small_vec(#[case] expected: Vec<usize>, #[case] data: &[u8]) {
        let mock = Builder::new().read(data).build();
        let mut reader = NixReader::new(mock);
        let actual: Vec<usize> = reader.read_value().await.unwrap();
        assert_eq!(actual, expected);
    }

    fn empty_map() -> BTreeMap<usize, u64> {
        BTreeMap::new()
    }
    macro_rules! map {
        ($($key:expr => $value:expr),*) => {{
            let mut ret = BTreeMap::new();
            $(ret.insert($key, $value);)*
            ret
        }};
    }

    #[rstest]
    #[case::empty(empty_map(), &hex!("0000 0000 0000 0000"))]
    #[case::one(map![0x7469usize => 10u64], &hex!("0100 0000 0000 0000 6974 0000 0000 0000 0A00 0000 0000 0000"))]
    #[tokio::test]
    async fn test_read_small_btree_map<E>(#[case] expected: E, #[case] data: &[u8])
    where
        E: NixDeserialize + PartialEq + fmt::Debug,
    {
        let mock = Builder::new().read(data).build();
        let mut reader = NixReader::new(mock);
        let actual: E = reader.read_value().await.unwrap();
        assert_eq!(actual, expected);
    }
}