From 73e9bfa44c5b104bd322bd7011bebb367853d58c Mon Sep 17 00:00:00 2001 From: pommicket Date: Mon, 8 Sep 2025 13:56:30 -0400 Subject: parse lists --- src/lib.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 809453c..c2d7771 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,8 +263,34 @@ fn parse_bool(location: &Location, string: &str) -> Result { } } -fn parse_list(_location: &Location, _string: &str) -> Vec { - todo!() +fn parse_list(string: &str) -> Vec { + 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> { - 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`. /// -- cgit v1.2.3