Struct valid_toml::TomlDef
[−]
[src]
pub struct TomlDef { /* fields omitted */ }
Defines a group in the TOML file. Also the starting point for defining the contents of the file, since the file is basically an unnamed group.
Examples
use valid_toml::{TomlDef, ItemDuration, ItemStr, ItemUsize}; let file = r#" password = "Abc" count = 15 [child] name = "test" time = "30s" "#; let mut def = TomlDef::new() .add(ItemStr::with_name("password").min(3).max(12)) .add(ItemUsize::with_name("count").optional()) .add(TomlDef::with_name("child") .add(ItemStr::with_name("name")) .add(ItemDuration::with_name("time").min("0s").max("1m"))); let file = def.parse_toml(file).unwrap(); let password = file.get_str("password"); let count = file.get_usize("count"); let name = file.get_str("child.name"); let time = file.get_duration("child.time"); assert_eq!(password, "Abc"); assert_eq!(count, 15); assert_eq!(name, "test"); assert_eq!(time, 30000);
Methods
impl TomlDef
[src]
fn new() -> TomlDef
Create a new TOML definition.
fn with_name<T: AsRef<str>>(name: T) -> TomlDef
Create a child group inside an element.
fn optional(self) -> Self
Marks the group as optional.
fn add<T: ItemDef>(self, item: T) -> Self
Adds an item or group to the definition. This method is functionaly equivalent to the add_ref() method, except it takes self and then returns it. This method would be used to define a TOML file using the following notation:
Examples
use valid_toml::{TomlDef, ItemStr, ItemUsize}; let file = r#" password = "Abc" count = 15 "#; let mut def = TomlDef::new() .add(ItemStr::with_name("password").min(3).max(12)) .add(ItemUsize::with_name("count").optional()); let file = def.parse_toml(file).unwrap(); let password = file.get_str("password"); let count = file.get_usize("count"); assert_eq!(password, "Abc"); assert_eq!(count, 15);
fn ref_add<T: ItemDef>(&mut self, item: T)
Adds an item or group to the definition. This method is functionaly equivalent to the add() method, except it takes a reference to self. This method would be used to define a TOML file using the following notation:
Examples
use valid_toml::{TomlDef, ItemStr, ItemUsize}; let file = r#" password = "Abc" count = 15 "#; let mut def = TomlDef::new(); def.ref_add(ItemStr::with_name("password").min(3).max(12)); def.ref_add(ItemUsize::with_name("count").optional()); let file = def.parse_toml(file).unwrap(); let password = file.get_str("password"); let count = file.get_usize("count"); assert_eq!(password, "Abc"); assert_eq!(count, 15);
fn parse_toml<T: AsRef<str>>(&mut self, input: T) -> Result<TomlData, Issues>
Load the TOML file from a string already loaded into memory.
fn load_toml<P: AsRef<Path>>(&mut self, file: P) -> Result<TomlData, Issues>
Load the TOML file.