Skip to content

Commit

Permalink
Merge pull request #341 from Kijewski/pr-missing-block
Browse files Browse the repository at this point in the history
derive: fail if requested template block is missing
  • Loading branch information
GuillaumeGomez authored Feb 9, 2025
2 parents a0e19aa + 13eea3b commit a0c8c43
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
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")]
| ^^^

0 comments on commit a0c8c43

Please sign in to comment.