Skip to content

Commit

Permalink
Merge pull request #4995 from epage/parse
Browse files Browse the repository at this point in the history
feat(derive): Better match serde in built-in type support
  • Loading branch information
epage authored Jul 5, 2023
2 parents 7353b2b + 99bcdfc commit 890af8a
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions clap_builder/src/builder/value_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2137,18 +2137,38 @@ impl ValueParserFactory for String {
ValueParser::string() // Default `clap_derive` to optimized implementation
}
}
impl ValueParserFactory for Box<str> {
type Parser = MapValueParser<StringValueParser, fn(String) -> Box<str>>;
fn value_parser() -> Self::Parser {
StringValueParser::new().map(String::into_boxed_str)
}
}
impl ValueParserFactory for std::ffi::OsString {
type Parser = ValueParser;
fn value_parser() -> Self::Parser {
ValueParser::os_string() // Default `clap_derive` to optimized implementation
}
}
impl ValueParserFactory for Box<std::ffi::OsStr> {
type Parser =
MapValueParser<OsStringValueParser, fn(std::ffi::OsString) -> Box<std::ffi::OsStr>>;
fn value_parser() -> Self::Parser {
OsStringValueParser::new().map(std::ffi::OsString::into_boxed_os_str)
}
}
impl ValueParserFactory for std::path::PathBuf {
type Parser = ValueParser;
fn value_parser() -> Self::Parser {
ValueParser::path_buf() // Default `clap_derive` to optimized implementation
}
}
impl ValueParserFactory for Box<std::path::Path> {
type Parser =
MapValueParser<PathBufValueParser, fn(std::path::PathBuf) -> Box<std::path::Path>>;
fn value_parser() -> Self::Parser {
PathBufValueParser::new().map(std::path::PathBuf::into_boxed_path)
}
}
impl ValueParserFactory for bool {
type Parser = ValueParser;
fn value_parser() -> Self::Parser {
Expand Down Expand Up @@ -2203,16 +2223,49 @@ impl ValueParserFactory for i32 {
RangedI64ValueParser::new().range(start..=end)
}
}
impl ValueParserFactory for u64 {
type Parser = RangedU64ValueParser<u64>;
fn value_parser() -> Self::Parser {
RangedU64ValueParser::new()
}
}
impl ValueParserFactory for i64 {
type Parser = RangedI64ValueParser<i64>;
fn value_parser() -> Self::Parser {
RangedI64ValueParser::new()
}
}
impl ValueParserFactory for u64 {
type Parser = RangedU64ValueParser<u64>;
impl<T> ValueParserFactory for std::num::Wrapping<T>
where
T: ValueParserFactory,
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
T: Send + Sync + Clone,
{
type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::num::Wrapping<T>>;
fn value_parser() -> Self::Parser {
RangedU64ValueParser::new()
T::value_parser().map(std::num::Wrapping)
}
}
impl<T> ValueParserFactory for Box<T>
where
T: ValueParserFactory,
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
T: Send + Sync + Clone,
{
type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> Box<T>>;
fn value_parser() -> Self::Parser {
T::value_parser().map(Box::new)
}
}
impl<T> ValueParserFactory for std::sync::Arc<T>
where
T: ValueParserFactory,
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
T: Send + Sync + Clone,
{
type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::sync::Arc<T>>;
fn value_parser() -> Self::Parser {
T::value_parser().map(std::sync::Arc::new)
}
}

Expand Down

0 comments on commit 890af8a

Please sign in to comment.