From 33415a14bbe1ecee01aa78981b55fff956202491 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 21 Oct 2022 20:48:14 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=B0=EF=B8=8F=20[config]=20Elimina=20c?= =?UTF-8?q?=C3=B3digo=20no=20usado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/config/data.rs | 81 +---------------------------------- pagetop/src/config/path.rs | 86 -------------------------------------- 2 files changed, 1 insertion(+), 166 deletions(-) diff --git a/pagetop/src/config/data.rs b/pagetop/src/config/data.rs index b9f72de7..22fe8359 100644 --- a/pagetop/src/config/data.rs +++ b/pagetop/src/config/data.rs @@ -1,7 +1,7 @@ use crate::config::error::*; use crate::config::path; use crate::config::source::Source; -use crate::config::value::{Table, Value}; +use crate::config::value::Value; use serde::de::Deserialize; @@ -38,14 +38,6 @@ pub struct ConfigData { } impl ConfigData { - pub fn new() -> Self { - Self { - kind: ConfigKind::default(), - // Config root should be instantiated as an empty table to avoid deserialization errors. - cache: Value::new(None, Table::new()), - } - } - /// Merge in a configuration property source. pub fn merge(&mut self, source: T) -> Result<&mut ConfigData> where @@ -63,24 +55,6 @@ impl ConfigData { self.refresh() } - /// Merge in a configuration property source. - pub fn with_merged(mut self, source: T) -> Result - where - T: 'static, - T: Source + Send + Sync, - { - match self.kind { - ConfigKind::Mutable { - ref mut sources, .. - } => { - sources.push(Box::new(source)); - } - } - - self.refresh()?; - Ok(self) - } - /// Refresh the configuration cache with fresh data from added sources. /// /// Configuration is automatically refreshed after a mutation operation (`set`, `merge`, @@ -145,59 +119,6 @@ impl ConfigData { self.refresh() } - pub fn set_once(&mut self, key: &str, value: Value) -> Result<()> { - let expr: path::Expression = key.parse()?; - - // Traverse the cache using the path to (possibly) retrieve a value. - if let Some(ref mut val) = expr.get_mut(&mut self.cache) { - **val = value; - } else { - expr.set(&mut self.cache, value); - } - Ok(()) - } - - pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result { - // Parse the key into a path expression. - let expr: path::Expression = key.parse()?; - - // Traverse the cache using the path to (possibly) retrieve a value. - let value = expr.get(&self.cache).cloned(); - - match value { - Some(value) => { - // Deserialize the received value into the requested type. - T::deserialize(value).map_err(|e| e.extend_with_key(key)) - } - - None => Err(ConfigError::NotFound(key.into())), - } - } - - pub fn get_str(&self, key: &str) -> Result { - self.get(key).and_then(Value::into_str) - } - - pub fn get_int(&self, key: &str) -> Result { - self.get(key).and_then(Value::into_int) - } - - pub fn get_float(&self, key: &str) -> Result { - self.get(key).and_then(Value::into_float) - } - - pub fn get_bool(&self, key: &str) -> Result { - self.get(key).and_then(Value::into_bool) - } - - pub fn get_table(&self, key: &str) -> Result> { - self.get(key).and_then(Value::into_table) - } - - pub fn get_array(&self, key: &str) -> Result> { - self.get(key).and_then(Value::into_array) - } - /// Attempt to deserialize the entire configuration into the requested type. pub fn try_into<'de, T: Deserialize<'de>>(self) -> Result { T::deserialize(self) diff --git a/pagetop/src/config/path.rs b/pagetop/src/config/path.rs index 1afe2ddd..e99c6d68 100644 --- a/pagetop/src/config/path.rs +++ b/pagetop/src/config/path.rs @@ -30,92 +30,6 @@ fn sindex_to_uindex(index: isize, len: usize) -> usize { } impl Expression { - pub fn get(self, root: &Value) -> Option<&Value> { - match self { - Expression::Identifier(id) => { - match root.kind { - // `x` access on a table is equivalent to: map[x]. - ValueKind::Table(ref map) => map.get(&id), - - // All other variants return None. - _ => None, - } - } - - Expression::Child(expr, key) => { - match expr.get(root) { - Some(value) => { - match value.kind { - // Access on a table is identical to Identifier, it just forwards. - ValueKind::Table(ref map) => map.get(&key), - - // All other variants return None. - _ => None, - } - } - - _ => None, - } - } - - Expression::Subscript(expr, index) => match expr.get(root) { - Some(value) => match value.kind { - ValueKind::Array(ref array) => { - let index = sindex_to_uindex(index, array.len()); - - if index >= array.len() { - None - } else { - Some(&array[index]) - } - } - - _ => None, - }, - - _ => None, - }, - } - } - - pub fn get_mut<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> { - match *self { - Expression::Identifier(ref id) => match root.kind { - ValueKind::Table(ref mut map) => map.get_mut(id), - - _ => None, - }, - - Expression::Child(ref expr, ref key) => match expr.get_mut(root) { - Some(value) => match value.kind { - ValueKind::Table(ref mut map) => map.get_mut(key), - - _ => None, - }, - - _ => None, - }, - - Expression::Subscript(ref expr, index) => match expr.get_mut(root) { - Some(value) => match value.kind { - ValueKind::Array(ref mut array) => { - let index = sindex_to_uindex(index, array.len()); - - if index >= array.len() { - None - } else { - Some(&mut array[index]) - } - } - - _ => None, - }, - - _ => None, - }, - } - } - pub fn get_mut_forcibly<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> { match *self { Expression::Identifier(ref id) => match root.kind {