diff --git a/examples/simple.rs b/examples/simple.rs index ce120c4..f73c6a4 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] -use std::{net::IpAddr, path::PathBuf}; use confique::Config; +use std::{net::IpAddr, path::PathBuf}; #[derive(Debug, Config)] /// A sample configuration for our app. diff --git a/src/builder.rs b/src/builder.rs index 6987422..4a310c4 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -19,9 +19,7 @@ pub struct Builder { impl Builder { pub(crate) fn new() -> Self { - Self { - sources: vec![], - } + Self { sources: vec![] } } /// Adds a configuration file as source. Infers the format from the file diff --git a/src/env.rs b/src/env.rs index 77049bb..33e3aa5 100644 --- a/src/env.rs +++ b/src/env.rs @@ -52,14 +52,13 @@ macro_rules! deserialize_via_parse { ($method:ident, $visit_method:ident, $int:ident) => { fn $method(self, visitor: V) -> Result where - V: serde::de::Visitor<'de> + V: serde::de::Visitor<'de>, { let s = self.value.trim(); let v = s.parse().map_err(|e| { DeError(format!( concat!("invalid value '{}' for type ", stringify!($int), ": {}"), - s, - e, + s, e, )) })?; visitor.$visit_method(v) @@ -72,14 +71,14 @@ impl<'de> serde::Deserializer<'de> for Deserializer { fn deserialize_any(self, visitor: V) -> Result where - V: serde::de::Visitor<'de> + V: serde::de::Visitor<'de>, { self.value.into_deserializer().deserialize_any(visitor) } fn deserialize_bool(self, visitor: V) -> Result where - V: serde::de::Visitor<'de> + V: serde::de::Visitor<'de>, { let v = match self.value.trim() { "1" | "true" | "TRUE" => true, diff --git a/src/error.rs b/src/error.rs index 6d85f39..f3e7e2e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,7 +13,10 @@ pub struct Error { // instead of repeating this cfg-attribute a lot in the rest of the file, we // just live with these unused variants. It's not like we need to optimize the // size of `ErrorInner`. -#[cfg_attr(not(any(feature = "toml", feature = "yaml", feature = "json5")), allow(dead_code))] +#[cfg_attr( + not(any(feature = "toml", feature = "yaml", feature = "json5")), + allow(dead_code) +)] pub(crate) enum ErrorInner { /// Returned by `Config::from_partial` when the partial does not contain /// values for all required configuration values. The string is a @@ -37,10 +40,7 @@ pub(crate) enum ErrorInner { }, /// When the env variable `key` is not Unicode. - EnvNotUnicode { - field: String, - key: String, - }, + EnvNotUnicode { field: String, key: String }, /// When deserialization via `env` fails. The string is what is passed to /// `serde::de::Error::custom`. @@ -53,20 +53,14 @@ pub(crate) enum ErrorInner { /// Returned by the [`Source`] impls for `Path` and `PathBuf` if the file /// extension is not supported by confique or if the corresponding Cargo /// feature of confique was not enabled. - UnsupportedFileFormat { - path: PathBuf, - }, + UnsupportedFileFormat { path: PathBuf }, /// Returned by the [`Source`] impls for `Path` and `PathBuf` if the path /// does not contain a file extension. - MissingFileExtension { - path: PathBuf, - }, + MissingFileExtension { path: PathBuf }, /// A file source was marked as required but the file does not exist. - MissingRequiredFile { - path: PathBuf, - } + MissingRequiredFile { path: PathBuf }, } impl std::error::Error for Error { diff --git a/src/file.rs b/src/file.rs index 5a3f7c2..b83e0e7 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,6 +1,6 @@ use std::{ffi::OsStr, fs, io, path::PathBuf}; -use crate::{Error, Partial, error::ErrorInner}; +use crate::{error::ErrorInner, Error, Partial}; /// A file as source for configuration. @@ -20,12 +20,11 @@ impl File { /// unknown, an error is returned. pub fn new(path: impl Into) -> Result { let path = path.into(); - let ext = path.extension().ok_or_else(|| { - ErrorInner::MissingFileExtension { path: path.clone() } - })?; - let format = FileFormat::from_extension(ext).ok_or_else(|| { - ErrorInner::UnsupportedFileFormat { path: path.clone() } - })?; + let ext = path + .extension() + .ok_or_else(|| ErrorInner::MissingFileExtension { path: path.clone() })?; + let format = FileFormat::from_extension(ext) + .ok_or_else(|| ErrorInner::UnsupportedFileFormat { path: path.clone() })?; Ok(Self::with_format(path, format)) } @@ -69,10 +68,12 @@ impl File { }; // Helper closure to create an error. - let error = |err| Error::from(ErrorInner::Deserialization { - err, - source: Some(format!("file '{}'", self.path.display())), - }); + let error = |err| { + Error::from(ErrorInner::Deserialization { + err, + source: Some(format!("file '{}'", self.path.display())), + }) + }; match self.format { #[cfg(feature = "toml")] @@ -87,7 +88,7 @@ impl File { FileFormat::Json5 => { let s = std::str::from_utf8(&file_content).map_err(|e| error(Box::new(e)))?; json5::from_str(s).map_err(|e| error(Box::new(e))) - }, + } } } } @@ -96,9 +97,12 @@ impl File { /// /// All enum variants are `#[cfg]` guarded with the respective crate feature. pub enum FileFormat { - #[cfg(feature = "toml")] Toml, - #[cfg(feature = "yaml")] Yaml, - #[cfg(feature = "json5")] Json5, + #[cfg(feature = "toml")] + Toml, + #[cfg(feature = "yaml")] + Yaml, + #[cfg(feature = "json5")] + Json5, } impl FileFormat { diff --git a/src/internal.rs b/src/internal.rs index a8ecaa5..ae68f54 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -2,7 +2,8 @@ //! intended to be used directly. None of this is covered by semver! Do not use //! any of this directly. -use crate::{Error, error::ErrorInner}; +use crate::{error::ErrorInner, Error}; + pub fn deserialize_default(src: I) -> Result where @@ -22,7 +23,7 @@ where pub fn unwrap_or_missing_value_err(value: Option, path: &str) -> Result { match value { Some(v) => Ok(v), - None => Err(ErrorInner::MissingValue(path.into()).into()) + None => Err(ErrorInner::MissingValue(path.into()).into()), } } @@ -50,10 +51,13 @@ pub fn from_env_with( ) -> Result, Error> { let s = match std::env::var(key) { Err(std::env::VarError::NotPresent) => return Ok(None), - Err(std::env::VarError::NotUnicode(_)) => return Err(ErrorInner::EnvNotUnicode { - key: key.into(), - field: field.into(), - }.into()), + Err(std::env::VarError::NotUnicode(_)) => { + let err = ErrorInner::EnvNotUnicode { + key: key.into(), + field: field.into(), + }; + return Err(err.into()); + } Ok(s) => s, }; diff --git a/src/json5.rs b/src/json5.rs index c9ca299..e3c9536 100644 --- a/src/json5.rs +++ b/src/json5.rs @@ -120,7 +120,10 @@ impl Json5Formatter { } fn dec_depth(&mut self) { - self.depth = self.depth.checked_sub(1).expect("formatter bug: ended too many nested"); + self.depth = self + .depth + .checked_sub(1) + .expect("formatter bug: ended too many nested"); } } @@ -191,8 +194,8 @@ impl fmt::Display for PrintExpr { #[cfg(test)] mod tests { - use crate::test_utils::{self, include_format_output}; use super::{template, FormatOptions}; + use crate::test_utils::{self, include_format_output}; use pretty_assertions::assert_str_eq; #[test] diff --git a/src/lib.rs b/src/lib.rs index e4864e4..0f63300 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,8 +206,8 @@ pub use self::{ #[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))] pub use crate::{ - template::FormatOptions, file::{File, FileFormat}, + template::FormatOptions, }; diff --git a/src/meta.rs b/src/meta.rs index f344fbf..fbe53dc 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -40,9 +40,7 @@ pub enum FieldKind { #[derive(Clone, Copy, Debug, PartialEq)] pub enum LeafKind { /// A leaf field with a non `Option<_>` type. - Required { - default: Option, - }, + Required { default: Option }, /// A leaf field with an `Option<_>` type. Optional, } @@ -59,7 +57,7 @@ pub enum Expr { /// A key value map, stored as slice in source code order. #[serde(serialize_with = "serialize_map")] - Map(&'static [MapEntry]) + Map(&'static [MapEntry]), } #[derive(Debug, Clone, Copy, PartialEq, serde::Serialize)] diff --git a/src/test_utils/example1.rs b/src/test_utils/example1.rs index e2c330f..3f077b5 100644 --- a/src/test_utils/example1.rs +++ b/src/test_utils/example1.rs @@ -1,11 +1,7 @@ -use std::collections::HashMap; -use std::{ - net::IpAddr, - path::PathBuf, -}; +use std::{collections::HashMap, net::IpAddr, path::PathBuf}; -use crate::Config; use crate as confique; +use crate::Config; #[derive(Debug, Config)] /// A sample configuration for our app. diff --git a/src/test_utils/example2.rs b/src/test_utils/example2.rs index 3e23961..f9a90af 100644 --- a/src/test_utils/example2.rs +++ b/src/test_utils/example2.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; -use crate::Config; use crate as confique; +use crate::Config; #[derive(Debug, Config)] /// A sample configuration for our app. diff --git a/src/toml.rs b/src/toml.rs index 5b3ba0e..7217e6c 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -4,9 +4,9 @@ use std::fmt::{self, Write}; use crate::{ - Config, - template::{self, Formatter}, meta::{Expr, MapKey}, + template::{self, Formatter}, + Config, }; @@ -205,9 +205,10 @@ fn is_valid_bare_key(s: &str) -> bool { #[cfg(test)] mod tests { + use pretty_assertions::assert_str_eq; + use crate::test_utils::{self, include_format_output}; use super::{template, FormatOptions}; - use pretty_assertions::assert_str_eq; #[test] fn default() { diff --git a/src/yaml.rs b/src/yaml.rs index 25b2ef9..bea49f4 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -4,9 +4,9 @@ use std::fmt::{self, Write}; use crate::{ - Config, - template::{self, Formatter}, meta::Expr, + template::{self, Formatter}, + Config, }; @@ -147,7 +147,10 @@ impl Formatter for YamlFormatter { } fn end_nested(&mut self) { - self.depth = self.depth.checked_sub(1).expect("formatter bug: ended too many nested"); + self.depth = self + .depth + .checked_sub(1) + .expect("formatter bug: ended too many nested"); } fn start_main(&mut self) { @@ -200,7 +203,7 @@ impl fmt::Display for PrintExpr<'_> { } f.write_str(" }")?; Ok(()) - }, + } // All these other types can simply be serialized as is. Expr::Str(_) | Expr::Float(_) | Expr::Integer(_) | Expr::Bool(_) => { @@ -221,9 +224,11 @@ impl fmt::Display for PrintExpr<'_> { #[cfg(test)] mod tests { + use pretty_assertions::assert_str_eq; + use crate::test_utils::{self, include_format_output}; use super::{template, FormatOptions}; - use pretty_assertions::assert_str_eq; + #[test] fn default() { diff --git a/tests/array_default.rs b/tests/array_default.rs index 6170a53..ff979a7 100644 --- a/tests/array_default.rs +++ b/tests/array_default.rs @@ -1,5 +1,6 @@ use pretty_assertions::assert_eq; -use confique::{Config, meta}; + +use confique::{meta, Config}; #[test] diff --git a/tests/general.rs b/tests/general.rs index 6df5ae1..878b0bc 100644 --- a/tests/general.rs +++ b/tests/general.rs @@ -1,7 +1,7 @@ -use std::{path::PathBuf, net::IpAddr, collections::HashMap}; - +use std::{collections::HashMap, net::IpAddr, path::PathBuf}; use pretty_assertions::assert_eq; -use confique::{Config, meta, Partial}; + +use confique::{meta, Config, Partial}; #[test] @@ -272,7 +272,8 @@ fn full() { struct Dummy(String); pub(crate) fn deserialize_dummy<'de, D>(deserializer: D) -> Result - where D: serde::Deserializer<'de>, +where + D: serde::Deserializer<'de>, { use serde::Deserialize; diff --git a/tests/map_default.rs b/tests/map_default.rs index c18128d..adb560f 100644 --- a/tests/map_default.rs +++ b/tests/map_default.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; - use pretty_assertions::assert_eq; -use confique::{Config, meta}; + +use confique::{meta, Config}; #[test]