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

Validate each field individually

Open Zizico2 opened this issue 2 years ago • 11 comments

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();.

Zizico2 avatar Jul 21 '23 10:07 Zizico2

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...

idanarye avatar Jul 22 '23 01:07 idanarye

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?

Zizico2 avatar Jul 24 '23 12:07 Zizico2

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

idanarye avatar Jul 24 '23 14:07 idanarye

Maybe you could use newtype instead

l-7-l avatar Aug 23 '24 09:08 l-7-l