Skip to main content

substrate_data/schema/plain/
dynamic.rs

1//! The schemas of a value of any shape, `bool` and `()`.
2
3use crate::binary::{Decoder, Writer};
4use crate::frame::{Packer, Unpacker};
5use crate::model::borrowed::ValueRef;
6use crate::model::value::Value;
7use crate::schema::{Expected, Reader, Schema, Shape};
8
9impl Schema for Value {
10    fn shape() -> Shape {
11        Shape::Any
12    }
13    fn read(v: &ValueRef<'_>, _: &mut Reader<'_>) -> Option<Self> {
14        Some(v.to_value())
15    }
16    fn write(&self) -> Value {
17        self.clone()
18    }
19    fn write_text(&self, w: &mut crate::text::Writer) {
20        w.value(self);
21    }
22}
23
24impl Schema for bool {
25    fn shape() -> Shape {
26        Shape::Bool
27    }
28    fn read(v: &ValueRef<'_>, cx: &mut Reader<'_>) -> Option<Self> {
29        v.as_bool().or_else(|| {
30            cx.expected(Expected::Bool, v);
31            None
32        })
33    }
34    fn write(&self) -> Value {
35        Value::Bool(*self)
36    }
37    fn encode(&self, w: &mut Writer) {
38        w.bool(*self);
39    }
40    fn write_text(&self, w: &mut crate::text::Writer) {
41        w.bool(*self);
42    }
43    fn decode(d: &mut Decoder<'_>) -> Option<Self> {
44        d.bool()
45    }
46    fn pack(&self, p: &mut Packer) {
47        p.put_bool(*self);
48    }
49    fn unpack(u: &mut Unpacker<'_>) -> Option<Self> {
50        u.bool()
51    }
52}
53
54/// A number as carried (an `i64`) back to `T`: out of `T`'s range, none.
55/// Nothing: what a call answers when it only says it's done.
56impl Schema for () {
57    fn shape() -> Shape {
58        Shape::Any
59    }
60    fn read(_v: &ValueRef<'_>, _cx: &mut Reader<'_>) -> Option<Self> {
61        Some(())
62    }
63    fn write(&self) -> Value {
64        Value::Null
65    }
66    fn write_text(&self, w: &mut crate::text::Writer) {
67        w.null();
68    }
69}