refactor: move config defaults from TOML files to Default trait (#352)

This commit is contained in:
Sanchith Hegde
2023-01-17 19:13:16 +05:30
committed by GitHub
parent 4f6a8fcd9d
commit beb0384047
20 changed files with 589 additions and 169 deletions

View File

@ -353,3 +353,34 @@ impl<A: Send, B> AsyncExt<A, B> for Option<A> {
}
}
}
/// Extension trait for validating application configuration. This trait provides utilities to
/// check whether the value is either the default value or is empty.
pub trait ConfigExt {
/// Returns whether the value of `self` is the default value for `Self`.
fn is_default(&self) -> bool
where
Self: Default + PartialEq<Self>,
{
*self == Self::default()
}
/// Returns whether the value of `self` is empty after trimming whitespace on both left and
/// right ends.
fn is_empty_after_trim(&self) -> bool;
/// Returns whether the value of `self` is the default value for `Self` or empty after trimming
/// whitespace on both left and right ends.
fn is_default_or_empty(&self) -> bool
where
Self: Default + PartialEq<Self>,
{
self.is_default() || self.is_empty_after_trim()
}
}
impl ConfigExt for String {
fn is_empty_after_trim(&self) -> bool {
self.trim().is_empty()
}
}