From 858ee035533a5bcbe6e68ea7054b36083752b563 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 7 Sep 2025 23:08:56 -0400 Subject: Fix various bugs --- examples/conf.pom | 22 +++++++++++++++++++++- examples/simple.rs | 14 +++++++++++++- src/lib.rs | 41 ++++++----------------------------------- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/examples/conf.pom b/examples/conf.pom index 90483f6..fde2274 100644 --- a/examples/conf.pom +++ b/examples/conf.pom @@ -1 +1,21 @@ -fav-colour = `green` + +indent-using-spaces = yes +show-line-numbers = yes +tab-size = 4 +font-size = 18 + +[file-extensions] +C = .c +Cpp = .cpp, .h, .hpp + +[plug-in.edit-over-ssh] +path = ~/misc/edit-over-ssh.so +enabled = yes + +[plug-in.edit-over-ssh.settings] +favourite-host = my-web-server + +[plug-in.edit-over-ssh.settings.hosts.my-web-server] +address = example.org +port = 22 +ssh-key = ~/.ssh/id_ed25519 diff --git a/examples/simple.rs b/examples/simple.rs index 161baac..a536f44 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -3,7 +3,19 @@ use std::process::ExitCode; fn try_main() -> Result<(), Box> { let conf = Configuration::load_path("examples/conf.pom")?; - println!("{conf}"); + println!("tab width is {}", conf.get_int_or_default("tab-size", 8)?); + println!( + "indenting with {}", + if conf.get_bool_or_default("indent-using-spaces", false)? { + "spaces" + } else { + "tabs" + } + ); + println!( + "edit-over-ssh plug-in path: {:?}", + conf.get("plug-in.edit-over-ssh.path") + ); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 16329bd..8a03384 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; -use alloc::borrow::Cow; #[cfg(not(feature = "std"))] use alloc::collections::BTreeMap as Map; use alloc::sync::Arc; @@ -40,37 +39,9 @@ pub struct Configuration { impl fmt::Display for Configuration { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut lines = vec![]; - fn format_value(val: &str) -> Cow { - if val.chars().all(|c| !c.is_ascii_control()) - && !val.starts_with(['\'', '"', '`']) - && !val.starts_with(char::is_whitespace) - { - return Cow::Borrowed(val); - } - let mut quoted = String::from("\""); - for c in val.chars() { - if c == '"' { - quoted.push_str("\\\""); - } else if c == '\n' { - quoted.push_str("\\n"); - } else if c == '\r' { - quoted.push_str("\\r"); - } else if c == '\t' { - quoted.push_str("\\t"); - } else if c == '\\' { - quoted.push('\\'); - } else if c.is_ascii_control() { - quoted.push_str(&format!("\\x{:02x}", c as u32)); - } else { - quoted.push(c); - } - } - quoted.push('"'); - Cow::Owned(quoted) - } fn add_lines(lines: &mut Vec, prefix: &str, conf: &Configuration) { for (key, val) in conf.values.iter() { - lines.push(format!("{prefix}{key}: {}", format_value(&val.value))); + lines.push(format!("{prefix}{key} = {:?}", val.value)); } for (key, child) in conf.children.iter() { add_lines(lines, &format!("{prefix}{key}."), child); @@ -359,7 +330,7 @@ impl Parser { loop { let line = if first { first = false; - quoted + "ed[1..] } else { line_buf.truncate(0); if !reader.read_until_lf(&mut line_buf)? { @@ -420,7 +391,7 @@ impl Parser { self.nonfatal_errors .push(Error::InvalidValue(location(line_number))); } - unquoted.push(char::try_from(nibble1 << 8 | nibble2).unwrap()); + unquoted.push(char::try_from(nibble1 << 4 | nibble2).unwrap()); } 'u' => { let mut c = chars.next(); @@ -467,6 +438,7 @@ impl Parser { unquoted.push(c); } } + unquoted.push('\n'); } Err(Error::UnterminatedString(start_location.clone(), delimiter)) } @@ -603,13 +575,12 @@ impl Configuration { .map(|x| x.as_ref()) } fn get_val(&self, key: &str) -> Option<&Value> { - let Some(last_dot) = key.rfind('.') else { + let Some((path, last_component)) = key.rsplit_once('.') else { return self.values.get(key); }; - let (path, last_component) = key.split_at(last_dot); let mut node = self; for component in path.split('.') { - node = self.children.get(component)?; + node = node.children.get(component)?; } node.values.get(last_component) } -- cgit v1.2.3