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 returns Result<(), OsString> #848

Closed
ssokolow opened this issue Feb 11, 2017 · 2 comments
Closed

validator_os returns Result<(), OsString> #848

ssokolow opened this issue Feb 11, 2017 · 2 comments
Labels
A-validators Area: ArgMatches validation logi
Milestone

Comments

@ssokolow
Copy link

Affected Version of clap

2.20.3 and anything API-compatible with it.

Expected Behavior Summary

Callbacks for validator_os should return Result<T, String> for some T because:

  • Error messages are intended for display
  • It's consistent with callbacks for validator in a context where the usage doesn't differ
  • It's easy to incorporate OsStr/OsString into format! output via the Debug trait.
  • There's no facility for formatting natively with OsStr/OsString
  • There's apparently no good reason to use Result<T, OsString>

Actual Behavior Summary

  • I have to stick .into() onto the end of every Err case that I return from a path-handling validator.
  • I get questions and then "eww" reactions when people not familiar with clap encounter Result<(), OsString>.

Sample Code or Link to Sample Code

pub fn filename_valid(value: &OsStr) -> Result<(), OsString> {
    // TODO: Switch to using to_bytes() once it's stabilized
    if value.to_string_lossy().chars().any(|c| is_bad_for_fname(&c)) {
        Err(format!("Name contains invalid characters: {:?}", value).into())
    } else {
        Ok(())
    }
}

I think this should be reconsidered for clap 3.x

@kbknapp
Copy link
Member

kbknapp commented Feb 12, 2017

Hmm, I see your point about errors being designed for display anyways. I'm inclined to agree it should be changed to Result<(), String> in 3.x. I'd be curious if anyone has a use case where returning a String wouldn't be acceptable?

I'll tentatively mark this for change in 3.x, but am open to discussion.

For now, .into() will have to do 😞

@kbknapp kbknapp added A-validators Area: ArgMatches validation logi D: easy labels Feb 12, 2017
@kbknapp kbknapp added this to the 3.0 milestone Feb 12, 2017
little-dude added a commit to little-dude/clap-rs that referenced this issue Jun 8, 2017
@kbknapp kbknapp modified the milestones: 3.0, v3-alpha1 Feb 2, 2018
little-dude added a commit to little-dude/clap-rs that referenced this issue Apr 21, 2018
little-dude added a commit to little-dude/clap-rs that referenced this issue Apr 21, 2018
little-dude added a commit to little-dude/clap-rs that referenced this issue Apr 21, 2018
@kbknapp
Copy link
Member

kbknapp commented Jul 22, 2018

This has been implemented on v3-master

@kbknapp kbknapp closed this as completed Jul 22, 2018
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.

2 participants