nix_compat/wire/de/
mod.rs1use std::error::Error as StdError;
2use std::future::Future;
3use std::ops::RangeInclusive;
4use std::{fmt, io};
5
6use ::bytes::Bytes;
7
8use super::ProtocolVersion;
9
10mod bytes;
11mod collections;
12mod int;
13#[cfg(any(test, feature = "test"))]
14pub mod mock;
15mod reader;
16
17pub use reader::{NixReader, NixReaderBuilder};
18
19pub trait Error: Sized + StdError {
22 fn custom<T: fmt::Display>(msg: T) -> Self;
24
25 fn io_error(err: std::io::Error) -> Self {
27 Self::custom(format_args!("There was an I/O error {}", err))
28 }
29
30 fn invalid_data<T: fmt::Display>(msg: T) -> Self {
33 Self::custom(msg)
34 }
35
36 fn missing_data<T: fmt::Display>(msg: T) -> Self {
38 Self::custom(msg)
39 }
40}
41
42impl Error for io::Error {
43 fn custom<T: fmt::Display>(msg: T) -> Self {
44 io::Error::new(io::ErrorKind::Other, msg.to_string())
45 }
46
47 fn io_error(err: std::io::Error) -> Self {
48 err
49 }
50
51 fn invalid_data<T: fmt::Display>(msg: T) -> Self {
52 io::Error::new(io::ErrorKind::InvalidData, msg.to_string())
53 }
54
55 fn missing_data<T: fmt::Display>(msg: T) -> Self {
56 io::Error::new(io::ErrorKind::UnexpectedEof, msg.to_string())
57 }
58}
59
60pub trait NixRead: Send {
65 type Error: Error + Send;
66
67 fn version(&self) -> ProtocolVersion;
70
71 fn try_read_number(
74 &mut self,
75 ) -> impl Future<Output = Result<Option<u64>, Self::Error>> + Send + '_;
76
77 fn try_read_bytes_limited(
81 &mut self,
82 limit: RangeInclusive<usize>,
83 ) -> impl Future<Output = Result<Option<Bytes>, Self::Error>> + Send + '_;
84
85 fn try_read_bytes(
91 &mut self,
92 ) -> impl Future<Output = Result<Option<Bytes>, Self::Error>> + Send + '_ {
93 self.try_read_bytes_limited(0..=usize::MAX)
94 }
95
96 fn read_number(&mut self) -> impl Future<Output = Result<u64, Self::Error>> + Send + '_ {
99 async move {
100 match self.try_read_number().await? {
101 Some(v) => Ok(v),
102 None => Err(Self::Error::missing_data("unexpected end-of-file")),
103 }
104 }
105 }
106
107 fn read_bytes_limited(
111 &mut self,
112 limit: RangeInclusive<usize>,
113 ) -> impl Future<Output = Result<Bytes, Self::Error>> + Send + '_ {
114 async move {
115 match self.try_read_bytes_limited(limit).await? {
116 Some(v) => Ok(v),
117 None => Err(Self::Error::missing_data("unexpected end-of-file")),
118 }
119 }
120 }
121
122 fn read_bytes(&mut self) -> impl Future<Output = Result<Bytes, Self::Error>> + Send + '_ {
128 self.read_bytes_limited(0..=usize::MAX)
129 }
130
131 fn read_value<V: NixDeserialize>(
134 &mut self,
135 ) -> impl Future<Output = Result<V, Self::Error>> + Send + '_ {
136 V::deserialize(self)
137 }
138
139 fn try_read_value<V: NixDeserialize>(
143 &mut self,
144 ) -> impl Future<Output = Result<Option<V>, Self::Error>> + Send + '_ {
145 V::try_deserialize(self)
146 }
147}
148
149impl<T: ?Sized + NixRead> NixRead for &mut T {
150 type Error = T::Error;
151
152 fn version(&self) -> ProtocolVersion {
153 (**self).version()
154 }
155
156 fn try_read_number(
157 &mut self,
158 ) -> impl Future<Output = Result<Option<u64>, Self::Error>> + Send + '_ {
159 (**self).try_read_number()
160 }
161
162 fn try_read_bytes_limited(
163 &mut self,
164 limit: RangeInclusive<usize>,
165 ) -> impl Future<Output = Result<Option<Bytes>, Self::Error>> + Send + '_ {
166 (**self).try_read_bytes_limited(limit)
167 }
168
169 fn try_read_bytes(
170 &mut self,
171 ) -> impl Future<Output = Result<Option<Bytes>, Self::Error>> + Send + '_ {
172 (**self).try_read_bytes()
173 }
174
175 fn read_number(&mut self) -> impl Future<Output = Result<u64, Self::Error>> + Send + '_ {
176 (**self).read_number()
177 }
178
179 fn read_bytes_limited(
180 &mut self,
181 limit: RangeInclusive<usize>,
182 ) -> impl Future<Output = Result<Bytes, Self::Error>> + Send + '_ {
183 (**self).read_bytes_limited(limit)
184 }
185
186 fn read_bytes(&mut self) -> impl Future<Output = Result<Bytes, Self::Error>> + Send + '_ {
187 (**self).read_bytes()
188 }
189
190 fn try_read_value<V: NixDeserialize>(
191 &mut self,
192 ) -> impl Future<Output = Result<Option<V>, Self::Error>> + Send + '_ {
193 (**self).try_read_value()
194 }
195
196 fn read_value<V: NixDeserialize>(
197 &mut self,
198 ) -> impl Future<Output = Result<V, Self::Error>> + Send + '_ {
199 (**self).read_value()
200 }
201}
202
203pub trait NixDeserialize: Sized {
206 fn try_deserialize<R>(
209 reader: &mut R,
210 ) -> impl Future<Output = Result<Option<Self>, R::Error>> + Send + '_
211 where
212 R: ?Sized + NixRead + Send;
213
214 fn deserialize<R>(reader: &mut R) -> impl Future<Output = Result<Self, R::Error>> + Send + '_
215 where
216 R: ?Sized + NixRead + Send,
217 {
218 async move {
219 match Self::try_deserialize(reader).await? {
220 Some(v) => Ok(v),
221 None => Err(R::Error::missing_data("unexpected end-of-file")),
222 }
223 }
224 }
225}