Skip to main content

nix_compat/wire/ser/
bytes.rs

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