Skip to main content

Schema

Struct Schema 

Source
pub struct Schema { /* private fields */ }
Expand description

The settings an application knows: every key with what it may hold and its default.

Give it to Settings::schema to check a loaded file, and turn on Settings::self_heal to repair it: unknown keys are removed and invalid values are replaced by their default. Values known only while running, such as the installed themes and languages, are passed in when the schema is built.

Two capabilities are opt-in, each on its own:

  • Schema::optional declares a key without a default. A valid value is kept, an invalid one is removed, and a missing one stays missing.
  • Schema::open keeps every key under a table as it is, for keys the application does not own, such as plugins’ settings.

A key missing from the file is never written into it: reading it gives None and the application uses its default.

use qframe::storage::{Schema, Settings};

let schema = Schema::builtin()
    .choice(Settings::LANGUAGE, ["en", "tr"], "en")
    .choice("deploy.region", ["eu-west", "us-east"], "eu-west")
    .check("editor.tab-width", 4u16, |width| (1..=16).contains(width));
let settings = Settings::parse_str("settings.toml", "language = \"sjds\"\ncolor = \"red\"\n")
    .schema(schema)
    .self_heal(true);
assert_eq!(settings.language().as_deref(), Some("en"));
assert!(settings.value("color").is_none());

Implementations§

Source§

impl Schema

Source

pub fn builtin() -> Self

The keys the framework itself reads: theme and language (any string; monochrome and en), icons (auto, nerd, unicode, ascii; auto), reduced-motion (false), pillar (thick, thin; thick) and slide (true). Declare theme and language again with Schema::choice to accept only what is installed.

Source

pub fn flag(self, key: &str, default: bool) -> Self

A true/false key. Declaring a key again replaces its earlier rule.

Source

pub fn text(self, key: &str, default: impl Into<String>) -> Self

A key holding any text.

Source

pub fn choice( self, key: &str, choices: impl IntoIterator<Item = impl Into<String>>, default: &str, ) -> Self

A key holding one text of choices, e.g. the installed theme ids.

Source

pub fn check<T: Setting + 'static>( self, key: &str, default: T, valid: impl Fn(&T) -> bool + Send + Sync + 'static, ) -> Self

A key whose value must read as T and pass valid, e.g. a number in a range or a name without spaces.

Source

pub fn optional(self, key: &str, kind: SettingKind) -> Self

A key without a default, holding kind, e.g. a note an application stores only once the user writes one. With self-healing on, a valid value is kept, an invalid one is removed (there is nothing to replace it with) and a missing one is not added; reading a missing key gives None.

use qframe::storage::{Schema, SettingKind, Settings};

let schema = Schema::default().optional("deploy.note", SettingKind::text());
let healed = Settings::parse_str("settings.toml", "[deploy]\nnote = 42\n").schema(schema).self_heal(true);
assert_eq!(healed.get::<String>("deploy.note"), None);
assert_eq!(healed.to_toml(), "");
Source

pub fn open(self, prefix: &str) -> Self

Keeps every key under the dotted table prefix as it is: open("plugins") keeps [plugins] and every table below it, unchecked and never removed, for settings the application does not own. Keys declared under the prefix are still checked by their rule. The prefix names a table, so a plain plugins = … key is not under it; "" opens nothing.

use qframe::storage::{Schema, Settings};

let schema = Schema::default().open("plugins").flag("plugins.enabled", true);
let text = "[plugins]\nenabled = \"yes\"\n\n[plugins.git]\nsign = true\n";
let healed = Settings::parse_str("settings.toml", text).schema(schema).self_heal(true);
assert_eq!(healed.to_toml(), "[plugins]\nenabled = true\n\n[plugins.git]\nsign = true\n");

Trait Implementations§

Source§

impl Clone for Schema

Source§

fn clone(&self) -> Schema

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 Schema

Source§

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

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

impl Default for Schema

Source§

fn default() -> Schema

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

impl PartialEq for Schema

Source§

fn eq(&self, other: &Schema) -> 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 Schema

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