idanarye / rust-typed-builder

Compile-time type-checked builder derive
https://crates.io/crates/typed-builder
Apache License 2.0
904 stars 52 forks source link

Validate each field individually #105

Open Zizico2 opened 1 year ago

Zizico2 commented 1 year ago

Is it possible (would it be possible?) to have each field validated when it is added?

It could look something like Foo::builder().x(1).try_into().y(2).try_into().z(3).try_into().build();.

idanarye commented 1 year ago

What does try_into have to do with anything? Shouldn't individual field validation mean that a setter returns a Result?

Either way, unlike #67 this is not something we can solve with a trait impl because we'd need one for each field. Maybe when const macros would support &str...

Zizico2 commented 1 year ago

Yeah, idk why I thought of try_into.

This would make most sense I guess? Foo::builder().x(1)?.y(2)?.z(3)?.build();.

Idk much about how typed_builder works. Do you think I couldn't find a way to make this work, that you would accept?

idanarye commented 1 year ago

It can be done, but we need to come up with a good syntax.

l-7-l commented 2 weeks ago

Maybe you could use newtype instead