lambda-fairy / maud

:pencil: Compile-time HTML templates for Rust
https://maud.lambda.xyz
Apache License 2.0
1.98k stars 132 forks source link

Typed tags #378

Closed imbolc closed 11 months ago

imbolc commented 1 year ago

It would be great to check tags attributes on compile time. Maybe something like HtmlTag trait one could derive to describe types:

#[derive(HtmlTag)]
struct Form {
    action: Option<String>,
    method: Option<Method>,
}

#[derive(FromStr)]
enum Method {
    Get,
    Post,
}

and then use it like

@Form
   method="PUT"  <-- bad value use one of [GET, POST]
lambda-fairy commented 1 year ago

Have you seen this?

https://github.com/yoshuawuyts/html

In general I'm against validation (except the bare minimum needed for security – see #181). Because it's a lot of work, and also I don't think Maud can do it better than the html crate.

imbolc commented 1 year ago

I'm not suggesting that Maud should implement the entire HTML specification. I don't even think it would be enough, considering enhancements like Htmx. However, I would like Maud to provide support for typed tags (e.g., through a derivable trait). Typically, projects use a small subset of tags and their attributes, which isn't so hard to type. This approach also enables people to share typing efforts by creating specialized crates, e.g. maud-htmx.

lambda-fairy commented 11 months ago

Typically, projects use a small subset of tags and their attributes, which isn't so hard to type.

Then we're offloading the validation effort to library users. That doesn't seem like an improvement over yoshuawuyts/html.

In general I feel this suggestion is a big change, with a lot of design decisions, and really different to where the library is now. If you'd like to explore that space then I suggest rolling your own library.