toasty_core/stmt/value_object.rs
1use super::Value;
2
3/// An ordered sequence of named [`Value`]s representing a document.
4///
5/// `ValueObject` is the named counterpart to [`ValueRecord`](super::ValueRecord):
6/// where a record is positional, an object carries a key for each field. It is
7/// the wire form of a `#[document]` value — typed by the structural
8/// [`Type::Object`](super::Type::Object) — and the only form in which a
9/// document crosses the driver boundary, in either direction. The query
10/// engine builds a `ValueObject` from a positional record (using the field
11/// names from the embedded model schema) just before handing a
12/// document-stored value to a driver, and raises a driver-decoded object back
13/// to the positional record on the way in. Drivers convert a `ValueObject`
14/// structurally — to a JSON object, a BSON sub-document, a DynamoDB map —
15/// without needing the schema; the embedded model's identity never reaches
16/// them.
17///
18/// Entries are kept in insertion order. Keys are not deduplicated; writers
19/// always build objects from a schema, so keys are unique by construction.
20#[derive(Debug, Default, Clone, PartialEq)]
21pub struct ValueObject {
22 /// The named field values, in insertion order.
23 pub entries: Vec<(String, Value)>,
24}
25
26impl ValueObject {
27 /// Creates a `ValueObject` from a vector of `(key, value)` pairs.
28 pub fn from_vec(entries: Vec<(String, Value)>) -> Self {
29 Self { entries }
30 }
31
32 /// Iterates over the `(key, value)` entries in insertion order.
33 pub fn iter(&self) -> std::slice::Iter<'_, (String, Value)> {
34 self.entries.iter()
35 }
36}
37
38impl From<ValueObject> for Value {
39 fn from(value: ValueObject) -> Self {
40 Self::Object(value)
41 }
42}