nix_compat/wire/ser/
mod.rs

1use std::error::Error as StdError;
2use std::future::Future;
3use std::{fmt, io};
4
5use super::ProtocolVersion;
6
7mod bytes;
8mod collections;
9#[cfg(feature = "nix-compat-derive")]
10mod display;
11mod int;
12#[cfg(any(test, feature = "test"))]
13pub mod mock;
14mod writer;
15
16pub use writer::{NixWriter, NixWriterBuilder};
17
18pub trait Error: Sized + StdError {
19    fn custom<T: fmt::Display>(msg: T) -> Self;
20
21    fn io_error(err: std::io::Error) -> Self {
22        Self::custom(format_args!("There was an I/O error {}", err))
23    }
24
25    fn unsupported_data<T: fmt::Display>(msg: T) -> Self {
26        Self::custom(msg)
27    }
28
29    fn invalid_enum<T: fmt::Display>(msg: T) -> Self {
30        Self::custom(msg)
31    }
32}
33
34impl Error for io::Error {
35    fn custom<T: fmt::Display>(msg: T) -> Self {
36        io::Error::new(io::ErrorKind::Other, msg.to_string())
37    }
38
39    fn io_error(err: std::io::Error) -> Self {
40        err
41    }
42
43    fn unsupported_data<T: fmt::Display>(msg: T) -> Self {
44        io::Error::new(io::ErrorKind::InvalidData, msg.to_string())
45    }
46}
47
48pub trait NixWrite: Send {
49    type Error: Error;
50
51    /// Some types are serialized differently depending on the version
52    /// of the protocol and so this can be used for implementing that.
53    fn version(&self) -> ProtocolVersion;
54
55    /// Write a single u64 to the protocol.
56    fn write_number(&mut self, value: u64) -> impl Future<Output = Result<(), Self::Error>> + Send;
57
58    /// Write a slice of bytes to the protocol.
59    fn write_slice(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Self::Error>> + Send;
60
61    /// Write a value that implements `std::fmt::Display` to the protocol.
62    /// The protocol uses many small string formats and instead of allocating
63    /// a `String` each time we want to write one an implementation of `NixWrite`
64    /// can instead use `Display` to dump these formats to a reusable buffer.
65    fn write_display<D>(&mut self, msg: D) -> impl Future<Output = Result<(), Self::Error>> + Send
66    where
67        D: fmt::Display + Send,
68        Self: Sized,
69    {
70        async move {
71            let s = msg.to_string();
72            self.write_slice(s.as_bytes()).await
73        }
74    }
75
76    /// Write a value to the protocol.
77    /// Uses `NixSerialize::serialize` to write the value.
78    fn write_value<V>(&mut self, value: &V) -> impl Future<Output = Result<(), Self::Error>> + Send
79    where
80        V: NixSerialize + Send + ?Sized,
81        Self: Sized,
82    {
83        value.serialize(self)
84    }
85}
86
87impl<T: NixWrite> NixWrite for &mut T {
88    type Error = T::Error;
89
90    fn version(&self) -> ProtocolVersion {
91        (**self).version()
92    }
93
94    fn write_number(&mut self, value: u64) -> impl Future<Output = Result<(), Self::Error>> + Send {
95        (**self).write_number(value)
96    }
97
98    fn write_slice(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Self::Error>> + Send {
99        (**self).write_slice(buf)
100    }
101
102    fn write_display<D>(&mut self, msg: D) -> impl Future<Output = Result<(), Self::Error>> + Send
103    where
104        D: fmt::Display + Send,
105        Self: Sized,
106    {
107        (**self).write_display(msg)
108    }
109
110    fn write_value<V>(&mut self, value: &V) -> impl Future<Output = Result<(), Self::Error>> + Send
111    where
112        V: NixSerialize + Send + ?Sized,
113        Self: Sized,
114    {
115        (**self).write_value(value)
116    }
117}
118
119pub trait NixSerialize {
120    /// Write a value to the writer.
121    fn serialize<W>(&self, writer: &mut W) -> impl Future<Output = Result<(), W::Error>> + Send
122    where
123        W: NixWrite;
124}
125
126// Noop
127impl NixSerialize for () {
128    async fn serialize<W>(&self, _writer: &mut W) -> Result<(), W::Error>
129    where
130        W: NixWrite,
131    {
132        Ok(())
133    }
134}