forked from clap-rs/clap
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(parser): Track str/OsStr values via Box
Unfortunately, we can't track using a `ValueParser` inside of `Command` because its `PartialEq`. We'll have to wait until a breaking change to relax that. Compatibility: - We now assert on using the wrong method to look up defaults. This shouldn't be a breaking change as it'll assert when getting a real value. - `values_of`, et al delay panicing on the wrong lookup until the iterator is being processed.
- Loading branch information
Showing
11 changed files
with
352 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::parser::AnyValue; | ||
|
||
/// Parse/validate argument values | ||
#[derive(Clone)] | ||
pub struct ValueParser(pub(crate) ValueParserInner); | ||
|
||
#[derive(Clone)] | ||
pub(crate) enum ValueParserInner { | ||
String, | ||
OsString, | ||
} | ||
|
||
impl ValueParser { | ||
/// `String` parser for argument values | ||
pub const fn string() -> Self { | ||
Self(ValueParserInner::String) | ||
} | ||
|
||
/// `OsString` parser for argument values | ||
pub const fn os_string() -> Self { | ||
Self(ValueParserInner::OsString) | ||
} | ||
} | ||
|
||
impl ValueParser { | ||
/// Parse into a `Arc<Any>` | ||
/// | ||
/// When `arg` is `None`, an external subcommand value is being parsed. | ||
pub fn parse_ref( | ||
&self, | ||
cmd: &crate::Command, | ||
_arg: Option<&crate::Arg>, | ||
value: &std::ffi::OsStr, | ||
) -> Result<AnyValue, crate::Error> { | ||
match &self.0 { | ||
ValueParserInner::String => { | ||
let value = value.to_str().ok_or_else(|| { | ||
crate::Error::invalid_utf8( | ||
cmd, | ||
crate::output::Usage::new(cmd).create_usage_with_title(&[]), | ||
) | ||
})?; | ||
Ok(Arc::new(value.to_owned())) | ||
} | ||
ValueParserInner::OsString => Ok(Arc::new(value.to_owned())), | ||
} | ||
} | ||
} | ||
|
||
impl<'help> std::fmt::Debug for ValueParser { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { | ||
match &self.0 { | ||
ValueParserInner::String => f.debug_struct("ValueParser::string").finish(), | ||
ValueParserInner::OsString => f.debug_struct("ValueParser::os_string").finish(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.