summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-09-10 12:11:01 -0400
committerpommicket <pommicket@gmail.com>2025-09-10 12:11:40 -0400
commit930bd7ad340dc61057cd1db1db9d1389aae569a5 (patch)
treef92de12d81885a7ee80a8fe0e025500551dc84d7 /examples
parentf1767908837d0e6a0da2aa73009ce11de6fb359d (diff)
More extensive example
Diffstat (limited to 'examples')
-rw-r--r--examples/conf.pom9
-rw-r--r--examples/read_conf.rs57
-rw-r--r--examples/simple.rs29
3 files changed, 60 insertions, 35 deletions
diff --git a/examples/conf.pom b/examples/conf.pom
index 73ee3f1..fe9fe2f 100644
--- a/examples/conf.pom
+++ b/examples/conf.pom
@@ -11,10 +11,7 @@ Cpp = .cpp, .h, .hpp
path = ~/misc/edit-over-ssh.so
enabled = yes
-[plug-in.edit-over-ssh.settings]
-favourite-host = my-web-server
+[plug-in.wrap-text]
+path = ~/misc/wrap_text_v3.5.7.so
+enabled = no
-[plug-in.edit-over-ssh.settings.hosts.my-web-server]
-address = example.org
-port = 22
-ssh-key = ~/.ssh/id_ed25519
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
+}
diff --git a/examples/simple.rs b/examples/simple.rs
deleted file mode 100644
index 72ccf1f..0000000
--- a/examples/simple.rs
+++ /dev/null
@@ -1,29 +0,0 @@
-use pom_parser::Configuration;
-use std::process::ExitCode;
-
-fn try_main() -> Result<(), Box<dyn std::error::Error>> {
- let conf = Configuration::load_path("examples/conf.pom")?;
- println!("tab width is {}", conf.get_int_or_default("tab-size", 8)?);
- println!(
- "indenting with {}",
- conf.get_or_default("indentation-type", "tabs")
- );
- println!(
- "show line numbers: {}",
- conf.get_bool_or_default("show-line-numbers", false)?
- );
- println!(
- "edit-over-ssh plug-in path: {:?}",
- conf.get("plug-in.edit-over-ssh.path")
- );
- println!("C++ extensions: {:?}", conf.get_list("file-extensions.Cpp"));
- Ok(())
-}
-
-fn main() -> ExitCode {
- if let Err(e) = try_main() {
- eprintln!("Error: {e}");
- return ExitCode::FAILURE;
- }
- ExitCode::SUCCESS
-}