read_conf/
read_conf.rs

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