diff options
author | Radosław Kot <rdkt13@gmail.com> | 2021-04-03 11:11:31 +0200 |
---|---|---|
committer | Radosław Kot <rdkt13@gmail.com> | 2021-04-03 11:11:31 +0200 |
commit | ee1c0618e855005f52888be0c3699132c1e2919d (patch) | |
tree | d87f183444b44401d707584f8dd0c5f216e184fc | |
parent | 95001bf4c3ff0ab64a72246a17040c50f1663575 (diff) |
/// comments, rustfmt
-rw-r--r-- | src/builder.rs | 215 | ||||
-rw-r--r-- | src/config.rs | 8 | ||||
-rw-r--r-- | src/lib.rs | 4 |
3 files changed, 103 insertions, 124 deletions
diff --git a/src/builder.rs b/src/builder.rs index dbf1aab..a2b1080 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,76 +1,66 @@ -use std::{ - collections::HashMap, - iter::IntoIterator -}; +use std::{collections::HashMap, iter::IntoIterator}; use crate::{ - config::Config, - value::Value, - source::Source, - error, - error::ConfigError, - path::Expression + config::Config, error, error::ConfigError, path::Expression, source::Source, value::Value, }; -/** -A configuration builder - -It registers ordered sources of configuration to later build consistent [`Config`] from them. -Configuration sources it defines are defaults, [`Source`]s and overrides. - -Defaults are alaways loaded first and can be overwritten by any of two other sources. -Overrides are always loaded last, thus cannot be overridden. -Both can be only set explicitly key by key in code -using [`set_default`](Self::set_default) or [`set_override`](Self::set_override). - -An intermediate category, [`Source`], set groups of keys at once implicitly using data coming from external sources -like files, environment variables or others that one implements. Defining a [`Source`] is as simple as implementing -a trait for a struct. - -Adding sources, setting defaults and overrides does not invoke any I/O nor builds a config. -It happens on demand when [`build`](Self::build) (or its alternative) is called. -Therefore all errors, related to any of the [`Source`] will only show up then. - -# Examples - -```rust -# use config::*; -# use std::error::Error; -# fn main() -> Result<(), Box<dyn Error>> { -let mut builder = ConfigBuilder::default(); - -builder.set_default("default", "1")?; -builder.add_source(File::new("config/settings", FileFormat::Json)); -builder.set_override("override", "1")?; - -match builder.build() { - Ok(config) => { - // use your config - }, - Err(e) => { - // something went wrong - } -} -# Ok(()) -# } -``` - -Calls can be chained as well -```rust -# use std::error::Error; -# use config::*; -# fn main() -> Result<(), Box<dyn Error>> { -let mut builder = ConfigBuilder::default(); - -builder - .set_default("default", "1")? - .add_source(File::new("config/settings", FileFormat::Json)) - .add_source(File::new("config/settings.prod", FileFormat::Json)) - .set_override("override", "1")?; -# Ok(()) -# } -``` -*/ +/// A configuration builder +/// +/// It registers ordered sources of configuration to later build consistent [`Config`] from them. +/// Configuration sources it defines are defaults, [`Source`]s and overrides. +/// +/// Defaults are alaways loaded first and can be overwritten by any of two other sources. +/// Overrides are always loaded last, thus cannot be overridden. +/// Both can be only set explicitly key by key in code +/// using [`set_default`](Self::set_default) or [`set_override`](Self::set_override). +/// +/// An intermediate category, [`Source`], set groups of keys at once implicitly using data coming from external sources +/// like files, environment variables or others that one implements. Defining a [`Source`] is as simple as implementing +/// a trait for a struct. +/// +/// Adding sources, setting defaults and overrides does not invoke any I/O nor builds a config. +/// It happens on demand when [`build`](Self::build) (or its alternative) is called. +/// Therefore all errors, related to any of the [`Source`] will only show up then. +/// +/// # Examples +/// +/// ```rust +/// # use config::*; +/// # use std::error::Error; +/// # fn main() -> Result<(), Box<dyn Error>> { +/// let mut builder = ConfigBuilder::default(); +/// +/// builder.set_default("default", "1")?; +/// builder.add_source(File::new("config/settings", FileFormat::Json)); +/// builder.set_override("override", "1")?; +/// +/// match builder.build() { +/// Ok(config) => { +/// // use your config +/// }, +/// Err(e) => { +/// // something went wrong +/// } +/// } +/// # Ok(()) +/// # } +/// ``` +/// +/// Calls can be chained as well +/// ```rust +/// # use std::error::Error; +/// # use config::*; +/// # fn main() -> Result<(), Box<dyn Error>> { +/// let mut builder = ConfigBuilder::default(); +/// +/// builder +/// .set_default("default", "1")? +/// .add_source(File::new("config/settings", FileFormat::Json)) +/// .add_source(File::new("config/settings.prod", FileFormat::Json)) +/// .set_override("override", "1")?; +/// # Ok(()) +/// # } +/// ``` #[derive(Debug, Clone, Default)] pub struct ConfigBuilder { defaults: HashMap<Expression, Value>, @@ -78,17 +68,13 @@ pub struct ConfigBuilder { sources: Vec<Box<dyn Source + Send + Sync>>, } -impl ConfigBuilder -{ - /** - Set a default `value` at `key` - - This value can be overwritten by any [`Source`] or override. - - # Errors - - Method can fail if `key` is not valid. - */ +impl ConfigBuilder { + /// Set a default `value` at `key` + /// + /// This value can be overwritten by any [`Source`] or override. + /// + /// # Errors + /// Method can fail if `key` is not valid. pub fn set_default<T>(&mut self, key: &str, value: T) -> error::Result<&mut ConfigBuilder> where T: Into<Value>, @@ -97,27 +83,24 @@ impl ConfigBuilder Ok(self) } - /** - Registers new [`Source`] in this builder. - - Calling this method does not invoke any I/O. [`Source`] is only saved in internal register for later use. - */ - pub fn add_source<T>(&mut self, source : T) -> &mut Self + /// Registers new [`Source`] in this builder. + /// + /// Calling this method does not invoke any I/O. [`Source`] is only saved in internal register for later use. + pub fn add_source<T>(&mut self, source: T) -> &mut Self where - T: Source + Send + Sync + 'static + T: Source + Send + Sync + 'static, { self.sources.push(Box::new(source)); self } - /** Set an override - - This function sets an overwrite value. It will not be altered by any default or [`Source`] - - # Errors - - Method can fail if `key` is not valid. - */ + /// Set an override + /// + /// This function sets an overwrite value. It will not be altered by any default or [`Source`] + /// + /// # Errors + /// + /// Method can fail if `key` is not valid. pub fn set_override<T>(&mut self, key: &str, value: T) -> error::Result<&mut ConfigBuilder> where T: Into<Value>, @@ -126,31 +109,27 @@ impl ConfigBuilder Ok(self) } - /** - Reads all registered [`Source`]s. - - This is the method that invokes all I/O operations. - For a non consuming alternative see [`build_cloned`](Self::build_cloned) - - # Errors - If source collection fails, be it technical reasons or related to inability to read data as `Config` for different reasons, - this method returns error. - */ + /// Reads all registered [`Source`]s. + /// + /// This is the method that invokes all I/O operations. + /// For a non consuming alternative see [`build_cloned`](Self::build_cloned) + /// + /// # Errors + /// If source collection fails, be it technical reasons or related to inability to read data as `Config` for different reasons, + /// this method returns error. pub fn build(self) -> error::Result<Config> { Self::build_internal(self.defaults, self.overrides, &self.sources) } - /** - Reads all registered [`Source`]s. - - Similar to [`build`](Self::build), but it does not take ownership of `ConfigBuilder` to allow later reuse. - Internally it clones data to achieve it. - - # Errors - If source collection fails, be it technical reasons or related to inability to read data as `Config` for different reasons, - this method returns error. - */ - pub fn build_cloned(&self) -> error::Result<Config> { + /// Reads all registered [`Source`]s. + /// + /// Similar to [`build`](Self::build), but it does not take ownership of `ConfigBuilder` to allow later reuse. + /// Internally it clones data to achieve it. + /// + /// # Errors + /// If source collection fails, be it technical reasons or related to inability to read data as `Config` for different reasons, + /// this method returns error. + pub fn build_cloned(&self) -> error::Result<Config> { Self::build_internal(self.defaults.clone(), self.overrides.clone(), &self.sources) } @@ -174,6 +153,6 @@ impl ConfigBuilder key.set(&mut cache, val); } - Ok(Config::new(cache)) + Ok(Config::new(cache)) } -}
\ No newline at end of file +} diff --git a/src/config.rs b/src/config.rs index 8000f8e..aebbadd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,10 +5,10 @@ use std::fmt::Debug; use std::ops::Deref; use std::str::FromStr; +use builder::ConfigBuilder; use error::*; use ser::ConfigSerializer; use source::Source; -use builder::ConfigBuilder; use path; use value::{Table, Value, ValueKind}; @@ -58,10 +58,10 @@ impl Default for Config { } impl Config { - pub(crate) fn new(value : Value) -> Self { + pub(crate) fn new(value: Value) -> Self { Config { - kind : ConfigKind::default(), - cache : value + kind: ConfigKind::default(), + cache: value, } } @@ -51,6 +51,7 @@ extern crate serde_hjson; #[cfg(feature = "ini")] extern crate ini; +mod builder; mod config; mod de; mod env; @@ -60,12 +61,11 @@ mod path; mod ser; mod source; mod value; -mod builder; +pub use builder::ConfigBuilder; pub use config::Config; pub use env::Environment; pub use error::ConfigError; pub use file::{File, FileFormat, FileSourceFile, FileSourceString}; pub use source::Source; pub use value::Value; -pub use builder::ConfigBuilder; |