summaryrefslogtreecommitdiff
path: root/examples/read_conf.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/read_conf.rs')
-rw-r--r--examples/read_conf.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/examples/read_conf.rs b/examples/read_conf.rs
new file mode 100644
index 0000000..c2d4044
--- /dev/null
+++ b/examples/read_conf.rs
@@ -0,0 +1,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
+}