preserves/value/
domain.rs

1//! Traits for working with Preserves [embedded
2//! values](https://preserves.dev/preserves.html#embeddeds).
3
4use std::io;
5
6use super::packed;
7use super::BinarySource;
8use super::BytesBinarySource;
9use super::Embeddable;
10use super::IOValue;
11use super::NestedValue;
12use super::Reader;
13use super::Writer;
14
15/// Implementations parse [IOValue]s to their own particular [Embeddable] values of type `D`.
16pub trait DomainParse<D: Embeddable> {
17    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D>;
18}
19
20/// Implementations read and parse from `src` to produce [Embeddable] values of type `D`.
21pub trait DomainDecode<D: Embeddable> {
22    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
23        &mut self,
24        src: &'src mut S,
25        read_annotations: bool,
26    ) -> io::Result<D>;
27}
28
29/// Implementations unparse and write `D`s to `w`, a [writer][crate::value::writer::Writer].
30pub trait DomainEncode<D: Embeddable> {
31    fn encode_embedded<W: Writer>(&mut self, w: &mut W, d: &D) -> io::Result<()>;
32}
33
34impl<'a, D: Embeddable, T: DomainParse<D>> DomainParse<D> for &'a mut T {
35    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D> {
36        (**self).parse_embedded(v)
37    }
38}
39
40impl<'a, D: Embeddable, T: DomainDecode<D>> DomainDecode<D> for &'a mut T {
41    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
42        &mut self,
43        src: &'src mut S,
44        read_annotations: bool,
45    ) -> io::Result<D> {
46        (**self).decode_embedded(src, read_annotations)
47    }
48}
49
50/// Convenience codec: use this as embedded codec for encoding (only) when embedded values
51/// should be serialized as Preserves `String`s holding their Rust [std::fmt::Debug]
52/// representation.
53pub struct DebugDomainEncode;
54
55impl<D: Embeddable> DomainEncode<D> for DebugDomainEncode {
56    fn encode_embedded<W: Writer>(&mut self, w: &mut W, d: &D) -> io::Result<()> {
57        d.debug_encode(w)
58    }
59}
60
61/// Convenience codec: use this as embedded codec for decoding (only) when embedded values are
62/// expected to conform to the syntax implicit in their [std::str::FromStr] implementation.
63pub struct FromStrDomainParse;
64
65impl<Err: Into<io::Error>, D: Embeddable + std::str::FromStr<Err = Err>> DomainParse<D>
66    for FromStrDomainParse
67{
68    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D> {
69        Ok(D::from_str(v.value().to_string()?).map_err(|e| e.into())?)
70    }
71}
72
73/// Use this as embedded codec when embedded data are already [IOValue]s that can be directly
74/// serialized and deserialized without further transformation.
75pub struct IOValueDomainCodec;
76
77impl DomainDecode<IOValue> for IOValueDomainCodec {
78    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
79        &mut self,
80        src: &'src mut S,
81        read_annotations: bool,
82    ) -> io::Result<IOValue> {
83        packed::PackedReader::new(src, IOValueDomainCodec).demand_next(read_annotations)
84    }
85}
86
87impl DomainEncode<IOValue> for IOValueDomainCodec {
88    fn encode_embedded<W: Writer>(&mut self, w: &mut W, d: &IOValue) -> io::Result<()> {
89        w.write(self, d)
90    }
91}
92
93/// Use this as embedded codec to forbid use of embedded values; an [io::Error] is signalled.
94pub struct NoEmbeddedDomainCodec;
95
96impl<D: Embeddable> DomainDecode<D> for NoEmbeddedDomainCodec {
97    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
98        &mut self,
99        _src: &'src mut S,
100        _read_annotations: bool,
101    ) -> io::Result<D> {
102        Err(io::Error::new(
103            io::ErrorKind::Unsupported,
104            "Embedded values not supported here",
105        ))
106    }
107}
108
109impl<D: Embeddable> DomainEncode<D> for NoEmbeddedDomainCodec {
110    fn encode_embedded<W: Writer>(&mut self, _w: &mut W, _d: &D) -> io::Result<()> {
111        Err(io::Error::new(
112            io::ErrorKind::Unsupported,
113            "Embedded values not supported here",
114        ))
115    }
116}
117
118/// If some `C` implements [DomainDecode] but not [DomainParse], or vice versa, use `ViaCodec`
119/// to promote the one to the other. Construct instances with [ViaCodec::new].
120pub struct ViaCodec<C>(C);
121
122impl<C> ViaCodec<C> {
123    /// Constructs a `ViaCodec` wrapper around an underlying codec of type `C`.
124    pub fn new(c: C) -> Self {
125        ViaCodec(c)
126    }
127}
128
129impl<D: Embeddable, C: DomainDecode<D>> DomainParse<D> for ViaCodec<C> {
130    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D> {
131        let bs = packed::PackedWriter::encode_iovalue(v)?;
132        self.0
133            .decode_embedded(&mut BytesBinarySource::new(&bs), true)
134    }
135}
136
137impl<D: Embeddable, C: DomainParse<D>> DomainDecode<D> for ViaCodec<C> {
138    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
139        &mut self,
140        src: &'src mut S,
141        read_annotations: bool,
142    ) -> io::Result<D> {
143        let v = src
144            .packed(IOValueDomainCodec)
145            .demand_next(read_annotations)?;
146        self.0.parse_embedded(&v)
147    }
148}