Skip to content
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 Content on enums #40

Open
ibutra opened this issue Oct 15, 2020 · 0 comments
Open

derive Content on enums #40

ibutra opened this issue Oct 15, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@ibutra
Copy link

ibutra commented Oct 15, 2020

Hi,

currently Enums are not supported by the derive macro.
I would therefore like to propose a way to interpret enums in ramhorns.
If this finds a consensus I am willing to try to implement this.

General Enum Handling

I think to access enum items Mustache sections are fitting as they include the notion of false values or emptiness:

enum Status {
    Available,
    OutOfStock,
}

Would get accessed like this:

The Item is currently: 
{{#Available}}
available
{{/Available}}
{{#OutOfStock}}
out of stock
{{/OutOfStock}}

This leaves us with how to handle different types of enums:

Enum Types:

Following enum types are currently present in rust:

  1. Fieldless Enumerations. I.e. No item has data attached, this is much like a c enum and can be interpreted as integer type
  2. Enums with attached data.
    2.1 An Item can have a Struct like form
    2.2 An Item can have a Tuple like form
    2.3 An Item can have no data attached

Fieldless Enumerations

I think these are pretty easy to handle as shown in the example above. The question is how to handle the numerical value of these. We could use the same syntax as for tuple like enums as discussed below.

Enums with attached data

If any item of an enum has attached data the whole enum can't be interpreted as integer anymore according to the Rust reference. We have to therefore define how the different Item cases are handled. I assume that all attached data types implement the Content trait.

Struct like

The easiest is probably the struct like form, this I believe can just be handled like normal structs. We open a section and access the fields by name in the section.

Tuple like

Handling the tuple like form is more complicated as we don't have an identifier for the fields. We could work around this with numbers as identifiers where the number n identifies the nth Item like so:

enum Quantity {
    Numerical(i32),
    WithUnit(i32, String),
}

We could access these like this:

We currently have 
{{#Numerical}}
    {{1}}
{{/Numerical}}
{{#WithUnit}}
    {{1}} {{2}}
{{/WithUnit}}
in stock.

No data attached

Since there is no data attached the section would just check whether it is truthy or not. No data can be accessed as there is none present.

Summary

I believe this would give much more flexibility to ramhorns while keeping mostly true to the mustache templates ideas. And prevent the need to unwrap all enum variants into their own Option just to be able to use them with ramhorns.

I am looking forward to your thoughts and feedback!

@maciejhirsz maciejhirsz added the enhancement New feature or request label Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants