diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 24 |
1 files changed, 17 insertions, 7 deletions
@@ -5,7 +5,6 @@ #![warn(clippy::redundant_closure_for_method_calls)] extern crate alloc; -use alloc::borrow::ToOwned; use alloc::boxed::Box; use alloc::string::String; use alloc::sync::Arc; @@ -698,7 +697,10 @@ impl Configuration { let start_idx = self.subkey_start_idx(key); let end_idx = self.subkey_end_idx(key); Configuration { - items: self.items[start_idx..end_idx].to_owned(), + items: self.items[start_idx..end_idx] + .iter() + .map(|(k, v)| (k[key.len() + 1..].into(), v.clone())) + .collect(), } } @@ -855,17 +857,25 @@ impl Configuration { /// Get value associated with `key`, and parse it as a comma-separated list, or else use `default`. /// + /// If you want `default = []`, use [`Self::get_list_or_empty`] instead + /// (this method will be cumbersome to use since Rust can't infer the type of `[]`). + /// /// Commas in list entries can be escaped with `\,`. /// - /// `default` can be anything that can be converted into an iterator of strings, - /// e.g. `Vec<&str>`, `Vec<String>`, `&[&str]`, etc. + /// `default` can be any iterable-of-strings + /// (e.g. `&[&str]`, `Vec<String>`, etc.). pub fn get_list_or_default<L>(&self, key: &str, default: L) -> Vec<String> where L: IntoIterator, - L::Item: AsRef<str>, + String: From<L::Item>, { self.get_list(key) - .unwrap_or_else(|| default.into_iter().map(|s| s.as_ref().to_owned()).collect()) + .unwrap_or_else(|| default.into_iter().map(String::from).collect()) + } + /// Get value associated with `key`, and parse it as a comma-separated list, or else use `[]`. + pub fn get_list_or_empty(&self, key: &str) -> Vec<String> { + let empty: [&'static str; 0] = []; + self.get_list_or_default(key, empty) } /// Merge `conf` into `self`, preferring values in `conf`. @@ -894,7 +904,7 @@ impl Configuration { } } -/// Opaque type returned by [`<&Configuration>::into_iter`]. +/// Opaque type returned by [`Configuration::iter`]. #[derive(Clone, Debug)] pub struct ConfigurationIter<'a>(core::slice::Iter<'a, (Box<str>, Value)>); |