| 1 |
use std::io::stdout; |
| 2 |
|
| 3 |
use ini::Ini; |
| 4 |
|
| 5 |
const CONF_FILE_NAME: &str = "test.ini"; |
| 6 |
|
| 7 |
fn main() { |
| 8 |
let mut conf = Ini::new(); |
| 9 |
conf.with_section(None::<String>).set("encoding", "utf-8"); |
| 10 |
conf.with_section(Some("User")) |
| 11 |
.set("name", "Raspberry树莓") |
| 12 |
.set("value", "Pi"); |
| 13 |
conf.with_section(Some("Library")) |
| 14 |
.set("name", "Sun Yat-sen U") |
| 15 |
.set("location", "Guangzhou=world\x0ahahaha"); |
| 16 |
|
| 17 |
conf.section_mut(Some("Library")).unwrap().insert("seats", "42"); |
| 18 |
|
| 19 |
println!("---------------------------------------"); |
| 20 |
println!("Writing to file {:?}\n", CONF_FILE_NAME); |
| 21 |
conf.write_to(&mut stdout()).unwrap(); |
| 22 |
|
| 23 |
conf.write_to_file(CONF_FILE_NAME).unwrap(); |
| 24 |
|
| 25 |
println!("----------------------------------------"); |
| 26 |
println!("Reading from file {:?}", CONF_FILE_NAME); |
| 27 |
let i = Ini::load_from_file(CONF_FILE_NAME).unwrap(); |
| 28 |
|
| 29 |
println!("Iterating"); |
| 30 |
let general_section_name = "__General__"; |
| 31 |
for (sec, prop) in i.iter() { |
| 32 |
let section_name = sec.as_ref().unwrap_or(&general_section_name); |
| 33 |
println!("-- Section: {:?} begins", section_name); |
| 34 |
for (k, v) in prop.iter() { |
| 35 |
println!("{}: {:?}", k, v); |
| 36 |
} |
| 37 |
} |
| 38 |
println!(); |
| 39 |
|
| 40 |
let section = i.section(Some("User")).unwrap(); |
| 41 |
println!("name={}", section.get("name").unwrap()); |
| 42 |
println!("conf[User][name]={}", &i["User"]["name"]); |
| 43 |
println!("General Section: {:?}", i.general_section()); |
| 44 |
} |