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::optionaldeclares a key without a default. A valid value is kept, an invalid one is removed, and a missing one stays missing.Schema::openkeeps 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
impl Schema
Sourcepub fn builtin() -> Self
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.
Sourcepub fn flag(self, key: &str, default: bool) -> Self
pub fn flag(self, key: &str, default: bool) -> Self
A true/false key. Declaring a key again replaces its earlier rule.
Sourcepub fn choice(
self,
key: &str,
choices: impl IntoIterator<Item = impl Into<String>>,
default: &str,
) -> Self
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.
Sourcepub fn check<T: Setting + 'static>(
self,
key: &str,
default: T,
valid: impl Fn(&T) -> bool + Send + Sync + 'static,
) -> Self
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.
Sourcepub fn optional(self, key: &str, kind: SettingKind) -> Self
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(), "");Sourcepub fn open(self, prefix: &str) -> Self
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§
impl StructuralPartialEq for Schema
Auto Trait Implementations§
impl !RefUnwindSafe for Schema
impl !UnwindSafe for Schema
impl Freeze for Schema
impl Send for Schema
impl Sync for Schema
impl Unpin for Schema
impl UnsafeUnpin for Schema
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<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