-
Notifications
You must be signed in to change notification settings - Fork 6
support required
attribute on non-optional fields
#171
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
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Jean Mertz <[email protected]>
pub version: Version, The only way to make this "required" is to wrap it. #[setting(required)]
pub version: Option<Version>, But this is still kind of gross because it still has a major drawback: you must unwrap it to use the value! I'm not a fan of this, so I'm attempting a different approach. Newtype wrappers that implement the necessary traits. For example, the new With this, we can now simply do the following, and completely avoid pub version: VersionSetting, If you want something to be truly required, you should use validation instead. #[setting(validate = is_not_empty)]
pub value: String, Because otherwise, how can you properly determine that a field is required (doesn't have a missing/empty value)? Based on your changes, I don't think this will actually work the way you want it to, because required checks should ideally happen at runtime, not macro compile time. |
I understand the original purpose, and mentioned the ugliness of
The implementation does two things:
Essentially, this means we've automated/codified your That is of course, unless I missed something in my implementation that makes it not work the way I expect it to, but I've added a few tests in my own CLI using this branch since then, and I haven't found any drawbacks/bugs so far. The other major change that had to be done, is that |
Yeah you're right. I was misremembering how it was implemented. However, I'm curious why you went with that approach and not the validate approach that the current required implementation uses? https://github.com/moonrepo/schematic/blob/master/crates/macros/src/config/field.rs#L171 I'm also hesitant to accept this because 1) it's a breaking change and I try not to do those often, and 2) I personally use the |
Well, I believe the code you linked is technically broken, because the
Correct. I can't think of a way to do this without introducing the breaking change. I still think it's highly desireable to allow modelling configuration structs in the exact way they are intended to be used for a given situation (e.g. optionals, required, and fields that do not have sensible default values). Also, to be clear, |
Good point. This is definitely an oversight on my part. The features were added only recently.
While this makes sense, IMO, it would be confusing for users when the |
You could look into running cargo-hack on the CI to see if all feature combinations compile as expected. That would capture issues like these.
I guess we could panic in the proc macro if |
This is a rough draft, that is probably incomplete, but does compile on my end (I haven't tested it at runtime yet). I wanted to see how invasive it was to support
required
non-optional fields.Turns out, somewhat invasive, but doable, specifically for the fallout of making
from_partial
fallible.This isn't ready to merge, so feel free to close if you have no desire to support this. But if you do, I'll continue working on this until it works for my use-case, after which I can add tests to validate correctness.
See also: #99 (comment)