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

Ensuring that at least one of a set of fields are set #6

Open
insanitybit opened this issue Nov 9, 2018 · 5 comments
Open

Ensuring that at least one of a set of fields are set #6

insanitybit opened this issue Nov 9, 2018 · 5 comments

Comments

@insanitybit
Copy link

It would be great if I could say something along the lines of:

#[derive(TypedBuilder)]
struct Foo {
    #[group(a)]
    #[default]
    bar: Option<String>,
    #[group(a)]
    #[default]
    baz: Option<String>,
    #[default]
    bat: Option<String>,
}

Made up the syntax, but basically some way to signal "one or both of bar/baz must be provided".

@idanarye
Copy link
Owner

I don't think the Rust type system is flexible enough to allow this...

@idanarye
Copy link
Owner

Actually... I was thinking about using marker traits, which are not stable yet, but instead I can use empty fields. Just set a field for each group and override its type when any member of the group is assigned.

@chris-morgan
Copy link
Contributor

I recommend stealing from #[cfg], for both optimal flexibility and familiarity.

Something like this, on the struct (not on the field any more):

#[builder(group = "a", required, any("bar", all("baz", not("quux"))))]

This would define a named group “a” (the name is for documentation, and may also be used in other places that accept a field or group, such as field dependencies which I’m about to create an issue about). In this case the group is also then declared to be required before build() can be called.

@nCrazed
Copy link

nCrazed commented Apr 24, 2024

Recently started using this crate and quickly ran into a scenario that required something like this.

Is anyone working on this? If not, is anyone able and willing to provide guidance on getting this implemented?

@mo8it
Copy link
Contributor

mo8it commented Apr 24, 2024

See #14 and my algorithm https://codeberg.org/mo8it/sus-impls

The macro has to use the algorithm to generate implementations

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

5 participants