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

feat(derive): add the non_empty attribute to children(...) #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TheLostLambda
Copy link

Hi @tailhook !

This is a slightly bigger change than my other PR, but fundamentally just enables me to add a non_empty tag to #[knuffel(children(...))] attributes, guaranteeing that at least one child is present. At the moment, Knuffel is happy parsing nodes with no children and simply returning an empty vector, but that's different from the behavior of #[knuffel(child)] which (sensibly) errors when no child is present. You can use child to parse exactly one child, you can use children for zero-or-more, but there is currently no way to parse one-or-more. This PR effectively adds that ability.

I've not written anything up in the documentation just yet (I wanted to be sure you were happy with this feature as is, before writing up anything), but here is a basic example...

Attempting to parse this KDL (with all of the children commented out):

  C "Carbon" {
    // isotope 12 12.0000000 0.9893
    // isotope 13 13.00335483507 0.0107
    // isotope 14 14.0032419884
  }

Using this struct:

#[derive(Decode, Debug)]
struct ElementKdl {
    #[knuffel(node_name)]
    symbol: ElementSymbol,
    #[knuffel(argument)]
    name: String,
    #[knuffel(children(name = "isotope", non_empty))]
    isotopes: Vec<IsotopeKdl>,
}

Now returns an error where it didn't before:
image

Let me know what you think! It's my first time touching proc_macros, so I'm sure there is plenty of learning for me to do!

Hope you're well,
Brooks

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

Successfully merging this pull request may close these issues.

1 participant