Skip to content

Commit

Permalink
new: Support help text. (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 10, 2023
1 parent 6f59efa commit 8c93aaa
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

#### 🚀 Updates

- Added `ConfigLoader.set_help` to customize help text for validation errors.

## 0.12.10

#### 🚀 Updates
Expand Down
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ serde_json = "1.0.108"
serde_yaml = "0.9.27"
toml = "0.8.8"
url = "2.5.0"
version_spec = "0.1.5"
warpgate = "0.5.14"
version_spec = "0.1.7"
warpgate = "0.7.0"
3 changes: 3 additions & 0 deletions crates/config/src/config/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub enum ConfigError {
// #[diagnostic_source]
#[source]
error: ValidatorError,

#[help]
help: Option<String>,
},
}

Expand Down
10 changes: 10 additions & 0 deletions crates/config/src/config/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ConfigLoadResult<T: Config> {
pub struct ConfigLoader<T: Config> {
_config: PhantomData<T>,
cacher: Mutex<BoxedCacher>,
help: Option<String>,
sources: Vec<Source>,
root: Option<PathBuf>,
}
Expand All @@ -35,6 +36,7 @@ impl<T: Config> ConfigLoader<T> {
ConfigLoader {
_config: PhantomData,
cacher: Mutex::new(Box::<MemoryCache>::default()),
help: None,
sources: vec![],
root: None,
}
Expand Down Expand Up @@ -104,6 +106,7 @@ impl<T: Config> ConfigLoader<T> {
None => T::META.name.to_owned(),
},
error,
help: self.help.clone(),
})?;

Ok(ConfigLoadResult {
Expand Down Expand Up @@ -135,6 +138,12 @@ impl<T: Config> ConfigLoader<T> {
self
}

/// Set a string of help text to include in validation errors.
pub fn set_help<H: AsRef<str>>(&mut self, help: H) -> &mut Self {
self.help = Some(help.as_ref().to_owned());
self
}

/// Set the project root directory, for use within error messages.
pub fn set_root<P: AsRef<Path>>(&mut self, root: P) -> &mut Self {
self.root = Some(root.as_ref().to_path_buf());
Expand Down Expand Up @@ -253,6 +262,7 @@ impl<T: Config> ConfigLoader<T> {
.map_err(|error| ConfigError::Validator {
config: location.to_owned(),
error,
help: self.help.clone(),
})?;

if let Some(extends_from) = partial.extends_from() {
Expand Down
2 changes: 1 addition & 1 deletion crates/config/src/config/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Source {
where
D: DeserializeOwned,
{
let handle_error = |error: crate::ParserError| ConfigError::Parser {
let handle_error = |error: crate::config::ParserError| ConfigError::Parser {
config: location.to_owned(),
error,
};
Expand Down
2 changes: 2 additions & 0 deletions crates/config/src/config/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ impl Display for ValidatorError {
}
}

writeln!(f)?;

Ok(())
}
}
1 change: 1 addition & 0 deletions crates/test-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn main() -> Result<()> {
// .code(r#"{ "string": "abc", "other": 123 }"#, Format::Json)?
// .code("{\n \"string\": 123\n}", Format::Json)?
.code("{\n \"string\": \"\" \n}", Format::Json)?
.set_help("let's go!")
.load()?;

dbg!(&config.config.string);
Expand Down

0 comments on commit 8c93aaa

Please sign in to comment.