pub struct Shape { /* private fields */ }Expand description
The shape of one table of a document: which keys it holds, which tables sit inside it and which of its keys carry an array of tables.
A Document is read against the shape of its root table. Keys are single
names, never dotted paths: nesting is declared with Shape::table, which reads both
[mounts] with project under it and the same key written as mounts.project.
use qframe::document::{Document, Shape, ValueKind};
let profile = Shape::new().required("name", ValueKind::text()).optional("added", ValueKind::text());
let shape = Shape::new()
.required("id", ValueKind::text())
.optional("created", ValueKind::text())
.table("mounts", Shape::new().optional("assets", ValueKind::choice(["rw", "ro"])))
.entries("profile", profile);
let text = "id = \"api\"\n\n[mounts]\nassets = \"ro\"\n\n[[profile]]\nname = \"review\"\n";
let document = Document::parse("project.qcode", text, &shape);
assert!(document.is_clean());
assert_eq!(document.root().text("id"), Some("api"));
assert_eq!(document.root().table("mounts").and_then(|mounts| mounts.text("assets")), Some("ro"));
assert_eq!(document.root().entries("profile").len(), 1);Implementations§
Source§impl Shape
impl Shape
Sourcepub fn required(self, key: &str, kind: ValueKind) -> Self
pub fn required(self, key: &str, kind: ValueKind) -> Self
A key the document must hold: a document without it, or with a value of another type, is reported as an error. Declaring a name again replaces what it declared before.
Sourcepub fn optional(self, key: &str, kind: ValueKind) -> Self
pub fn optional(self, key: &str, kind: ValueKind) -> Self
A key the document may hold: it is read when it is there and of the declared type, a value of another type is a warning, and a missing key is not a problem at all.
Sourcepub fn table(self, key: &str, shape: Shape) -> Self
pub fn table(self, key: &str, shape: Shape) -> Self
A table inside this one, [key] with shape below it.
The table itself is optional: a document without it is not a problem, and the keys of a missing table read as missing. Its required keys are required once the table is there.
Sourcepub fn entries(self, key: &str, shape: Shape) -> Self
pub fn entries(self, key: &str, shape: Shape) -> Self
An array of tables, [[key]] repeated, each entry shaped by shape.
The array itself is optional: a document that lists no entry simply has none. Each entry is checked on its own, and an entry that is missing a required key is reported where it starts, beside the entries that were read.
Trait Implementations§
impl Eq for Shape
impl StructuralPartialEq for Shape
Auto Trait Implementations§
impl Freeze for Shape
impl RefUnwindSafe for Shape
impl Send for Shape
impl Sync for Shape
impl Unpin for Shape
impl UnsafeUnpin for Shape
impl UnwindSafe for Shape
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more