-
-
Notifications
You must be signed in to change notification settings - Fork 223
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
Suggestion: allow deriving Template
on enums
#666
Comments
So what would |
Indeed, it's unnecessary. |
So basically this is kind of a generic derive that forwards to the trait implementation for the unit variant's nested type? I guess other crates already provide something like that... |
I would lose the single element requirement, and would turn your example #[derive(Template)]
#[template(path = "hello.html")]
enum OrderStatus {
Arrived(<elements...>),
Delivery(<elements...>),
PendingConfirmation { <elements...> },
} into: const _: () = {
impl Template for OrderStatus {
fn render(&self) -> Result<String> {
match self {
Arrived(_) => {
Template_Arrived(<elements...>).render()
},
Delivery(<elements...>) => {
Template_Delivery(<elements...>).render()
},
PendingConfirmation(<elements...>) => {
Template_PendingConfirmation(<elements...>).render()
},
}
}
fn render_into(&self, writer: &mut impl Write + ?Sized) -> Result<()> {
match self {
Arrived(<elements...>) => {
Template_Arrived(<elements...>).render_into(writer)
},
Delivery(<elements...>) => {
Template_Delivery(<elements...>).render_into(writer)
},
PendingConfirmation { <elements...> } => {
Template_PendingConfirmation { <elements...> }.render_into(writer)
},
}
}
}
#[derive(Template)]
#[template(path = "hello.html")]
struct Template_Arrived<'a>(<&'a elements...>);
#[derive(Template)]
#[template(path = "hello.html")]
struct Template_Delivery<'a>(<&'a elements...>);
#[derive(Template)]
struct Template_PendingConfirmation<'a> { <&'a elements...> };
}; Should be easy enough to implement since all the magic can be done by syn, no actual implementation needed. Most likely it would be more useful to put |
I implemented a very simplistic version of my idea, but I don't think there's anymore needed:
This might serve as a better/simpler multiple templates per struct approach than #600. What do y'all think? |
It seems nice but pretty niche. :) It's probably good enough as a solution for this issue? |
Yes. thanks @Kijewski. |
Restrictions: Each enum variant should be tuple like with exactly one variant, wrapped type of each variant should implement
Template
Semantics: While useless for top level pages(??), it may be handy for component templates:
Option
and template level matching)Consider:
Isn't that nice?
Edit: removed pointless annotation
The text was updated successfully, but these errors were encountered: