Skip to content

Commit

Permalink
Doens't work: no indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jul 29, 2024
1 parent 2e887a5 commit 821a147
Show file tree
Hide file tree
Showing 33 changed files with 111 additions and 510 deletions.
48 changes: 30 additions & 18 deletions rinja_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use proc_macro::TokenStream as TokenStream12;
#[cfg(feature = "__standalone")]
use proc_macro2::TokenStream as TokenStream12;
use proc_macro2::{Span, TokenStream};
use syn::parse_quote_spanned;

/// The `Template` derive macro and its `template()` attribute.
///
Expand Down Expand Up @@ -93,16 +94,26 @@ pub fn derive_template(input: TokenStream12) -> TokenStream12 {
let ast = syn::parse2(input.into()).unwrap();
match build_template(&ast) {
Ok(source) => source.parse().unwrap(),
Err(mut e) => {
if e.span.is_none() {
e.span = Some(ast.ident.span());
}
let mut e = e.into_compile_error();
Err(CompileError {
msg,
span,
rendered,
}) => {
let msg = if rendered {
eprintln!("{msg}");
"the previous template error derives from"
} else {
&msg
};
let mut ts: TokenStream = parse_quote_spanned! {
span.unwrap_or(ast.ident.span()) =>
::core::compile_error!(#msg);
};
if let Ok(source) = build_skeleton(&ast) {
let source: TokenStream = source.parse().unwrap();
e.extend(source);
ts.extend(source);
}
e.into()
ts.into()
}
}
}
Expand Down Expand Up @@ -210,6 +221,7 @@ fn build_template_inner(
struct CompileError {
msg: String,
span: Option<Span>,
rendered: bool,
}

impl CompileError {
Expand Down Expand Up @@ -246,32 +258,32 @@ impl CompileError {
.fold(true)
.annotation(annotation);
let message = Level::Error.title(&label).snippet(snippet);

let mut msg = Renderer::styled().render(message).to_string();
if let Some((prefix, _)) = msg.split_once(' ') {
msg.replace_range(..=prefix.len(), "");
}
return Self { msg, span };
return Self {
msg: Renderer::styled().render(message).to_string(),
span,
rendered: true,
};
}
}

let msg = match file_info {
Some(file_info) => format!("{msg}{file_info}"),
None => msg.to_string(),
};
Self { msg, span }
Self {
msg,
span,
rendered: false,
}
}

fn no_file_info<S: fmt::Display>(msg: S, span: Option<Span>) -> Self {
Self {
msg: msg.to_string(),
span,
rendered: false,
}
}

fn into_compile_error(self) -> TokenStream {
syn::Error::new(self.span.unwrap_or_else(Span::call_site), self.msg).to_compile_error()
}
}

impl std::error::Error for CompileError {}
Expand Down
42 changes: 6 additions & 36 deletions testing/tests/ui/as-primitive-type.stderr
Original file line number Diff line number Diff line change
@@ -1,64 +1,34 @@
error: `as` operator expects the name of a primitive type on its right-hand side
--> <source attribute>:1:9
|
1 | {{ 1234 as 4567 }}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/as-primitive-type.rs:4:12
|
4 | #[template(source = r#"{{ 1234 as 4567 }}"#, ext = "html")]
| ^^^^^^

error: `as` operator expects the name of a primitive type on its right-hand side
--> <source attribute>:1:9
|
1 | {{ 1234 as ? }}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/as-primitive-type.rs:8:12
|
8 | #[template(source = r#"{{ 1234 as ? }}"#, ext = "html")]
| ^^^^^^

error: `as` operator expects the name of a primitive type on its right-hand side, found `u1234`
--> <source attribute>:1:9
|
1 | {{ 1234 as u1234 }}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/as-primitive-type.rs:12:12
|
12 | #[template(source = r#"{{ 1234 as u1234 }}"#, ext = "html")]
| ^^^^^^

error: `as` operator expects the name of a primitive type on its right-hand side, found `core`
--> <source attribute>:1:9
|
1 | {{ 1234 as core::primitive::u32 }}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/as-primitive-type.rs:16:12
|
16 | #[template(source = r#"{{ 1234 as core::primitive::u32 }}"#, ext = "html")]
| ^^^^^^

error: `as` operator expects the name of a primitive type on its right-hand side, found `int32_t`
--> <source attribute>:1:9
|
1 | {{ 1234 as int32_t }}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/as-primitive-type.rs:20:12
|
20 | #[template(source = r#"{{ 1234 as int32_t }}"#, ext = "html")]
| ^^^^^^

error: `as` operator expects the name of a primitive type on its right-hand side, found `int32_t`
--> <source attribute>:1:36
|
1 | {{ (1234 + 4 * 12 / 45675445 - 13) as int32_t }}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/as-primitive-type.rs:24:12
|
24 | #[template(source = r#"{{ (1234 + 4 * 12 / 45675445 - 13) as int32_t }}"#, ext = "html")]
Expand Down
7 changes: 1 addition & 6 deletions testing/tests/ui/block_in_filter_block.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
error: cannot have a block inside a filter block
--> BlockInFilter.html:7:11
|
7 | {% block title %}New title{% endblock %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/block_in_filter_block.rs:5:5
|
5 | source = r#"{% extends "html-base.html" %}
Expand Down
21 changes: 3 additions & 18 deletions testing/tests/ui/blocks_below_top_level.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
error: `extends` blocks are not allowed below top level
--> MyTemplate1.txt:3:3
|
3 | {% extends "bla.txt" %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/blocks_below_top_level.rs:4:12
|
4 | #[template(source = r#"
| ^^^^^^

error: `macro` blocks are not allowed below top level
--> MyTemplate2.txt:3:3
|
3 | {% macro bla() %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/blocks_below_top_level.rs:12:12
|
12 | #[template(source = r#"
| ^^^^^^

error: `import` blocks are not allowed below top level
--> MyTemplate3.txt:3:3
|
3 | {% import "bla.txt" as blue %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/blocks_below_top_level.rs:21:12
|
21 | #[template(source = r#"
Expand Down
7 changes: 1 addition & 6 deletions testing/tests/ui/break_outside_of_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
error: you can only `break` inside a `for` loop
--> <source attribute>:1:10
|
1 | Have a {%break%}, have a parsing error!
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/break_outside_of_loop.rs:5:5
|
5 | source = "Have a {%break%}, have a parsing error!",
Expand Down
56 changes: 8 additions & 48 deletions testing/tests/ui/char_literal.stderr
Original file line number Diff line number Diff line change
@@ -1,86 +1,46 @@
error: invalid character
--> testing/templates/char-literals/char-literal-1.txt:1:12
|
1 | {% let s = '\a' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:4:12
|
4 | #[template(path = "char-literals/char-literal-1.txt")]
| ^^^^

error: invalid character
--> testing/templates/char-literals/char-literal-2.txt:1:12
|
1 | {% let s = '\x' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:8:12
|
8 | #[template(path = "char-literals/char-literal-2.txt")]
| ^^^^

error: invalid character
--> testing/templates/char-literals/char-literal-3.txt:1:12
|
1 | {% let s = '\x1' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:12:12
|
12 | #[template(path = "char-literals/char-literal-3.txt")]
| ^^^^

error: must be a character in the range [\x00-\x7f]
--> testing/templates/char-literals/char-literal-4.txt:1:12
|
1 | {% let s = '\x80' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:16:12
|
16 | #[template(path = "char-literals/char-literal-4.txt")]
| ^^^^

error: invalid character
--> testing/templates/char-literals/char-literal-5.txt:1:12
|
1 | {% let s = '\u' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:20:12
|
20 | #[template(path = "char-literals/char-literal-5.txt")]
| ^^^^

error: invalid character
--> testing/templates/char-literals/char-literal-6.txt:1:12
|
1 | {% let s = '\u{}' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:24:12
|
24 | #[template(path = "char-literals/char-literal-6.txt")]
| ^^^^

error: unicode escape must be at most 10FFFF
--> testing/templates/char-literals/char-literal-7.txt:1:12
|
1 | {% let s = '\u{110000}' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:28:12
|
28 | #[template(path = "char-literals/char-literal-7.txt")]
| ^^^^

error: invalid character
--> <source attribute>:1:12
|
1 | {% let s = 'aaa' %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/char_literal.rs:32:12
|
32 | #[template(source = "{% let s = 'aaa' %}", ext = "html")]
Expand Down
21 changes: 3 additions & 18 deletions testing/tests/ui/error_file_path.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
error: failed to parse template source
--> testing/templates/invalid_syntax.html:1:15
|
1 | {% let 12 = 0 }
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/error_file_path.rs:4:12
|
4 | #[template(path = "invalid_syntax.html")]
| ^^^^

error: failed to parse template source
--> testing/templates/invalid_syntax.html:1:15
|
1 | {% let 12 = 0 }
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/error_file_path.rs:8:12
|
8 | #[template(path = "include_invalid_syntax.html")]
| ^^^^

error: failed to parse template source
--> testing/templates/invalid_syntax.html:1:15
|
1 | {% let 12 = 0 }
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/error_file_path.rs:12:12
|
12 | #[template(source = r#"{% extends "include_invalid_syntax.html" %}"#, ext = "txt")]
Expand Down
7 changes: 1 addition & 6 deletions testing/tests/ui/excessive_nesting.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
error: your template code is too deeply nested, or last expression is too complex
--> <source attribute>:14:43
|
14 | {%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/excessive_nesting.rs:5:5
|
5 | source = "
Expand Down
14 changes: 2 additions & 12 deletions testing/tests/ui/extends.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
error: whitespace control is not allowed on `extends`
--> <source attribute>:1:3
|
1 | {%- extends "whatever.html" %}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/extends.rs:5:5
|
5 | source = r#"{%- extends "whatever.html" %}"#,
| ^^^^^^

error: whitespace control is not allowed on `extends`
--> <source attribute>:1:3
|
1 | {% extends "whatever.html" -%}
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/extends.rs:12:5
|
12 | source = r#"{% extends "whatever.html" -%}"#,
Expand Down
7 changes: 1 addition & 6 deletions testing/tests/ui/filter-recursion.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
error: your template code is too deeply nested, or last expression is too complex
--> testing/templates/filter-recursion.html:1:256
|
1 | ...A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A|A|AA|A|A|A||A|A|AA|A...
| ^ close to this token
|
error: the previous template error derives from
--> tests/ui/filter-recursion.rs:4:12
|
4 | #[template(path = "filter-recursion.html")]
Expand Down
Loading

0 comments on commit 821a147

Please sign in to comment.