rust-typed-builder icon indicating copy to clipboard operation
rust-typed-builder copied to clipboard

Mutators as a path among others

Open AlexandreCassagne opened this issue 7 months ago • 1 comments

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?

AlexandreCassagne avatar Nov 18 '23 17:11 AlexandreCassagne