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

Mutators as a path among others #130

Open
AlexandreCassagne opened this issue Nov 18, 2023 · 1 comment
Open

Mutators as a path among others #130

AlexandreCassagne opened this issue Nov 18, 2023 · 1 comment

Comments

@AlexandreCassagne
Copy link

I have an example use case that either does not work currently, or I am deeply misunderstanding the documentation ;-)

#[derive(Debug, TypedBuilder)]
struct CalendarEvent<Tz: chrono::offset::TimeZone> {
    #[builder(setter(into))]
    title: String,

    // Goal: User should be able to set start(...), then may EITHER:
    //   - set end(...)
    //   - call with_duration(chrono::Duration) which would automatically add duration to start and set end.
    #[builder(
      mutators(
        fn with_duration(&mut self, duration: chrono::Duration) {
            self.end = self.start.clone().checked_add_signed(duration).unwrap();
        }
      )
    )]
    start: chrono::DateTime<Tz>,
    #[builder(???)]
    end: chrono::DateTime<Tz>,
}

The "goal" comment describes the use case, i.e. two different paths to the completed builder.

Of course, there may be multiple other paths.

I have tried several combinations but none work. Is it because I am using generics? Or am I trying to do something that is currently unsupported?

@idanarye
Copy link
Owner

idanarye commented Nov 19, 2023

Mutators change the value of the builder payload, but they cannot change its type. Having mutators that can change the type is a more complex feature (which would also require more complex syntax) that is not currently implemented.

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

2 participants