⚰️ [config] Elimina código no usado

This commit is contained in:
Manuel Cillero 2022-10-21 20:48:14 +02:00
parent 3167c3780d
commit 33415a14bb
2 changed files with 1 additions and 166 deletions

View file

@ -1,7 +1,7 @@
use crate::config::error::*; use crate::config::error::*;
use crate::config::path; use crate::config::path;
use crate::config::source::Source; use crate::config::source::Source;
use crate::config::value::{Table, Value}; use crate::config::value::Value;
use serde::de::Deserialize; use serde::de::Deserialize;
@ -38,14 +38,6 @@ pub struct ConfigData {
} }
impl 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. /// Merge in a configuration property source.
pub fn merge<T>(&mut self, source: T) -> Result<&mut ConfigData> pub fn merge<T>(&mut self, source: T) -> Result<&mut ConfigData>
where where
@ -63,24 +55,6 @@ impl ConfigData {
self.refresh() self.refresh()
} }
/// Merge in a configuration property source.
pub fn with_merged<T>(mut self, source: T) -> Result<Self>
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. /// Refresh the configuration cache with fresh data from added sources.
/// ///
/// Configuration is automatically refreshed after a mutation operation (`set`, `merge`, /// Configuration is automatically refreshed after a mutation operation (`set`, `merge`,
@ -145,59 +119,6 @@ impl ConfigData {
self.refresh() 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<T> {
// 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<String> {
self.get(key).and_then(Value::into_str)
}
pub fn get_int(&self, key: &str) -> Result<i64> {
self.get(key).and_then(Value::into_int)
}
pub fn get_float(&self, key: &str) -> Result<f64> {
self.get(key).and_then(Value::into_float)
}
pub fn get_bool(&self, key: &str) -> Result<bool> {
self.get(key).and_then(Value::into_bool)
}
pub fn get_table(&self, key: &str) -> Result<HashMap<String, Value>> {
self.get(key).and_then(Value::into_table)
}
pub fn get_array(&self, key: &str) -> Result<Vec<Value>> {
self.get(key).and_then(Value::into_array)
}
/// Attempt to deserialize the entire configuration into the requested type. /// Attempt to deserialize the entire configuration into the requested type.
pub fn try_into<'de, T: Deserialize<'de>>(self) -> Result<T> { pub fn try_into<'de, T: Deserialize<'de>>(self) -> Result<T> {
T::deserialize(self) T::deserialize(self)

View file

@ -30,92 +30,6 @@ fn sindex_to_uindex(index: isize, len: usize) -> usize {
} }
impl Expression { 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> { pub fn get_mut_forcibly<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> {
match *self { match *self {
Expression::Identifier(ref id) => match root.kind { Expression::Identifier(ref id) => match root.kind {