Skip to main content

Shape

Struct Shape 

Source
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

Source

pub fn new() -> Self

A table that holds nothing yet. Every key it may hold is declared on it.

Source

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.

Source

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.

Source

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.

Source

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§

Source§

impl Clone for Shape

Source§

fn clone(&self) -> Shape

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 Shape

Source§

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

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

impl Default for Shape

Source§

fn default() -> Shape

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

impl Eq for Shape

Source§

impl PartialEq for Shape

Source§

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

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

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

Inequality operator !=. Read more
Source§

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> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.