pub struct Dictionary { /* private fields */ }Expand description
The values references expand to — the words of a vocabulary, the texts
of a language, the entries of a table — kept per kind and id, in the
order they were added. It is a Resolve: a reference of a kind it
holds is checked against its ids and, where a field takes a value
(with::Expand), expanded to the value; a
reference of a kind it doesn’t hold is left alone.
It is a Schema too, written { <kind>: { <id>: <value>, … }, … }, so
a tool loads it from text, a value or the binary form like any
document (an empty kind, { word: {} }, holds nothing and says every
@word:… is missing). Keys that are notes ($…, _…, comment) are
skipped, as in any object.
use substrate_data::schema::{Dictionary, from_text, from_value_with};
use substrate_data::{Ref, Value};
let mut dictionary: Dictionary =
from_text(r#"{ project: { alpha: { name: "Alpha" } } }"#).unwrap().value;
let alpha = Ref::parse("project", "alpha").unwrap();
assert_eq!(dictionary.get(&alpha).unwrap().get("name").unwrap().as_str(), Some("Alpha"));
// As a resolver, it expands what a field reads through `Expand`.
#[derive(substrate_data::Schema)]
struct Card {
#[data(with = "substrate_data::with::Expand<substrate_data::with::Plain>")]
project: Value,
}
let doc: Value = from_text(r#"{ project: "@project:alpha" }"#).unwrap().value;
let card = from_value_with::<Card>(&doc, &mut dictionary).unwrap().value;
assert_eq!(card.project.get("name").unwrap().as_str(), Some("Alpha"));Implementations§
Source§impl Dictionary
impl Dictionary
Sourcepub fn insert(
&mut self,
kind: KindName,
id: RefKey,
value: Value,
) -> Option<Value>
pub fn insert( &mut self, kind: KindName, id: RefKey, value: Value, ) -> Option<Value>
value as the value of id of kind, returning the one it
replaced.
Sourcepub fn declare(&mut self, kind: KindName)
pub fn declare(&mut self, kind: KindName)
Holds kind (with no values if there are none): its references are
now checked, and one to an id there isn’t is missing.
Trait Implementations§
Source§impl Clone for Dictionary
impl Clone for Dictionary
Source§impl Debug for Dictionary
impl Debug for Dictionary
Source§impl Default for Dictionary
impl Default for Dictionary
Source§impl PartialEq for Dictionary
impl PartialEq for Dictionary
Source§impl Resolve for Dictionary
impl Resolve for Dictionary
Source§fn reference(&mut self, r: &Ref, _path: &Path) -> Result<RefKey, Unresolved>
fn reference(&mut self, r: &Ref, _path: &Path) -> Result<RefKey, Unresolved>
path: the id to keep, or why it’s wrong. Read moreSource§fn expand(&mut self, r: &Ref, _path: &Path) -> Result<Option<Value>, Unresolved>
fn expand(&mut self, r: &Ref, _path: &Path) -> Result<Option<Value>, Unresolved>
path the tool replaces by a value — a word of a
dictionary, a localized text: Some(value) is read in its place,
None keeps it a reference. Asked only where a field says it takes
a value or a reference (with::Expand); a
reference met anywhere else goes through Resolve::reference. Read moreSource§impl Schema for Dictionary
impl Schema for Dictionary
Source§fn read(v: &ValueRef<'_>, cx: &mut Reader<'_>) -> Option<Self>
fn read(v: &ValueRef<'_>, cx: &mut Reader<'_>) -> Option<Self>
v. Problems go to cx at its current path; None when the
value couldn’t be read (the problem is in cx).Source§fn absent() -> Option<Self>
fn absent() -> Option<Self>
None: the field is required.Source§fn encode(&self, w: &mut Writer)
fn encode(&self, w: &mut Writer)
Schema::write’s
value encoded, unless the type knows better (derived ones do).Source§fn write_text(&self, w: &mut Writer)
fn write_text(&self, w: &mut Writer)
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.Source§fn decode(d: &mut Decoder<'_>) -> Option<Self>
fn decode(d: &mut Decoder<'_>) -> Option<Self>
None when it isn’t
what Schema::encode writes — then read_bytes reads the slow
way, which says what’s wrong. Through a value unless the type knows
better (derived ones do).Source§fn pack(&self, p: &mut Packer)
fn pack(&self, p: &mut Packer)
crate::frame): its binary form as
bytes, unless the type packs itself (derived ones and plain types do).Source§fn unpack(u: &mut Unpacker<'_>) -> Option<Self>
fn unpack(u: &mut Unpacker<'_>) -> Option<Self>
None if the bytes aren’t what
Schema::pack writes.