Merge pull request #46 from Kijewski/pr-one-less-allow

Remove a `#[allow(clippy::match_wild_err_arm)]`  and  dedup error message creation
This commit is contained in:
Guillaume Gomez 2024-07-04 11:08:45 +02:00 committed by GitHub
commit e3c1fe3548
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 22 deletions

View File

@ -298,38 +298,27 @@ where
vals.iter().map(|s| s.to_string()).collect()
}
#[allow(clippy::match_wild_err_arm)]
pub(crate) fn get_template_source(
tpl_path: &Path,
import_from: Option<(&Rc<Path>, &str, &str)>,
) -> std::result::Result<String, CompileError> {
) -> Result<String, CompileError> {
match fs::read_to_string(tpl_path) {
Err(_) => {
if let Some((node_file, file_source, node_source)) = import_from {
Err(CompileError::new(
format!(
"unable to open template file '{}'",
tpl_path.to_str().unwrap(),
),
Some(FileInfo::new(
node_file,
Some(file_source),
Some(node_source),
)),
))
} else {
Err(CompileError::no_file_info(format!(
"unable to open template file '{}'",
tpl_path.to_str().unwrap()
)))
}
}
Ok(mut source) => {
if source.ends_with('\n') {
let _ = source.pop();
}
Ok(source)
}
Err(err) => {
let msg = format!(
"unable to open template file '{}': {err}",
tpl_path.to_str().unwrap(),
);
let file_info = import_from.map(|(node_file, file_source, node_source)| {
FileInfo::new(node_file, Some(file_source), Some(node_source))
});
Err(CompileError::new(msg, file_info))
}
}
}

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,8 @@
use rinja::Template;
#[derive(Template)]
#[template(ext = "txt", source = r#"{% include "a_file_that_is_actually_a_folder.html" %}"#)]
struct YouCannotIncludeFolders;
fn main() {
}

View File

@ -0,0 +1,9 @@
error: unable to open template file '$WORKSPACE/target/tests/trybuild/rinja_testing/templates/a_file_that_is_actually_a_folder.html': Is a directory (os error 21)
--> YouCannotIncludeFolders.txt:1:2
" include \"a_file_that_is_actually_a_fold"...
--> tests/ui/include-a-folder.rs:3:10
|
3 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)