Skip to content
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

validator_os takes &OsStr, but validator takes String #1165

Closed
oli-obk opened this issue Feb 5, 2018 · 5 comments · Fixed by #1884
Closed

validator_os takes &OsStr, but validator takes String #1165

oli-obk opened this issue Feb 5, 2018 · 5 comments · Fixed by #1884
Labels
A-validators Area: ArgMatches validation logi
Milestone

Comments

@oli-obk
Copy link

oli-obk commented Feb 5, 2018

If find it somewhat inconsistent that one function borrows the value, while the other takes it by value. It is very unlikely that anyone just forwards the argument passed to the Err return value, so there's no benefit of offering users an owned String.

Full disclosure: It clashes with a clippy lint, and I don't know how to act on it: rust-lang/rust-clippy#2434

@kbknapp kbknapp mentioned this issue Feb 5, 2018
87 tasks
@kbknapp
Copy link
Member

kbknapp commented Feb 5, 2018

This is kind of related to #848 and I plan on re-working the signature of these methods in v3 (see #1037).

The reason validator takes String by value is there is (was?) no way to convert from OsStr (which originates from env::args_os) to &str because of the utf-8 validation. But validator_os has no such stipulation and can just use the OsStr directly.

@kbknapp
Copy link
Member

kbknapp commented Feb 5, 2018

Bottom line, this is something I've been wanting to change and put more thought into but couldn't without a breaking change. Hindsight is always 20/20 and I wish I'd done it differently prior 😄

@kbknapp kbknapp added P2: need to have A-validators Area: ArgMatches validation logi labels Feb 5, 2018
@kbknapp kbknapp added this to the v3-alpha1 milestone Feb 5, 2018
@pksunkara pksunkara modified the milestones: v3-alpha.2, v3.0 Feb 1, 2020
@CreepySkeleton
Copy link
Contributor

Having looked at the the code, this is the only place validators play:

clap/src/parse/validator.rs

Lines 138 to 159 in 5b9dbee

if let Some(ref vtor) = arg.validator {
debug!("Validator::validate_arg_values: checking validator...");
if let Err(e) = vtor(val.to_string_lossy().into_owned()) {
debug!("error");
return Err(Error::value_validation(Some(arg), &e, self.p.app.color())?);
} else {
debug!("good");
}
}
if let Some(ref vtor) = arg.validator_os {
debug!("Validator::validate_arg_values: checking validator_os...");
if let Err(e) = vtor(val) {
debug!("error");
return Err(Error::value_validation(
Some(arg),
&(*e).to_string(),
self.p.app.color(),
)?);
} else {
debug!("good");
}
}

Essentially, we could just replace

vtor(val.to_string_lossy().into_owned())

with

vtor(val.to_string_lossy().as_ref())) // what was the correct function?

And change the signature for Validator.

This kind of change won't give us any perf improvement, but it will make the API uniform (both os and str versions will take a reference) and will open road to future improvements. @pksunkara What do you think?

Side note: as of now, if both validator and validator_os are present, str version takes priority (is executed first). I never saw anybody using both, but maybe me should disallow this? Or perhaps run os version only if str has failed?

@kbknapp
Copy link
Member

kbknapp commented Apr 29, 2020

Side note: as of now, if both validator and validator_os are present, str version takes priority (is executed first). I never saw anybody using both, but maybe me should disallow this? Or perhaps run os version only if str has failed?

I don't think it would make sense to have both, as all validator_os can handle anything validator can, but the reverse is not true. So I'm not sure there'd be a reason for a user to have both?

@CreepySkeleton
Copy link
Contributor

None I can think of. I'll make it panic in case both have been passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants