[−][src]Trait cini::Ini
Parse an ini str into a struct.
Example
use cini::{Callback, CallbackKind, Ini}; #[derive(Default)] struct Config { foo: i32, bar: i32, cake: bool, } impl Ini for Config { type Err = String; fn callback(&mut self, cb: Callback) -> Result<(), Self::Err> { match cb.kind { CallbackKind::Section(section) => Err("No sections allowed".to_string()), CallbackKind::Directive(section, key, value) => { match key { "foo" => self.foo = value.unwrap().parse().unwrap(), "bar" => self.bar = value.unwrap().parse().unwrap(), "cake" => self.cake = true, _ => return Err(format!("Unknown key: {}", key)), } Ok(()) } } } } fn main() { let ini = " foo = 5 bar = 44 cake "; let mut config = Config::default(); config.parse_str(ini).unwrap(); assert_eq!(config.foo, 5); assert_eq!(config.bar, 44); assert_eq!(config.cake, true); }
Associated Types
type Err
The associated error which can be returned from parsing.
Required methods
fn callback(&mut self, cb: Callback) -> Result<(), Self::Err>
The callback function that is called for every line parsed.
Provided methods
fn parse_str(&mut self, ini: &str) -> Result<(), Self::Err>
Parses an ini str into a struct.
This function takes the struct via &mut self
. This means
many different ini files could be parsed by calling this
method repeatidly.
fn parse(&mut self, filename: Option<&str>, ini: &str) -> Result<(), Self::Err>
Parses an ini str into a struct. Optionally a filename can be supplied, this is passed to the callback so that error messages can contain the filename.
Note this method still reads from a str. You must write your own code to open a file and pass it to this method
This function takes the struct via &mut self
. This means
many different ini files could be parsed by calling this
method repeatidly.