-
Notifications
You must be signed in to change notification settings - Fork 9
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: allow supplying source in comment #127
Conversation
I like the idea! Don't forget to test with |
c19e0fa
to
e801a93
Compare
Oof, yeah, my code did not work at all with these two. :) Fixed. |
Does it work with code examples like: /// let x = 12;
///
/// `````
/// let x = 12;
/// ````` ? If not, I'd recommend using |
Oh, I didn't think about this case. Implemented. |
It's just one of many. Not sure if your code would handle: /// `````
/// ```rinja
/// {{bla}}
/// ```
/// ````` correctly |
|
I use pulldown-cmark, now. It has a different opinion how to interpret ↓, but I guess it knows it better. :)
|
Should we wait until pulldown-cmark/pulldown-cmark#916 is released to keep our MSRV? |
Oh that'd be convenient. Another thing I thought about: should we make it an optional feature? |
Yes, good call! It will probably stay a very niche feature. What would you call the feature flag? Is "code-in-doc" too long? |
|
695bd53
to
3f43e43
Compare
61ae0ee
to
d984cd5
Compare
fn collect_comment_blocks(ast: &syn::DeriveInput) -> Option<(Option<Span>, String)> { | ||
let mut span: Option<Span> = None; | ||
let mut assign_span = |kv: &syn::MetaNameValue| { | ||
// FIXME: uncomment once <https://github.com/rust-lang/rust/issues/54725> is stable |
Check notice
Code scanning / devskim
A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note
Oh btw I asked on the |
New |
Well, let's not. If the user does not need the feature, then 1.17.0 is good enough. |
Hm, should |
f41c0b0
to
90d5a79
Compare
I'd say yes. |
book/src/creating_templates.md
Outdated
As an alternative to supplying the code template code in an external file (as `path` argument), | ||
or as a string (as `source` argument), you can also enable the `"code-in-doc"` feature. | ||
With this feature, you can specify the template code directly in the documentation | ||
of the template `struct`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of the template `struct`. | |
of the template item. |
Because I think we support union
or enum
(or both). Never remember which.
I already, pro-actively made this case succeed. :) |
book/src/creating_templates.md
Outdated
of the template `struct`. | ||
|
||
Instead of `path = "…"` or `source = "…"`, specify `in_doc = true` in the `#[template]` attribute, | ||
and in the struct's documentation add a ```` ```rinja ```` code block: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and in the struct's documentation add a ```` ```rinja ```` code block: | |
and in the item's documentation, add a code block with the `rinja` attribute: |
rinja_actix/Cargo.toml
Outdated
@@ -1,6 +1,6 @@ | |||
[package] | |||
name = "rinja_actix" | |||
version = "0.3.0" | |||
version = "0.3.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A reason why you're updating versions? Shouldn't it be done in a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I added a new feature, but it could be done in another PR, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not the first one. Please do it in another PR then. Like that we make publish it too and also ensure we don't have any other features we want to be merged before making this release.
rinja_derive/src/input.rs
Outdated
CompileError::new("template `path` or `source` not found in attributes", None) | ||
CompileError::new( | ||
#[cfg(not(feature = "code-in-doc"))] | ||
"specify one template argument `path` OR `source`", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe let's not put OR
in capital? XD
rinja_derive/src/input.rs
Outdated
@@ -422,6 +433,177 @@ impl TemplateArgs { | |||
} | |||
} | |||
|
|||
/// Try to find the souce in the comment, in a "```rinja```" block |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Try to find the souce in the comment, in a "```rinja```" block | |
/// Try to find the souce in the comment, in a `rinja` code block. |
let kind = match &ast.data { | ||
syn::Data::Struct(_) => "struct", | ||
syn::Data::Enum(_) => "enum", | ||
syn::Data::Union(_) => "union", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we do support all item types! 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hehe, I simply added cases for all variant of the enum. I actually don't think that we generate valid code for unions, because I think reading from unions is only allowed in an unsafe context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny. Might worth handling unions correctly at some point.
rinja_derive/src/lib.rs
Outdated
/// of the template `struct`. | ||
/// | ||
/// Instead of `path = "…"` or `source = "…"`, specify `in_doc = true` in the `#[template]` | ||
/// attribute, and in the struct's documentation add a ```` ```rinja ```` code block: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// attribute, and in the struct's documentation add a ```` ```rinja ```` code block: | |
/// attribute, and in the struct's documentation add a `rinja` code block: |
All suggestions applied. |
Thanks a lot, it's a very cool feature! :) |
Thanks! And thank you for your reviews and suggestions! :) |
Using
#[template(source = "…")]
is tiresome, because you have to escape the input. That makes it difficult to read, too. Using#[template(path = "…")
can be tiresome, too, if the code is small and/or if you just want to prototype something.This PR adds a third option to supply the source of a template: You can supply the source in the comments of a template. This works similar to doctest. Any
block gets extracted, and the combined blocks are the source code of the template.rinja