preserves/value/
domain.rs1use 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
15pub trait DomainParse<D: Embeddable> {
17 fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D>;
18}
19
20pub 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
29pub 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
50pub 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
61pub 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
73pub 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
93pub 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
118pub struct ViaCodec<C>(C);
121
122impl<C> ViaCodec<C> {
123 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}