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

Support for streamlined nested-enum declarations? #5

Open
daniel5151 opened this issue Apr 5, 2024 · 3 comments
Open

Support for streamlined nested-enum declarations? #5

daniel5151 opened this issue Apr 5, 2024 · 3 comments

Comments

@daniel5151
Copy link

First off - awesome crate!
As someone who often works with deeply nested C structures and JSON specs, this will come quite in handy in many different places :)


I wanted to float a feature request by you that I think might be a natural extension to nestify.

Consider this example:

nest! {
    enum Foo {
        Bar(struct Bar(usize)),
        Baz(enum Baz { Quz }),
    }
}

While this works, it does require the author to repeat the enum variant names twice (once as part of the variant, and once again as part of the struct), which is somewhat unfortunate.

Would it be possible to have the nest! macro accept a shorthand for this pattern instead? e.g:

nest! {
    enum Foo {
        struct Bar(usize),
        enum Baz { Qux },
    }
}

...precise syntax subject to bikeshedding / tweaks, of course.
Maybe requiring a sigil on the outer enum Foo to opt-in to this infer syntax?
Maybe having a way to add a prefix / postfix to the generated struct name?

Do you think is is something nestify could support?

@snowfoxsh
Copy link
Owner

Hm this is an interesting idea! I will definitely consider implementing it as long as it doesn't degrade other features. I'll look into it over the coming days

@nanocryk
Copy link
Contributor

nanocryk commented Apr 6, 2024

What about something like:

nest! {
    enum Foo {
        _(struct Bar(usize)),
        _(enum Baz { Quz }),
    }
}

I see multiple possible behaviors if normal or nested types are allowed:

  • Use the first/outer-most ident, which allow to easily declare wrapper variants (_(MyOuterType)).
  • However that would conflict with another interesting behavior _(Option<struct Foo { ... }>) which could get variant name Foo (instead of Option).
  • Maybe the 2 can be combined as follow: if there is a special type defined, use its ident (if multiple use the first), other use the ident of the first type.

@snowfoxsh
Copy link
Owner

I appreciate the idea behind the macro, but I'm concerned it might complicate comprehension for those not already familiar with it. Nestify allows for defining structures in place of types.

I believe using explicit syntax, as shown in the original proposal by @daniel5151, is clearer and immediately understandable since It eliminates ambiguity about the structure's design. However, I'm not fully convinced that deviating from whats more inline official rust syntax is a would be beneficial. Its for sure a balance between being immediately understandable and easy to write.

nest! {
    enum Foo {
        struct Bar(usize),
        enum Baz { Qux },
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants