Skip to content

Commit

Permalink
derive: don't panic if source+path are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 12, 2024
1 parent ea74031 commit 1c72f11
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rinja_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ impl TemplateInput<'_> {
// Validate the `source` and `ext` value together, since they are
// related. In case `source` was used instead of `path`, the value
// of `ext` is merged into a synthetic `path` value here.
let &(ref source, source_span) = source
.as_ref()
.expect("template path or source not found in attributes");
let &(ref source, source_span) = source.as_ref().ok_or_else(|| {
CompileError::new("template `path` or `source` not found in attributes", None)
})?;
let path = match (&source, &ext) {
(Source::Path(path), _) => config.find_template(path, None, None)?,
(&Source::Source(_), Some(ext)) => {
Expand Down
16 changes: 16 additions & 0 deletions rinja_derive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,20 @@ fn test_code_in_comment() {
let generated = build_template(&ast).unwrap();
assert!(generated.contains("Hello\nworld!"));
assert!(!generated.contains("compile_error"));

let ts = "
#[template(ext = \"txt\")]
/// `````
/// ```rinja
/// {{bla}}
/// ```
/// `````
struct BlockOnBlock;
";
let ast = syn::parse_str(ts).unwrap();
let err = build_template(&ast).unwrap_err();
assert_eq!(
err.to_string(),
"template `path` or `source` not found in attributes"
);
}
9 changes: 9 additions & 0 deletions testing/tests/ui/rinja-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ struct Unterminated;
/// ```
struct SyntaxError;

#[derive(Template)]
#[template(ext = "txt")]
/// `````
/// ```rinja
/// {{bla}}
/// ```
/// `````
struct BlockOnBlock;

fn main() {}
6 changes: 6 additions & 0 deletions testing/tests/ui/rinja-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ error: failed to parse template source
|
15 | /// ```html,rinja
| ^^^^^^^^^^^^^^^^^

error: template `path` or `source` not found in attributes
--> tests/ui/rinja-block.rs:23:3
|
23 | #[template(ext = "txt")]
| ^^^^^^^^

0 comments on commit 1c72f11

Please sign in to comment.