-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[modular] User-driven validation #3008
Comments
I'm not quite understanding what the |
My general expectation for traits in this issue and related ones (e.g. #2683) is they would live in All of this would start out behind an This is all contingent on the design working out. We might find it just isn't worth completing this. |
Wait, why would The trait could just fit into Then you could call |
The proposed validator trait would be called during parse so we have the context of the specific subcommand being processed. |
Forgive me for not knowing the internals as well as you, but couldn't the Validator trait, instead of being called on Perhaps as |
It needs access to the relevant
|
I see. Honestly, as much as I don't like it, perhaps you do have a strong case for pushing |
The primary motivation behind this is to see if we can make the cost of validation paid by those who use it rather than everyone (ie clap's existing validation logic like conflicts, special require rules, etc). A secondary motivation is to simplify the APIs for the more complex validation APIs in clap today. All other validation use cases would just be a bonus since we do have solutions like |
Hopefully you can avoid my horrible situation of the past week: adding an API (MFEK/glifparser.rlib@6aaf880), realizing it's broken and trying to fix it (MFEK/glifparser.rlib@d9aa702)…twice (MFEK/glifparser.rlib@9b06a94). Maybe it has no edge cases now, we shall see. Popular software can't afford such…unprofessionalism, I'm aware. :) |
This would most likely be implemented via
|
Although I once thought that it's likely we'd be able to go back to master `clap`, MFEK might have to maintain its fork indefinitely. `clap`'s maintainer @epage unfortunately wrote in clap-rs/clap#3008: > We might find it just isn't worth completing this.
What about relying on |
I worry |
Not necessarily. With serde_value, you get a I think @kraktus' idea is a good middle ground. |
I'm not seeing how that addressed the concern I raised. I'm assuming there is context missing that I could make guesses at but I would prefer to understand what you are getting at then understanding my assumptions :) The other problem with serde is it assumes Paths are UTF-8 so naive handling of paths will be done incorrectly while clap is wanting to ensure paths are default handled as a bag of bytes rather than utf-8 |
I was thinking that you'd have |
How does users writing custom validation fit into that picture? If the user just wants to do a field-level validator, they can use the field attributes to provide their own custom deserializer. If they want to validate the entire struct, which is what this issue is focusing on, then they would need to hand write the entire Compare that to just passing in a value that implements That said, it would be interesting for people to support serde support for |
My problem was I was only considering my patch #3029 and not that you wanted to make every case work. To refresh memories, my patch was focused on validating a single struct field that contains multiple values. So I could use a Newtype and But you're right this won't let you validate the whole struct. |
Erm, or I could Then when I call I hadn't considered the other cases, my bad, I forgot this issue was not just about #3029 |
Where is work being done on this? Is there more discussion that must be had first? |
For myself, I have tasks on other projects that I feel are higher priority on this. Someone else could do some exploratory work on this but the challenge is this is very open-ended work and someone working on it would need to keep us in the loop on decisions to make sure there is buy-in on the direction being taken. |
It seems like what this is proposing is a validator trait on functions which can take a validator to make new validator. Then use Combinatory_logic to define all the validators. How is this proposed approach different from copying bpaf? |
In Disclaimer, I'm not an expert in
Values later extracted into variables https://github.com/rust-lang/cargo/blob/5b377cece0e0dd0af686cf53ce4637d5d85c2a10/src/bin/cargo/commands/add.rs#L226 And consumed with https://github.com/rust-lang/cargo/blob/5b377cece0e0dd0af686cf53ce4637d5d85c2a10/src/bin/cargo/commands/add.rs#L259 Biggest problem with that is that user code ( With bpaf you would have those values constructed either by derive or struct Options {
...
source: Input,
...
}
enum Ref {
Branch { branch: String },
Tag { tag: String },
Rev { rev: String },
}
enum Input {
Crate { name: String },
File { path: PathBuf },
Git { uri: String, ref: Option<Ref> },
} With parsers and validators operating on strictly typed Rust values. For hypothetical input validator (let's say against crate blacklist) Now say you want to add one extra input type, let's call it "interactive picker". With clap you modify parser definition and then you have to go over all the places where With This becomes a problem when argument parser or parts of it are shared across multiple binaries, but even within a single crate it is easier to work with the right type enum OutputFormat {
Intel,
Att,
Llvm,
LlvmInput,
Mir,
Wasm,
McaIntel,
McaAtt,
} rather than trying to emulate it using product types: struct OutputFormat {
intel: bool,
att: bool,
llvm: bool,
...
} |
Please complete the following tasks
Clap Version
master
Describe your use case
Describe the solution you'd like
Define one or more App Validation trait
App
andArgMatches
as an input, reports errorsApp
,ArgMatches
, and args as input, reports which args are requiredApp
,ArgMatches
, and args as input, reports default values for themWith maybe a
hint(&self, app: &App) -> Option<String>
(see #1695 and #3321)matches: Option<&ArgMatches>
for smart hints?Steps
default_value_if
API with its use ofOption
by clarifying intent through buildersclap::validate
Arg
functions so now only those using this API pay the cost due to dead code eliminationAlternatives, if applicable
No response
Additional Context
See also #2832
Related issues
required_unless_eq_any
andrequired_unless_eq_all
forArg
andArgGroup
#4682Risks are usability and performance. The most basic APIs, like
required
, we probably need to keep baked in. Its the special inter-arg interactions that we should be focusing on.The text was updated successfully, but these errors were encountered: