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
//! [`Schema`]: a format, a type that reads, writes, encodes, packs and describes itself.
use super::{Reader, Shape, read_ref_value};
use crate::binary::{Decoder, Writer};
use crate::frame::{Packer, Unpacker};
use crate::model::borrowed::ValueRef;
use crate::model::value::Value;
/// A format: a type that reads, writes, encodes, packs and describes itself.
pub trait Schema: Sized {
/// What the format looks like.
fn shape() -> Shape;
/// Reads `v`. Problems go to `cx` at its current path; `None` when the
/// value couldn't be read (the problem is in `cx`).
fn read(v: &ValueRef<'_>, cx: &mut Reader<'_>) -> Option<Self>;
/// The value, as the format writes it.
fn write(&self) -> Value;
/// The value of a field that isn't there — `None`: the field is required.
#[must_use]
fn absent() -> Option<Self> {
None
}
/// Whether a struct leaves this field out when writing it.
fn skip(&self) -> bool {
false
}
/// Writes itself in the binary form, straight — [`Schema::write`]'s
/// value encoded, unless the type knows better (derived ones do).
fn encode(&self, w: &mut Writer) {
w.value(&self.write());
}
/// Writes itself as text, straight ([`crate::text::Writer`]) —
/// [`Schema::write`]'s value as text, unless the type knows better
/// (derived ones do): no value tree is built on the way.
fn write_text(&self, w: &mut crate::text::Writer) {
w.value(&self.write());
}
/// Reads itself from the binary form, straight; `None` when it isn't
/// what [`Schema::encode`] writes — then [`read_bytes`](crate::schema::read_bytes) reads the slow
/// way, which says what's wrong. Through a value unless the type knows
/// better (derived ones do).
fn decode(d: &mut Decoder<'_>) -> Option<Self> {
let arena = bumpalo::Bump::new();
let v = d.value_ref(&arena).ok()?;
read_ref_value(&v).ok()
}
/// Packs itself into a frame ([`crate::frame`]): its binary form as
/// bytes, unless the type packs itself (derived ones and plain types do).
fn pack(&self, p: &mut Packer) {
p.put_tagged(self);
}
/// Reads itself out of a frame; `None` if the bytes aren't what
/// [`Schema::pack`] writes.
fn unpack(u: &mut Unpacker<'_>) -> Option<Self> {
u.tagged()
}
/// A list of these packed: a count, then each one (floats raw).
fn pack_list(items: &[Self], p: &mut Packer) {
let at = p.begin_list(items.len());
for item in items {
item.pack(p);
}
p.end(at);
}
/// A list of these out of a frame.
fn unpack_list(u: &mut Unpacker<'_>) -> Option<Vec<Self>> {
let (n, end) = u.list()?;
let mut out = Vec::with_capacity(n);
for _ in 0..n {
out.push(Self::unpack(u)?);
}
u.skip_to(end)?;
Some(out)
}
/// A list of these written straight (numbers pack theirs).
fn encode_list(items: &[Self], w: &mut Writer) {
w.list(items.len());
for item in items {
item.encode(w);
}
}
/// A list of these read straight.
fn decode_list(d: &mut Decoder<'_>) -> Option<Vec<Self>> {
let n = d.list()?;
let mut out = Vec::with_capacity(n);
for _ in 0..n {
out.push(Self::decode(d)?);
}
d.leave();
Some(out)
}
}