Skip to content

Add support for struct expressions#602

Merged
GuillaumeGomez merged 5 commits intoaskama-rs:mainfrom
VojtaStanek:struct-expr
Oct 19, 2025
Merged

Add support for struct expressions#602
GuillaumeGomez merged 5 commits intoaskama-rs:mainfrom
VojtaStanek:struct-expr

Conversation

@VojtaStanek
Copy link
Copy Markdown
Contributor

I have found some time to do it, so here it is.

fix #597

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

This is a solid start, thanks! Please add ui tests in case the syntax is wrong too.

And one question: should we add support for tuple structs with the () syntax too?

@VojtaStanek
Copy link
Copy Markdown
Contributor Author

And one question: should we add support for tuple structs with the () syntax too?

I thought that it is supported today as a "function call" since the syntax is not any different, but maybe I am missing something

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

... It's so obvious I wonder how I missed it. ^^' So yeah, just missing tests and docs.

@VojtaStanek
Copy link
Copy Markdown
Contributor Author

I have added UI tests, but I don't know if my choice of test cases is correct.

I have also added a short mention to the book.

@VojtaStanek
Copy link
Copy Markdown
Contributor Author

CI failure is on nightly due to rust-lang/rust#138907

@@ -0,0 +1,63 @@
error: expected closing `}` of struct expression
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That proves that we need to improve parsing error. Like if ..expr is followed by another field, emit an error about that directly.

18 | source = r#"{{ InnerCell { ..Default::default(), other: 24 } }}"#,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: failed to parse template source
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, not sure if this case is useful.

24 | #[template(source = r#"some{{ InnerCell { }}thing"#, ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected closing `}` of struct expression
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

28 | #[template(source = r#"some{{ InnerCell { foo }}thing"#, ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected closing `}` of struct expression
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error here should be that a value is missing after the field name.

32 | #[template(source = r#"some{{ InnerCell { foo: }}thing"#, ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: failed to parse template source
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not useful either.

36 | #[template(source = r#"some{{ InnerCell { foo: 32 }}thing"#, ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: failed to parse template source
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Although, there is no "working" test which checks if trailing comma is supported, please add one.

40 | #[template(source = r#"some{{ InnerCell { foo: 32, }}thing"#, ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected closing `}` of struct expression
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message should be improved here: it should say that something is expected after ...

44 | #[template(source = r#"some{{ InnerCell { foo: 32, .. }}thing"#, ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: failed to parse template source
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not useful.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Code looks good, docs look good. Errors need improvements (thanks the tests to show us why and how hehe) and tests also need some improvements. The idea here is trying to ensure that the struct syntax is working and to check what happens in weird user input like:

  • Struct { . }
  • Struct { , }
  • Struct { .. : }

Also need to check trailing commas like:

  • Struct { a: 12, }
  • Struct { ..Default::default(), }

@VojtaStanek
Copy link
Copy Markdown
Contributor Author

Thanks for the comments.

I probably won't find time to move this forward in next two week, someday after that probably yes but I don't know when... If anyone wants to pick this up and finish it feel free to.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Then I'll if you don't mind as I think it's a really nice addition. :)

Comment thread askama_derive/src/generator/expr.rs Outdated
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

I'm much happier with the error messages now. Also reverted the .. base to expression.

What do you think @Kijewski ?

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

(fixed the clippy warning too)

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Gonna merge. When @Kijewski has time and motivation to take a look, I'm sure a PR will follow. 😉

@GuillaumeGomez GuillaumeGomez merged commit d220cd5 into askama-rs:main Oct 19, 2025
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support struct expression in expressions

3 participants