pub trait Ini {
type Err;
fn callback(&mut self, cb: Callback<'_>) -> Result<(), Self::Err>;
fn parse_str(&mut self, ini: &str) -> Result<(), Self::Err> { ... }
fn parse(
&mut self,
filename: Option<&str>,
ini: &str
) -> Result<(), Self::Err> { ... }
fn parse_with_section<'a>(
&mut self,
section: Option<&'a str>,
filename: Option<&str>,
ini: &'a str
) -> Result<Option<&'a str>, Self::Err> { ... }
fn parse_line<'a>(
&mut self,
filename: Option<&str>,
line: &'a str,
line_number: usize,
section: Option<&'a str>
) -> Result<Option<&'a str>, Self::Err> { ... }
}
Expand description
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
Required methods
Provided methods
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.
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.
Like parse() but allows you to input the starting section as well as returning the section the input data leaves on.
This allows keeping track of the section for recursive parsing.