yoshuawuyts / html

Type-safe HTML support for Rust
Apache License 2.0
246 stars 7 forks source link

Enums for attributes #65

Open tolyo opened 1 year ago

tolyo commented 1 year ago

Looking at the global attributes spec, we have only three types: String, Integer, and Bool. For the String type, what would be the strategy to support enumerated attributes?

yoshuawuyts commented 1 year ago

Thanks for filing this issue; I'd love to see this! The right direction for enumerated attributes would probably be to create an enum per attribute derived from the spec, and then provide that next to the element.

The right first step would probably be to start scraping and normalizing the attributes so we can update the JSON. And then in a follow-up PR work on introducing the codegen.

tolyo commented 1 year ago

Awesome. So assuming we take dir element for example, what should be our format? I am assuming enums are always strings (???) and they can be hypen-separated (???). Need to double check but we can go with:

{
        "name": "dir",
        "description": "Indicates the directionality of the element's text with enumerated type",
        "field_name": "direction",
        "ty": "Enum{ltr, rtl, auto}"
 }

or

...
        "ty": "Enum",
        "values": ["ltr", "rtl", "auto"]
yoshuawuyts commented 1 year ago

I'm not sure how to parse enums either, so you'll have to look into that. But starting with the assumption that they're always strings is probably a safe one?

In terms of formats: the second one would be preferable over the first, since it seems easier to generate/parse using serde.