diff options
author | pommicket <pommicket@gmail.com> | 2025-09-08 13:56:30 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-08 13:56:30 -0400 |
commit | 73e9bfa44c5b104bd322bd7011bebb367853d58c (patch) | |
tree | d82a82a63f71c20bb857626c9d711bfdcded75e2 | |
parent | 019e2518a943f22c8d7ffee541ba90326613aeb4 (diff) |
parse lists
-rw-r--r-- | examples/simple.rs | 11 | ||||
-rw-r--r-- | src/lib.rs | 34 |
2 files changed, 36 insertions, 9 deletions
diff --git a/examples/simple.rs b/examples/simple.rs index a536f44..72ccf1f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -6,16 +6,17 @@ fn try_main() -> Result<(), Box<dyn std::error::Error>> { 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" - } + 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(()) } @@ -263,8 +263,34 @@ fn parse_bool(location: &Location, string: &str) -> Result<bool> { } } -fn parse_list(_location: &Location, _string: &str) -> Vec<String> { - todo!() +fn parse_list(string: &str) -> Vec<String> { + let mut list = vec![]; + let mut item = String::new(); + let mut chars = string.chars(); + while let Some(c) = chars.next() { + if c == ',' { + list.push(item); + item = String::new(); + } else if c == '\\' { + if let Some(next) = chars.next() { + if next == ',' || next == '\\' { + item.push(next); + } else { + item.push('\\'); + item.push(next); + } + } else { + item.push('\\'); + break; + } + } else { + item.push(c); + } + } + if !item.is_empty() { + list.push(item); + } + list } /// Trait for reading configurations. @@ -759,8 +785,8 @@ impl Configuration { /// Commas in list entries can be escaped with `\,`. #[must_use] pub fn get_list(&self, key: &str) -> Option<Vec<String>> { - let Value { value, defined_at } = self.get_val(key)?; - Some(parse_list(defined_at, value.as_ref())) + let value = &self.get_val(key)?.value; + Some(parse_list(value.as_ref())) } /// Get value associated with `key`, and parse it as a comma-separated list, or else use `default`. /// |