diff --git a/README.md b/README.md index 02660d83..8bcd7fff 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,6 @@ values back to the configuration file(s)! ## Usage -```toml -[dependencies] -config = "0.14.0" -``` - ### Feature flags - `ini` - Adds support for reading INI files diff --git a/src/builder.rs b/src/builder.rs index 6c6ab559..fb0eea3d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -125,9 +125,8 @@ enum SourceType { impl BuilderState for DefaultState {} impl BuilderState for AsyncState {} +/// Operations allowed in any state impl ConfigBuilder { - // operations allowed in any state - /// Set a default `value` at `key` /// /// This value can be overwritten by any [`Source`], [`AsyncSource`] or override. @@ -183,9 +182,8 @@ impl ConfigBuilder { } } +/// Operations allowed in sync state impl ConfigBuilder { - // operations allowed in sync state - /// 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. @@ -273,9 +271,8 @@ impl ConfigBuilder { } } +/// Operations allowed in async state impl ConfigBuilder { - // operations allowed in async state - /// 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. diff --git a/src/config.rs b/src/config.rs index 64d6b7f1..a994dd9b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,8 +11,9 @@ use crate::ser::ConfigSerializer; use crate::source::Source; use crate::value::{Table, Value}; -/// A prioritized configuration repository. It maintains a set of -/// configuration sources, fetches values to populate those, and provides +/// A prioritized configuration repository. +/// +/// It maintains a set of configuration sources, fetches values to populate those, and provides /// them according to the source's priority. #[derive(Clone, Debug)] pub struct Config { diff --git a/src/file/mod.rs b/src/file/mod.rs index 04fe2def..72e0a361 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -15,6 +15,14 @@ pub use self::format::FileFormat; pub use self::source::file::FileSourceFile; pub use self::source::string::FileSourceString; +/// An extension of [`Format`] trait. +/// +/// Associates format with file extensions, therefore linking storage-agnostic notion of format to a file system. +pub trait FileStoredFormat: Format { + /// Returns a vector of file extensions, for instance `[yml, yaml]`. + fn file_extensions(&self) -> &'static [&'static str]; +} + /// A configuration source backed up by a file. /// /// It supports optional automatic file format discovery. @@ -30,14 +38,6 @@ pub struct File { required: bool, } -/// An extension of [`Format`] trait. -/// -/// Associates format with file extensions, therefore linking storage-agnostic notion of format to a file system. -pub trait FileStoredFormat: Format { - /// Returns a vector of file extensions, for instance `[yml, yaml]`. - fn file_extensions(&self) -> &'static [&'static str]; -} - impl File where F: FileStoredFormat + 'static, @@ -67,15 +67,32 @@ where impl File { /// Given the basename of a file, will attempt to locate a file by setting its /// extension to a registered format. - pub fn with_name(name: &str) -> Self { + pub fn with_name(base_name: &str) -> Self { Self { format: None, required: true, - source: FileSourceFile::new(name.into()), + source: FileSourceFile::new(base_name.into()), } } } +impl File +where + F: FileStoredFormat + 'static, + T: FileSource, +{ + pub fn format(mut self, format: F) -> Self { + self.format = Some(format); + self + } + + /// Set required to false to make a file optional when building the config. + pub fn required(mut self, required: bool) -> Self { + self.required = required; + self + } +} + impl<'a> From<&'a Path> for File { fn from(path: &'a Path) -> Self { Self { @@ -96,23 +113,6 @@ impl From for File { } } -impl File -where - F: FileStoredFormat + 'static, - T: FileSource, -{ - pub fn format(mut self, format: F) -> Self { - self.format = Some(format); - self - } - - /// Set required to false to make a file optional when building the config. - pub fn required(mut self, required: bool) -> Self { - self.required = required; - self - } -} - impl Source for File where F: FileStoredFormat + Debug + Clone + Send + Sync + 'static, diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index d017a765..393199be 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -6,7 +6,7 @@ use std::fmt::Debug; use crate::{file::FileStoredFormat, Format}; -/// Describes where the file is sourced +/// Describes where the [`File`][super::File] is sourced pub trait FileSource: Debug + Clone where T: Format + FileStoredFormat, diff --git a/src/lib.rs b/src/lib.rs index 09cbafb7..33957aae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,21 +1,29 @@ -//! Config organizes hierarchical or layered configurations for Rust applications. +//! [`Config`] organizes hierarchical or layered configurations for Rust applications. //! -//! Config lets you set a set of default parameters and then extend them via merging in +//! [`Config`] lets you set a set of [default parameters][ConfigBuilder::set_default] and then extend them via merging in //! configuration from a variety of sources: //! -//! - Environment variables -//! - String literals in well-known formats -//! - Another Config instance -//! - Files: TOML, JSON, YAML, INI, RON, JSON5 and custom ones defined with Format trait -//! - Manual, programmatic override (via a `.set` method on the Config instance) +//! - [Environment variables][Environment] +//! - [String literals][FileSourceString] in [well-known formats][FileFormat] +//! - Another [`Config`] instance +//! - [Files][FileSourceFile] in [well known formats][FileFormat] and custom ones defined with [`Format`] trait +//! - Manual, programmatic [overrides][ConfigBuilder::set_override] //! -//! Additionally, Config supports: +//! Additionally, [`Config`] supports: //! //! - Live watching and re-reading of configuration files //! - Deep access into the merged configuration via a path syntax //! - Deserialization via `serde` of the configuration or any subset defined via a path //! -//! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for +//! # Example +//! +//! ```rust +//! # #[cfg(feature = "toml")] { +#![doc = include_str!("../examples/simple/main.rs")] +//! # } +//! ``` +//! +//! See more [examples](https://github.com/mehcode/config-rs/tree/master/examples) for //! general usage information. #![cfg_attr(docsrs, feature(doc_auto_cfg))] diff --git a/src/map.rs b/src/map.rs index 5873f0d2..36900aeb 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,7 @@ +/// The backing store for [`Config`][crate::Config] +pub type Map = InternalMap; + #[cfg(not(feature = "preserve_order"))] -pub type Map = std::collections::HashMap; +type InternalMap = std::collections::HashMap; #[cfg(feature = "preserve_order")] -pub type Map = indexmap::IndexMap; +type InternalMap = indexmap::IndexMap; diff --git a/src/source.rs b/src/source.rs index aba24368..2923fc9e 100644 --- a/src/source.rs +++ b/src/source.rs @@ -13,8 +13,8 @@ use crate::value::{Value, ValueKind}; pub trait Source: Debug { fn clone_into_box(&self) -> Box; - /// Collect all configuration properties available from this source and return - /// a Map. + /// Collect all configuration properties available from this source into + /// a [`Map`]. fn collect(&self) -> Result>; /// Collects all configuration properties to a provided cache. diff --git a/src/value.rs b/src/value.rs index d07bbbe6..97956dfe 100644 --- a/src/value.rs +++ b/src/value.rs @@ -9,8 +9,8 @@ use crate::map::Map; /// Underlying kind of the configuration value. /// -/// Standard operations on a `Value` by users of this crate do not require -/// knowledge of `ValueKind`. Introspection of underlying kind is only required +/// Standard operations on a [`Value`] by users of this crate do not require +/// knowledge of [`ValueKind`]. Introspection of underlying kind is only required /// when the configuration values are unstructured or do not have known types. #[derive(Debug, Clone, PartialEq)] pub enum ValueKind {