nix_compat/wire/ser/
bytes.rs

1use bytes::Bytes;
2
3use super::{NixSerialize, NixWrite};
4
5impl NixSerialize for Bytes {
6    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
7    where
8        W: NixWrite,
9    {
10        writer.write_slice(self).await
11    }
12}
13
14impl NixSerialize for &[u8] {
15    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
16    where
17        W: NixWrite,
18    {
19        writer.write_slice(self).await
20    }
21}
22
23impl NixSerialize for String {
24    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
25    where
26        W: NixWrite,
27    {
28        writer.write_slice(self.as_bytes()).await
29    }
30}
31
32impl NixSerialize for str {
33    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
34    where
35        W: NixWrite,
36    {
37        writer.write_slice(self.as_bytes()).await
38    }
39}
40
41impl NixSerialize for &str {
42    async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error>
43    where
44        W: NixWrite,
45    {
46        writer.write_slice(self.as_bytes()).await
47    }
48}
49
50#[cfg(test)]
51mod test {
52    use hex_literal::hex;
53    use rstest::rstest;
54    use tokio::io::AsyncWriteExt as _;
55    use tokio_test::io::Builder;
56
57    use crate::wire::ser::{NixWrite, NixWriter};
58
59    #[rstest]
60    #[case::empty("", &hex!("0000 0000 0000 0000"))]
61    #[case::one(")", &hex!("0100 0000 0000 0000 2900 0000 0000 0000"))]
62    #[case::two("it", &hex!("0200 0000 0000 0000 6974 0000 0000 0000"))]
63    #[case::three("tea", &hex!("0300 0000 0000 0000 7465 6100 0000 0000"))]
64    #[case::four("were", &hex!("0400 0000 0000 0000 7765 7265 0000 0000"))]
65    #[case::five("where", &hex!("0500 0000 0000 0000 7768 6572 6500 0000"))]
66    #[case::six("unwrap", &hex!("0600 0000 0000 0000 756E 7772 6170 0000"))]
67    #[case::seven("where's", &hex!("0700 0000 0000 0000 7768 6572 6527 7300"))]
68    #[case::aligned("read_tea", &hex!("0800 0000 0000 0000 7265 6164 5F74 6561"))]
69    #[case::more_bytes("read_tess", &hex!("0900 0000 0000 0000 7265 6164 5F74 6573 7300 0000 0000 0000"))]
70    #[case::utf8("The quick brown 🦊 jumps over 13 lazy 🐶.", &hex!("2D00 0000 0000 0000  5468 6520 7175 6963  6b20 6272 6f77 6e20  f09f a68a 206a 756d  7073 206f 7665 7220  3133 206c 617a 7920  f09f 90b6 2e00 0000"))]
71    #[tokio::test]
72    async fn test_write_str(#[case] value: &str, #[case] data: &[u8]) {
73        let mock = Builder::new().write(data).build();
74        let mut writer = NixWriter::new(mock);
75        writer.write_value(value).await.unwrap();
76        writer.flush().await.unwrap();
77    }
78
79    #[rstest]
80    #[case::empty("", &hex!("0000 0000 0000 0000"))]
81    #[case::one(")", &hex!("0100 0000 0000 0000 2900 0000 0000 0000"))]
82    #[case::two("it", &hex!("0200 0000 0000 0000 6974 0000 0000 0000"))]
83    #[case::three("tea", &hex!("0300 0000 0000 0000 7465 6100 0000 0000"))]
84    #[case::four("were", &hex!("0400 0000 0000 0000 7765 7265 0000 0000"))]
85    #[case::five("where", &hex!("0500 0000 0000 0000 7768 6572 6500 0000"))]
86    #[case::six("unwrap", &hex!("0600 0000 0000 0000 756E 7772 6170 0000"))]
87    #[case::seven("where's", &hex!("0700 0000 0000 0000 7768 6572 6527 7300"))]
88    #[case::aligned("read_tea", &hex!("0800 0000 0000 0000 7265 6164 5F74 6561"))]
89    #[case::more_bytes("read_tess", &hex!("0900 0000 0000 0000 7265 6164 5F74 6573 7300 0000 0000 0000"))]
90    #[case::utf8("The quick brown 🦊 jumps over 13 lazy 🐶.", &hex!("2D00 0000 0000 0000  5468 6520 7175 6963  6b20 6272 6f77 6e20  f09f a68a 206a 756d  7073 206f 7665 7220  3133 206c 617a 7920  f09f 90b6 2e00 0000"))]
91    #[tokio::test]
92    async fn test_write_string(#[case] value: &str, #[case] data: &[u8]) {
93        let mock = Builder::new().write(data).build();
94        let mut writer = NixWriter::new(mock);
95        writer.write_value(&value.to_string()).await.unwrap();
96        writer.flush().await.unwrap();
97    }
98}