nix_remote/
serialize.rs

1//! Serialization and deserialization for the nix remote protocol.
2//!
3//! The protocol has two primitive types: integers and byte buffers.
4//! All integers are encoded in 64 bits (little endian). I haven't seen signed integers
5//! appear in the protocol yet, but presumably they're encoded in twos complement.
6//! Byte buffers are encoded as a length (64-bit integer), followed by the bytes in the buffer.
7//! If the length of the buffer is not a multiple of 8, it is zero-padded to a multiple of
8//! 8 bytes.
9//!
10//! The Nix source parses the protocol imperatively, but there are some common patterns that
11//! we implement declaratively with the help of serde's derive macros:
12//! - structs and tuples are serialized as the concatenation of their fields
13//!   (Nix does this manually for each struct)
14//! - sequences (like `Vec`s) are serialized as a length followed by the concatenation of the
15//!   elements (Nix has functions like `readStrings` for this).
16//!
17//! So for example, the struct
18//! ```ignore
19//! pub struct BuildPathsWithResults {
20//!     paths: Vec<ByteBuf>,
21//!     build_mode: u64,
22//! }
23//! ```
24//! gets serde-derived serialization implementations that encode it as:
25//! - the number of paths (an int)
26//! - the paths concatenated together, each of which consists of
27//!    + a length (an int)
28//!    + a byte buffer of that length
29//! - the build mode (an int)
30//!
31//! Nix also has some sort of implicit "tagged union", consisting of a type tag (and integer)
32//! followed by a body. This serializer does not have built-in support for that, because serde
33//! enums are built on string-valued tags (whereas the nix protocol wants integer tags).
34//! Instead, we have a separate `tagged_serde` macro for transforming enums into tuples.
35
36use std::io::{Read, Write};
37
38use serde::{de, ser, Serialize};
39
40pub struct Tee<R, W> {
41    read: R,
42    write: W,
43}
44
45impl<R: Read, W: Write> Tee<R, W> {
46    pub fn new(read: R, write: W) -> Self {
47        Tee { read, write }
48    }
49}
50
51impl<R: Read, W: Write> Read for Tee<R, W> {
52    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
53        let n = self.read.read(buf)?;
54        self.write.write_all(&buf[0..n])?;
55        Ok(n)
56    }
57}
58
59#[derive(Debug, thiserror::Error)]
60pub enum Error {
61    #[error("Won't implement {0}")]
62    WontImplement(&'static str),
63    #[error("io error {0}")]
64    Io(#[from] std::io::Error),
65    #[error("Custom {0}")]
66    Custom(String),
67}
68
69impl de::Error for Error {
70    fn custom<T>(msg: T) -> Self
71    where
72        T: std::fmt::Display,
73    {
74        Error::Custom(msg.to_string())
75    }
76}
77
78impl ser::Error for Error {
79    fn custom<T>(msg: T) -> Self
80    where
81        T: std::fmt::Display,
82    {
83        Error::Custom(msg.to_string())
84    }
85}
86
87pub type Result<T, E = Error> = std::result::Result<T, E>;
88
89pub trait NixReadExt {
90    fn read_nix<'de, 'a: 'de, D: serde::Deserialize<'de>>(&'a mut self) -> Result<D>;
91}
92
93impl<R: Read> NixReadExt for R {
94    fn read_nix<'de, 'a: 'de, D: serde::Deserialize<'de>>(&'a mut self) -> Result<D> {
95        D::deserialize(&mut NixDeserializer { read: self })
96    }
97}
98
99pub trait NixWriteExt {
100    fn write_nix(&mut self, val: &impl Serialize) -> Result<()>;
101}
102
103impl<W: Write> NixWriteExt for W {
104    fn write_nix(&mut self, val: &impl Serialize) -> Result<()> {
105        val.serialize(&mut NixSerializer { write: self })?;
106        Ok(())
107    }
108}
109
110/// A deserializer for the nix remote protocol.
111pub struct NixDeserializer<'de> {
112    pub read: &'de mut dyn Read,
113}
114
115/// A serializer for the nix remote protocol.
116pub struct NixSerializer<'se> {
117    pub write: &'se mut dyn Write,
118}
119
120struct Seq<'a, 'de: 'a> {
121    deserializer: &'a mut NixDeserializer<'de>,
122    len: usize,
123}
124
125impl<'a, 'de: 'a> de::SeqAccess<'de> for Seq<'a, 'de> {
126    type Error = Error;
127
128    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
129    where
130        T: de::DeserializeSeed<'de>,
131    {
132        if self.len > 0 {
133            self.len -= 1;
134            Ok(Some(de::DeserializeSeed::deserialize(
135                seed,
136                &mut *self.deserializer,
137            )?))
138        } else {
139            Ok(None)
140        }
141    }
142
143    fn size_hint(&self) -> Option<usize> {
144        Some(self.len)
145    }
146}
147
148impl<'de> NixDeserializer<'de> {
149    pub fn read_u64(&mut self) -> Result<u64> {
150        let mut buf = [0u8; 8];
151        self.read.read_exact(&mut buf)?;
152        Ok(u64::from_le_bytes(buf))
153    }
154
155    pub fn read_byte_buf(&mut self) -> Result<Vec<u8>> {
156        // possible errors:
157        // Unexecpted EOF
158        // IO Error
159        // out of memory
160        let len = self.read_u64()? as usize;
161
162        // TODO(optimization): don't initialize
163        let mut buf = vec![0; len];
164        self.read.read_exact(&mut buf)?;
165
166        if len % 8 > 0 {
167            let padding = 8 - len % 8;
168            let mut pad_buf = [0; 8];
169            self.read.read_exact(&mut pad_buf[..padding])?;
170        }
171
172        Ok(buf)
173    }
174}
175
176impl<'se> NixSerializer<'se> {
177    pub fn write_byte_buf(&mut self, s: &[u8]) -> Result<()> {
178        let len = s.len();
179
180        self.write.write_all(&len.to_le_bytes())?;
181        self.write.write_all(s)?;
182
183        if len % 8 > 0 {
184            let padding = 8 - len % 8;
185            let pad_buf = [0; 8];
186            self.write.write_all(&pad_buf[..padding])?;
187        };
188
189        Ok(())
190    }
191}
192
193impl<'de, 'a> de::Deserializer<'de> for &'a mut NixDeserializer<'de> {
194    type Error = Error;
195
196    fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
197    where
198        V: de::Visitor<'de>,
199    {
200        Err(Error::WontImplement("any"))
201    }
202
203    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
204    where
205        V: de::Visitor<'de>,
206    {
207        visitor.visit_bool(self.read_u64()? != 0)
208    }
209
210    fn deserialize_i8<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
211    where
212        V: de::Visitor<'de>,
213    {
214        Err(Error::WontImplement("i8"))
215    }
216
217    fn deserialize_i16<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
218    where
219        V: de::Visitor<'de>,
220    {
221        Err(Error::WontImplement("i16"))
222    }
223
224    fn deserialize_i32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
225    where
226        V: de::Visitor<'de>,
227    {
228        Err(Error::WontImplement("i32"))
229    }
230
231    fn deserialize_i64<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
232    where
233        V: de::Visitor<'de>,
234    {
235        Err(Error::WontImplement("i64"))
236    }
237
238    fn deserialize_u8<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
239    where
240        V: de::Visitor<'de>,
241    {
242        Err(Error::WontImplement("u8"))
243    }
244
245    fn deserialize_u16<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
246    where
247        V: de::Visitor<'de>,
248    {
249        Err(Error::WontImplement("u16"))
250    }
251
252    fn deserialize_u32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
253    where
254        V: de::Visitor<'de>,
255    {
256        Err(Error::WontImplement("u32"))
257    }
258
259    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
260    where
261        V: de::Visitor<'de>,
262    {
263        visitor.visit_u64(self.read_u64()?)
264    }
265
266    fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
267    where
268        V: de::Visitor<'de>,
269    {
270        Err(Error::WontImplement("f32"))
271    }
272
273    fn deserialize_f64<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
274    where
275        V: de::Visitor<'de>,
276    {
277        Err(Error::WontImplement("f64"))
278    }
279
280    fn deserialize_char<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
281    where
282        V: de::Visitor<'de>,
283    {
284        Err(Error::WontImplement("char"))
285    }
286
287    fn deserialize_str<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
288    where
289        V: de::Visitor<'de>,
290    {
291        Err(Error::WontImplement("str"))
292    }
293
294    fn deserialize_string<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
295    where
296        V: de::Visitor<'de>,
297    {
298        Err(Error::WontImplement("String"))
299    }
300
301    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
302    where
303        V: de::Visitor<'de>,
304    {
305        self.deserialize_byte_buf(visitor)
306    }
307
308    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
309    where
310        V: de::Visitor<'de>,
311    {
312        visitor.visit_byte_buf(self.read_byte_buf()?)
313    }
314
315    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
316    where
317        V: de::Visitor<'de>,
318    {
319        let tag = self.read_u64()?;
320        if tag == 1 {
321            visitor.visit_some(self)
322        } else {
323            visitor.visit_none()
324        }
325    }
326
327    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
328    where
329        V: de::Visitor<'de>,
330    {
331        visitor.visit_unit()
332    }
333
334    fn deserialize_unit_struct<V>(
335        self,
336        _name: &'static str,
337        visitor: V,
338    ) -> Result<V::Value, Self::Error>
339    where
340        V: de::Visitor<'de>,
341    {
342        self.deserialize_unit(visitor)
343    }
344
345    fn deserialize_newtype_struct<V>(
346        self,
347        _name: &'static str,
348        visitor: V,
349    ) -> Result<V::Value, Self::Error>
350    where
351        V: de::Visitor<'de>,
352    {
353        visitor.visit_newtype_struct(self)
354    }
355
356    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
357    where
358        V: de::Visitor<'de>,
359    {
360        let len = self.read_u64()? as usize;
361        visitor.visit_seq(Seq {
362            deserializer: self,
363            len,
364        })
365    }
366
367    fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
368    where
369        V: de::Visitor<'de>,
370    {
371        visitor.visit_seq(Seq {
372            deserializer: self,
373            len,
374        })
375    }
376
377    fn deserialize_tuple_struct<V>(
378        self,
379        _name: &'static str,
380        len: usize,
381        visitor: V,
382    ) -> Result<V::Value, Self::Error>
383    where
384        V: de::Visitor<'de>,
385    {
386        visitor.visit_seq(Seq {
387            deserializer: self,
388            len,
389        })
390    }
391
392    fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
393    where
394        V: de::Visitor<'de>,
395    {
396        Err(Error::WontImplement("map"))
397    }
398
399    fn deserialize_struct<V>(
400        self,
401        _name: &'static str,
402        fields: &'static [&'static str],
403        visitor: V,
404    ) -> Result<V::Value, Self::Error>
405    where
406        V: de::Visitor<'de>,
407    {
408        self.deserialize_tuple(fields.len(), visitor)
409    }
410
411    fn deserialize_enum<V>(
412        self,
413        _name: &'static str,
414        _variants: &'static [&'static str],
415        _visitor: V,
416    ) -> Result<V::Value, Self::Error>
417    where
418        V: de::Visitor<'de>,
419    {
420        Err(Error::WontImplement("enum"))
421    }
422
423    fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
424    where
425        V: de::Visitor<'de>,
426    {
427        Err(Error::WontImplement("ident"))
428    }
429
430    fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
431    where
432        V: de::Visitor<'de>,
433    {
434        Err(Error::WontImplement("ignored"))
435    }
436}
437
438impl<'se> ser::SerializeSeq for &mut NixSerializer<'se> {
439    type Ok = ();
440    type Error = Error;
441
442    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
443    where
444        T: Serialize,
445    {
446        value.serialize(&mut **self)
447    }
448
449    fn end(self) -> Result<Self::Ok, Self::Error> {
450        Ok(())
451    }
452}
453
454impl<'se> ser::SerializeTuple for &mut NixSerializer<'se> {
455    type Ok = ();
456    type Error = Error;
457
458    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
459    where
460        T: Serialize,
461    {
462        value.serialize(&mut **self)
463    }
464
465    fn end(self) -> Result<Self::Ok, Self::Error> {
466        Ok(())
467    }
468}
469
470impl<'se> ser::SerializeTupleStruct for &mut NixSerializer<'se> {
471    type Ok = ();
472    type Error = Error;
473
474    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
475    where
476        T: Serialize,
477    {
478        value.serialize(&mut **self)
479    }
480
481    fn end(self) -> Result<Self::Ok, Self::Error> {
482        Ok(())
483    }
484}
485
486impl<'se> ser::SerializeTupleVariant for &mut NixSerializer<'se> {
487    type Ok = ();
488    type Error = Error;
489
490    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
491    where
492        T: Serialize,
493    {
494        value.serialize(&mut **self)
495    }
496
497    fn end(self) -> Result<Self::Ok, Self::Error> {
498        Ok(())
499    }
500}
501
502impl<'se> ser::SerializeMap for &mut NixSerializer<'se> {
503    type Ok = ();
504    type Error = Error;
505
506    fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
507    where
508        T: Serialize,
509    {
510        key.serialize(&mut **self)
511    }
512
513    fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
514    where
515        T: Serialize,
516    {
517        value.serialize(&mut **self)
518    }
519
520    fn end(self) -> Result<Self::Ok, Self::Error> {
521        Ok(())
522    }
523}
524
525impl<'se> ser::SerializeStruct for &mut NixSerializer<'se> {
526    type Ok = ();
527    type Error = Error;
528
529    fn serialize_field<T: ?Sized>(
530        &mut self,
531        _name: &'static str,
532        value: &T,
533    ) -> Result<(), Self::Error>
534    where
535        T: Serialize,
536    {
537        value.serialize(&mut **self)
538    }
539
540    fn end(self) -> Result<Self::Ok, Self::Error> {
541        Ok(())
542    }
543}
544
545impl<'se> ser::SerializeStructVariant for &mut NixSerializer<'se> {
546    type Ok = ();
547    type Error = Error;
548
549    fn serialize_field<T: ?Sized>(
550        &mut self,
551        _name: &'static str,
552        value: &T,
553    ) -> Result<(), Self::Error>
554    where
555        T: Serialize,
556    {
557        value.serialize(&mut **self)
558    }
559
560    fn end(self) -> Result<Self::Ok, Self::Error> {
561        Ok(())
562    }
563}
564
565impl<'se> serde::Serializer for &mut NixSerializer<'se> {
566    type Ok = ();
567
568    type Error = Error;
569
570    type SerializeSeq = Self;
571
572    type SerializeTuple = Self;
573
574    type SerializeTupleStruct = Self;
575
576    type SerializeTupleVariant = Self;
577
578    type SerializeMap = Self;
579
580    type SerializeStruct = Self;
581
582    type SerializeStructVariant = Self;
583
584    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
585        self.serialize_u64(v as u64)
586    }
587
588    fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
589        Err(Error::WontImplement("i8"))
590    }
591
592    fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
593        Err(Error::WontImplement("i16"))
594    }
595
596    fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
597        Err(Error::WontImplement("i32"))
598    }
599
600    fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
601        Err(Error::WontImplement("i64"))
602    }
603
604    fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
605        Err(Error::WontImplement("u8"))
606    }
607
608    fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
609        Err(Error::WontImplement("u16"))
610    }
611
612    fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
613        Err(Error::WontImplement("u32"))
614    }
615
616    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
617        Ok(self.write.write_all(&v.to_le_bytes())?)
618    }
619
620    fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
621        Err(Error::WontImplement("f32"))
622    }
623
624    fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
625        Err(Error::WontImplement("f64"))
626    }
627
628    fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
629        Err(Error::WontImplement("char"))
630    }
631
632    fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
633        Err(Error::WontImplement("String"))
634    }
635
636    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
637        self.write_byte_buf(v)
638    }
639
640    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
641        self.write.write_all(&0u64.to_le_bytes())?;
642        Ok(())
643    }
644
645    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
646    where
647        T: Serialize,
648    {
649        self.write.write_all(&1u64.to_le_bytes())?;
650        value.serialize(self)
651    }
652
653    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
654        Ok(())
655    }
656
657    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
658        Ok(())
659    }
660
661    fn serialize_unit_variant(
662        self,
663        _name: &'static str,
664        _variant_index: u32,
665        _variant: &'static str,
666    ) -> Result<Self::Ok, Self::Error> {
667        Err(Error::WontImplement("unit variant"))
668    }
669
670    fn serialize_newtype_struct<T: ?Sized>(
671        self,
672        _name: &'static str,
673        value: &T,
674    ) -> Result<Self::Ok, Self::Error>
675    where
676        T: Serialize,
677    {
678        value.serialize(self)
679    }
680
681    fn serialize_newtype_variant<T: ?Sized>(
682        self,
683        _name: &'static str,
684        _variant_index: u32,
685        _variant: &'static str,
686        value: &T,
687    ) -> Result<Self::Ok, Self::Error>
688    where
689        T: Serialize,
690    {
691        value.serialize(self)
692    }
693
694    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
695        self.serialize_u64(len.unwrap() as u64)?;
696        Ok(self)
697    }
698
699    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
700        Ok(self)
701    }
702
703    fn serialize_tuple_struct(
704        self,
705        _name: &'static str,
706        len: usize,
707    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
708        self.serialize_tuple(len)
709    }
710
711    fn serialize_tuple_variant(
712        self,
713        _name: &'static str,
714        _variant_index: u32,
715        _variant: &'static str,
716        _len: usize,
717    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
718        Err(Error::WontImplement("tuple variant"))
719    }
720
721    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
722        Err(Error::WontImplement("map"))
723    }
724
725    fn serialize_struct(
726        self,
727        _name: &'static str,
728        _len: usize,
729    ) -> Result<Self::SerializeStruct, Self::Error> {
730        Ok(self)
731    }
732
733    fn serialize_struct_variant(
734        self,
735        _name: &'static str,
736        _variant_index: u32,
737        _variant: &'static str,
738        _len: usize,
739    ) -> Result<Self::SerializeStructVariant, Self::Error> {
740        Err(Error::WontImplement("struct variant"))
741    }
742}