Skip to main content

verit_core/
value.rs

1//! Dynamic values for the write path. The prototype has no codegen; you build
2//! a `Value` tree against a runtime [`crate::Schema`] and encode it. A struct
3//! value lists (field id, value) pairs — omitted fields are absent (their
4//! presence bit stays 0 and readers see `None`).
5
6#[derive(Clone, Debug, PartialEq)]
7pub enum Value {
8    Bool(bool),
9    U8(u8),
10    U16(u16),
11    U32(u32),
12    U64(u64),
13    I8(i8),
14    I16(i16),
15    I32(i32),
16    I64(i64),
17    F32(f32),
18    F64(f64),
19    Str(String),
20    Bytes(Vec<u8>),
21    /// Raw enum value; enums are open, so this need not name a known variant.
22    Enum(u32),
23    List(Vec<Value>),
24    /// (field id, value) pairs. Order does not matter; duplicate ids are an
25    /// encode-time error.
26    Struct(Vec<(u16, Value)>),
27    /// (key, value) entries. Order does not matter — the encoder sorts entries
28    /// by key into canonical order; duplicate keys are an encode-time error.
29    Map(Vec<(Value, Value)>),
30    /// A `union` value: the variant tag (index into the schema's variant list)
31    /// and that variant's value.
32    Union(u32, Box<Value>),
33}
34
35impl Value {
36    pub fn str(s: &str) -> Value {
37        Value::Str(s.to_string())
38    }
39
40    pub fn kind(&self) -> &'static str {
41        match self {
42            Value::Bool(_) => "bool",
43            Value::U8(_) => "u8",
44            Value::U16(_) => "u16",
45            Value::U32(_) => "u32",
46            Value::U64(_) => "u64",
47            Value::I8(_) => "i8",
48            Value::I16(_) => "i16",
49            Value::I32(_) => "i32",
50            Value::I64(_) => "i64",
51            Value::F32(_) => "f32",
52            Value::F64(_) => "f64",
53            Value::Str(_) => "string",
54            Value::Bytes(_) => "bytes",
55            Value::Enum(_) => "enum",
56            Value::List(_) => "list",
57            Value::Struct(_) => "struct",
58            Value::Map(_) => "map",
59            Value::Union(..) => "union",
60        }
61    }
62}