Skip to main content

Module with

Module with 

Source
Expand description

Field codecs: #[data(with = "…")] reads and writes a field through a type other than its own — like serde’s with. The field keeps the plain type a program uses (String), the format says what it holds (a reference to a file), checked and resolved by the Reader’s Resolve.

use substrate_data::with::{Opt, RefTo};

substrate_data::kind!(File = "file", User = "user");

#[derive(substrate_data::Schema)]
struct Attachment {
    /// `@file:…`, kept as the plain id.
    #[data(with = "RefTo<File>")]
    file: String,
    #[data(with = "Opt<RefTo<User>>")]
    owner: Option<String>,
}

let a: Attachment =
    substrate_data::from_text(r#"{ file: "@file:images/logo.png" }"#).unwrap().value;
assert_eq!(a.file, "images/logo.png");
assert_eq!(a.owner, None);

Structs§

List
A list, each item through W.
Opt
An optional field through W.
Plain
The field’s own schema — with spelled out (List<Plain> for a Vec<T: Schema>).
Raw
Bytes as they are (Vec<u8>): a payload a format doesn’t look into.
RefTo
A reference of kind(s) K, kept as the resolved id.
Spelled
Text as it’s spelled — markup’s attribute, which may spell a reference (@file:images/logo.png): a string kept as written, a typed value as its spelling. Nothing is checked or resolved here.
Values
An object of any keys, each value through W.

Traits§

With
How a field of type F is read and written.