1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
use pom_parser::Configuration;
use std::process::ExitCode;
fn try_main() -> Result<(), Box<dyn std::error::Error>> {
// Try editing examples/conf.pom and see how the output changes!
let conf = Configuration::load_path("examples/conf.pom")?;
// get looks up a key in a configuration file.
// it returns Some(value) if the key is set to value,
// and None otherwise.
println!(
"indenting with {}",
conf.get("indentation-type")
.ok_or("you must pick an indentation-type!")?
);
// get_int_or_default parses a key as an integer.
// It returns an Err if the key is set to something that’s not a valid integer.
println!("tab width is {}", conf.get_int_or_default("tab-size", 8)?);
// get_bool_or_default parses a key as a boolean.
// no, off, false all count as false, and yes, on, true count as true.
println!(
"show line numbers: {}",
conf.get_bool_or_default("show-line-numbers", false)?
);
// lists are comma-separated. they can use \, to escape literal commas.
println!("C++ extensions: {:?}", conf.get_list("file-extensions.Cpp"));
println!("===plug-ins===");
// extract out a section of a configuration
let plug_ins = conf.section("plug-in");
for plug_in in plug_ins.keys() {
// extract out just this plug-in's section
let plug_in_cfg = plug_ins.section(plug_in);
println!(
"{plug_in} is {}",
if plug_in_cfg.get_bool_or_default("enabled", true)? {
"enabled"
} else {
"disabled"
}
);
println!(
"{plug_in} plug-in path: {:?}",
plug_in_cfg
.get("path")
.ok_or_else(|| format!("no path set for plug-in {plug_in}!"))?
);
}
Ok(())
}
fn main() -> ExitCode {
if let Err(e) = try_main() {
eprintln!("Error: {e}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}
|