1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Traits for working with Preserves [embedded
//! values](https://preserves.dev/preserves.html#embeddeds).

use std::io;

use super::packed;
use super::BinarySource;
use super::BytesBinarySource;
use super::Embeddable;
use super::IOValue;
use super::NestedValue;
use super::Reader;
use super::Writer;

/// Implementations parse [IOValue]s to their own particular [Embeddable] values of type `D`.
pub trait DomainParse<D: Embeddable> {
    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D>;
}

/// Implementations read and parse from `src` to produce [Embeddable] values of type `D`.
pub trait DomainDecode<D: Embeddable> {
    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
        &mut self,
        src: &'src mut S,
        read_annotations: bool,
    ) -> io::Result<D>;
}

/// Implementations unparse and write `D`s to `w`, a [writer][crate::value::writer::Writer].
pub trait DomainEncode<D: Embeddable> {
    fn encode_embedded<W: Writer>(&mut self, w: &mut W, d: &D) -> io::Result<()>;
}

impl<'a, D: Embeddable, T: DomainParse<D>> DomainParse<D> for &'a mut T {
    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D> {
        (**self).parse_embedded(v)
    }
}

impl<'a, D: Embeddable, T: DomainDecode<D>> DomainDecode<D> for &'a mut T {
    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
        &mut self,
        src: &'src mut S,
        read_annotations: bool,
    ) -> io::Result<D> {
        (**self).decode_embedded(src, read_annotations)
    }
}

/// Convenience codec: use this as embedded codec for encoding (only) when embedded values
/// should be serialized as Preserves `String`s holding their Rust [std::fmt::Debug]
/// representation.
pub struct DebugDomainEncode;

impl<D: Embeddable> DomainEncode<D> for DebugDomainEncode {
    fn encode_embedded<W: Writer>(&mut self, w: &mut W, d: &D) -> io::Result<()> {
        d.debug_encode(w)
    }
}

/// Convenience codec: use this as embedded codec for decoding (only) when embedded values are
/// expected to conform to the syntax implicit in their [std::str::FromStr] implementation.
pub struct FromStrDomainParse;

impl<Err: Into<io::Error>, D: Embeddable + std::str::FromStr<Err = Err>> DomainParse<D>
    for FromStrDomainParse
{
    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D> {
        Ok(D::from_str(v.value().to_string()?).map_err(|e| e.into())?)
    }
}

/// Use this as embedded codec when embedded data are already [IOValue]s that can be directly
/// serialized and deserialized without further transformation.
pub struct IOValueDomainCodec;

impl DomainDecode<IOValue> for IOValueDomainCodec {
    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
        &mut self,
        src: &'src mut S,
        read_annotations: bool,
    ) -> io::Result<IOValue> {
        packed::PackedReader::new(src, IOValueDomainCodec).demand_next(read_annotations)
    }
}

impl DomainEncode<IOValue> for IOValueDomainCodec {
    fn encode_embedded<W: Writer>(&mut self, w: &mut W, d: &IOValue) -> io::Result<()> {
        w.write(self, d)
    }
}

/// Use this as embedded codec to forbid use of embedded values; an [io::Error] is signalled.
pub struct NoEmbeddedDomainCodec;

impl<D: Embeddable> DomainDecode<D> for NoEmbeddedDomainCodec {
    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
        &mut self,
        _src: &'src mut S,
        _read_annotations: bool,
    ) -> io::Result<D> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "Embedded values not supported here",
        ))
    }
}

impl<D: Embeddable> DomainEncode<D> for NoEmbeddedDomainCodec {
    fn encode_embedded<W: Writer>(&mut self, _w: &mut W, _d: &D) -> io::Result<()> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "Embedded values not supported here",
        ))
    }
}

/// If some `C` implements [DomainDecode] but not [DomainParse], or vice versa, use `ViaCodec`
/// to promote the one to the other. Construct instances with [ViaCodec::new].
pub struct ViaCodec<C>(C);

impl<C> ViaCodec<C> {
    /// Constructs a `ViaCodec` wrapper around an underlying codec of type `C`.
    pub fn new(c: C) -> Self {
        ViaCodec(c)
    }
}

impl<D: Embeddable, C: DomainDecode<D>> DomainParse<D> for ViaCodec<C> {
    fn parse_embedded(&mut self, v: &IOValue) -> io::Result<D> {
        let bs = packed::PackedWriter::encode_iovalue(v)?;
        self.0
            .decode_embedded(&mut BytesBinarySource::new(&bs), true)
    }
}

impl<D: Embeddable, C: DomainParse<D>> DomainDecode<D> for ViaCodec<C> {
    fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
        &mut self,
        src: &'src mut S,
        read_annotations: bool,
    ) -> io::Result<D> {
        let v = src
            .packed(IOValueDomainCodec)
            .demand_next(read_annotations)?;
        self.0.parse_embedded(&v)
    }
}