Skip to main content

Dictionary

Struct Dictionary 

Source
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

Source

pub fn new() -> Dictionary

A dictionary holding nothing.

Source

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.

Source

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.

Source

pub fn holds(&self, kind: KindName) -> bool

Whether the dictionary holds kind.

Source

pub fn get(&self, r: &Ref) -> Option<&Value>

The value r names.

Source

pub fn kinds(&self) -> impl Iterator<Item = KindName>

The kinds held, by name.

Source

pub fn entries(&self, kind: KindName) -> impl Iterator<Item = (&RefKey, &Value)>

kind’s values in the order they were added (none if it isn’t held).

Source

pub fn len(&self) -> usize

How many values it holds, all kinds together.

Source

pub fn is_empty(&self) -> bool

Whether it holds no value.

Trait Implementations§

Source§

impl Clone for Dictionary

Source§

fn clone(&self) -> Dictionary

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dictionary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Dictionary

Source§

fn default() -> Dictionary

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Dictionary

Source§

fn eq(&self, other: &Dictionary) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Resolve for Dictionary

Source§

fn reference(&mut self, r: &Ref, _path: &Path) -> Result<RefKey, Unresolved>

A reference at path: the id to keep, or why it’s wrong. Read more
Source§

fn expand(&mut self, r: &Ref, _path: &Path) -> Result<Option<Value>, Unresolved>

A reference at 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 more
Source§

impl Schema for Dictionary

Source§

fn shape() -> Shape

What the format looks like.
Source§

fn read(v: &ValueRef<'_>, cx: &mut Reader<'_>) -> Option<Dictionary>

Reads v. Problems go to cx at its current path; None when the value couldn’t be read (the problem is in cx).
Source§

fn write(&self) -> Value

The value, as the format writes it.
Source§

fn absent() -> Option<Self>

The value of a field that isn’t there — None: the field is required.
Source§

fn skip(&self) -> bool

Whether a struct leaves this field out when writing it.
Source§

fn encode(&self, w: &mut Writer)

Writes itself in the binary form, straight — Schema::write’s value encoded, unless the type knows better (derived ones do).
Source§

fn write_text(&self, w: &mut Writer)

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.
Source§

fn decode(d: &mut Decoder<'_>) -> Option<Self>

Reads itself from the binary form, straight; 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)

Packs itself into a frame (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>

Reads itself out of a frame; None if the bytes aren’t what Schema::pack writes.
Source§

fn pack_list(items: &[Self], p: &mut Packer)

A list of these packed: a count, then each one (floats raw).
Source§

fn unpack_list(u: &mut Unpacker<'_>) -> Option<Vec<Self>>

A list of these out of a frame.
Source§

fn encode_list(items: &[Self], w: &mut Writer)

A list of these written straight (numbers pack theirs).
Source§

fn decode_list(d: &mut Decoder<'_>) -> Option<Vec<Self>>

A list of these read straight.
Source§

impl StructuralPartialEq for Dictionary

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.