Format code

This is `cargo fmt` but with lots of changes reverted and some adjusted.
This commit is contained in:
Lukas Kalbertodt
2022-10-24 10:13:06 +02:00
parent 324cedee47
commit 0e47eb7acf
16 changed files with 77 additions and 73 deletions

View File

@@ -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.

View File

@@ -19,9 +19,7 @@ pub struct Builder<C: Config> {
impl<C: Config> Builder<C> {
pub(crate) fn new() -> Self {
Self {
sources: vec![],
}
Self { sources: vec![] }
}
/// Adds a configuration file as source. Infers the format from the file

View File

@@ -52,14 +52,13 @@ macro_rules! deserialize_via_parse {
($method:ident, $visit_method:ident, $int:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>
V: serde::de::Visitor<'de>,
{
self.value.into_deserializer().deserialize_any(visitor)
}
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>
V: serde::de::Visitor<'de>,
{
let v = match self.value.trim() {
"1" | "true" | "TRUE" => true,

View File

@@ -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 {

View File

@@ -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<PathBuf>) -> Result<Self, Error> {
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 {

View File

@@ -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<I, O>(src: I) -> Result<O, serde::de::value::Error>
where
@@ -22,7 +23,7 @@ where
pub fn unwrap_or_missing_value_err<T>(value: Option<T>, path: &str) -> Result<T, Error> {
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<T>(
) -> Result<Option<T>, 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,
};

View File

@@ -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]

View File

@@ -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,
};

View File

@@ -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<Expr>,
},
Required { default: Option<Expr> },
/// 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)]

View File

@@ -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.

View File

@@ -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.

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -1,5 +1,6 @@
use pretty_assertions::assert_eq;
use confique::{Config, meta};
use confique::{meta, Config};
#[test]

View File

@@ -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<Dummy, D::Error>
where D: serde::Deserializer<'de>,
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap;
use pretty_assertions::assert_eq;
use confique::{Config, meta};
use confique::{meta, Config};
#[test]