From d090c23d90285cb96e7d0dcba5be4a65b385024c Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Tue, 23 May 2023 23:08:16 +0200 Subject: [PATCH] fix(conf): fix the ambigity when we get a list of confs Signed-off-by: Vincenzo Palazzo --- conf/src/lib.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/conf/src/lib.rs b/conf/src/lib.rs index 105019f..8356029 100644 --- a/conf/src/lib.rs +++ b/conf/src/lib.rs @@ -78,17 +78,48 @@ impl CLNConf { Ok(()) } - pub fn get_conf(&self, key: &str) -> Option> { + /// Get a unique field with the specified key, if there are multiple definition + /// the function return an error. + /// + /// In the case of multiple definition of the same key you would like to use `get_confs`. + pub fn get_conf(&self, key: &str) -> Result, ParsingError> { + let mut results = vec![]; + if let Some(fields) = self.fields.get(key) { + results.append(&mut fields.clone()); + } + for include in &self.includes { + let fields = include.get_confs(key); + if !fields.is_empty() { + results.append(&mut fields.clone()); + } + } + if results.is_empty() { + return Ok(None); + } + + if results.len() > 1 { + return Err(ParsingError { + core: 1, + cause: "mutiple field with the `{key}`".to_owned(), + }); + } + Ok(Some(results.first().unwrap().clone())) + } + + /// Return a list of values with the specified key, if no + /// item is found, return an empity vector. + pub fn get_confs(&self, key: &str) -> Vec { let mut results = vec![]; if let Some(fields) = self.fields.get(key) { results.append(&mut fields.clone()); } for include in &self.includes { - if let Some(fields) = include.get_conf(key) { + let fields = include.get_confs(key); + if !fields.is_empty() { results.append(&mut fields.clone()); } } - Some(results) + results } pub fn add_subconf(&mut self, conf: CLNConf) -> Result<(), ParsingError> {