Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive: fail if requested template block is missing #341

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions rinja_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,24 @@ fn build_template_item(

let ctx = &contexts[&input.path];
let heritage = if !ctx.blocks.is_empty() || ctx.extends.is_some() {
let heritage = Heritage::new(ctx, &contexts);

if let Some((block_name, block_span)) = input.block {
if !heritage.blocks.contains_key(&block_name) {
return Err(CompileError::no_file_info(
format_args!("cannot find block `{block_name}`"),
Some(block_span),
));
}
}

Some(heritage)
Some(Heritage::new(ctx, &contexts))
} else {
None
};

if let Some((block_name, block_span)) = input.block {
let has_block = match &heritage {
Some(heritage) => heritage.blocks.contains_key(block_name),
None => ctx.blocks.contains_key(block_name),
};
if !has_block {
return Err(CompileError::no_file_info(
format_args!("cannot find block `{block_name}`"),
Some(block_span),
));
}
}

if input.print == Print::Ast || input.print == Print::All {
eprintln!("{:?}", templates[&input.path].nodes());
}
Expand Down
1 change: 1 addition & 0 deletions testing/templates/no-block-with-base-template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "no-block-with-include-times-2.txt" %}
1 change: 1 addition & 0 deletions testing/templates/no-block-with-include-times-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include "no-block-with-include.txt" %}
1 change: 1 addition & 0 deletions testing/templates/no-block-with-include.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include "no-block.txt" %}
Empty file added testing/templates/no-block.txt
Empty file.
28 changes: 28 additions & 0 deletions testing/tests/ui/no-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use rinja::Template;

#[derive(Template)]
#[template(
ext = "txt",
source = "{% block not_a %}{% endblock %}",
block = "a"
)]
struct SourceTemplate;

#[derive(Template)]
#[template(path = "no-block.txt", block = "a")]
struct PathTemplate;

#[derive(Template)]
#[template(path = "no-block-with-include.txt", block = "a")]
struct NoBlockWithInclude;

#[derive(Template)]
#[template(path = "no-block-with-include-times-2.txt", block = "a")]
struct NoBlockWithIncludeTimes2;

#[derive(Template)]
#[template(path = "no-block-with-base-template.txt", block = "a")]
struct NoBlockWithBaseTemplate;

fn main() {
}
29 changes: 29 additions & 0 deletions testing/tests/ui/no-block.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: cannot find block `a`
--> tests/ui/no-block.rs:7:13
|
7 | block = "a"
| ^^^

error: cannot find block `a`
--> tests/ui/no-block.rs:12:43
|
12 | #[template(path = "no-block.txt", block = "a")]
| ^^^

error: cannot find block `a`
--> tests/ui/no-block.rs:16:56
|
16 | #[template(path = "no-block-with-include.txt", block = "a")]
| ^^^

error: cannot find block `a`
--> tests/ui/no-block.rs:20:64
|
20 | #[template(path = "no-block-with-include-times-2.txt", block = "a")]
| ^^^

error: cannot find block `a`
--> tests/ui/no-block.rs:24:62
|
24 | #[template(path = "no-block-with-base-template.txt", block = "a")]
| ^^^
Loading