Skip to content

Commit

Permalink
derive: way less noisy error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jul 29, 2024
1 parent 380ecc1 commit 1110a8b
Show file tree
Hide file tree
Showing 39 changed files with 429 additions and 580 deletions.
46 changes: 27 additions & 19 deletions rinja_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,13 @@ impl Config {
}
}

Err(CompileError::no_file_info(format!(
"template {:?} not found in directories {:?}",
path, self.dirs
)))
Err(CompileError::no_file_info(
format!(
"template {:?} not found in directories {:?}",
path, self.dirs
),
None,
))
}
}

Expand Down Expand Up @@ -319,13 +322,15 @@ impl<'a> TryInto<Syntax<'a>> for RawSyntax<'a> {
syntax.comment_end,
] {
if s.len() < 2 {
return Err(CompileError::no_file_info(format!(
"delimiters must be at least two characters long: {s:?}"
)));
return Err(CompileError::no_file_info(
format!("delimiters must be at least two characters long: {s:?}"),
None,
));
} else if s.chars().any(|c| c.is_whitespace()) {
return Err(CompileError::no_file_info(format!(
"delimiters may not contain white spaces: {s:?}"
)));
return Err(CompileError::no_file_info(
format!("delimiters may not contain white spaces: {s:?}"),
None,
));
}
}

Expand All @@ -335,9 +340,12 @@ impl<'a> TryInto<Syntax<'a>> for RawSyntax<'a> {
(syntax.expr_start, syntax.comment_start),
] {
if s1.starts_with(s2) || s2.starts_with(s1) {
return Err(CompileError::no_file_info(format!(
"a delimiter may not be the prefix of another delimiter: {s1:?} vs {s2:?}",
)));
return Err(CompileError::no_file_info(
format!(
"a delimiter may not be the prefix of another delimiter: {s1:?} vs {s2:?}",
),
None,
));
}
}

Expand All @@ -358,7 +366,7 @@ impl RawConfig<'_> {
#[cfg(feature = "config")]
fn from_toml_str(s: &str) -> Result<RawConfig<'_>, CompileError> {
basic_toml::from_str(s).map_err(|e| {
CompileError::no_file_info(format!("invalid TOML in {CONFIG_FILE_NAME}: {e}"))
CompileError::no_file_info(format!("invalid TOML in {CONFIG_FILE_NAME}: {e}"), None)
})
}

Expand Down Expand Up @@ -428,13 +436,13 @@ pub(crate) fn read_config_file(config_path: Option<&str>) -> Result<String, Comp

if filename.exists() {
fs::read_to_string(&filename).map_err(|_| {
CompileError::no_file_info(format!("unable to read {:?}", filename.to_str().unwrap()))
CompileError::no_file_info(format!("unable to read {}", filename.display()), None)
})
} else if config_path.is_some() {
Err(CompileError::no_file_info(format!(
"`{}` does not exist",
root.display()
)))
Err(CompileError::no_file_info(
format!("`{}` does not exist", root.display()),
None,
))
} else {
Ok("".to_string())
}
Expand Down
Loading

0 comments on commit 1110a8b

Please sign in to comment.