1use std::convert::TryInto;
2use std::ops::RangeBounds;
3
4use crate::builder::Str;
5use crate::builder::StyledStr;
6use crate::parser::ValueSource;
7use crate::util::AnyValue;
8use crate::util::AnyValueId;
9
10pub struct ValueParser(ValueParserInner);
64
65enum ValueParserInner {
66 Bool,
68 String,
70 OsString,
72 PathBuf,
74 Other(Box<dyn AnyValueParser>),
75}
76
77impl ValueParser {
78 pub fn new<P>(other: P) -> Self
113 where
114 P: TypedValueParser,
115 {
116 Self(ValueParserInner::Other(Box::new(other)))
117 }
118
119 pub const fn bool() -> Self {
144 Self(ValueParserInner::Bool)
145 }
146
147 pub const fn string() -> Self {
169 Self(ValueParserInner::String)
170 }
171
172 pub const fn os_string() -> Self {
201 Self(ValueParserInner::OsString)
202 }
203
204 pub const fn path_buf() -> Self {
227 Self(ValueParserInner::PathBuf)
228 }
229}
230
231impl ValueParser {
232 pub(crate) fn parse_ref(
236 &self,
237 cmd: &crate::Command,
238 arg: Option<&crate::Arg>,
239 value: &std::ffi::OsStr,
240 source: ValueSource,
241 ) -> Result<AnyValue, crate::Error> {
242 self.any_value_parser().parse_ref_(cmd, arg, value, source)
243 }
244
245 pub fn type_id(&self) -> AnyValueId {
247 self.any_value_parser().type_id()
248 }
249
250 pub fn possible_values(
255 &self,
256 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
257 self.any_value_parser().possible_values()
258 }
259
260 fn any_value_parser(&self) -> &dyn AnyValueParser {
261 match &self.0 {
262 ValueParserInner::Bool => &BoolValueParser {},
263 ValueParserInner::String => &StringValueParser {},
264 ValueParserInner::OsString => &OsStringValueParser {},
265 ValueParserInner::PathBuf => &PathBufValueParser {},
266 ValueParserInner::Other(o) => o.as_ref(),
267 }
268 }
269}
270
271impl<P> From<P> for ValueParser
295where
296 P: TypedValueParser + Send + Sync + 'static,
297{
298 fn from(p: P) -> Self {
299 Self::new(p)
300 }
301}
302
303impl From<_AnonymousValueParser> for ValueParser {
304 fn from(p: _AnonymousValueParser) -> Self {
305 p.0
306 }
307}
308
309impl From<std::ops::Range<i64>> for ValueParser {
334 fn from(value: std::ops::Range<i64>) -> Self {
335 let inner = RangedI64ValueParser::<i64>::new().range(value.start..value.end);
336 Self::from(inner)
337 }
338}
339
340impl From<std::ops::RangeInclusive<i64>> for ValueParser {
365 fn from(value: std::ops::RangeInclusive<i64>) -> Self {
366 let inner = RangedI64ValueParser::<i64>::new().range(value.start()..=value.end());
367 Self::from(inner)
368 }
369}
370
371impl From<std::ops::RangeFrom<i64>> for ValueParser {
396 fn from(value: std::ops::RangeFrom<i64>) -> Self {
397 let inner = RangedI64ValueParser::<i64>::new().range(value.start..);
398 Self::from(inner)
399 }
400}
401
402impl From<std::ops::RangeTo<i64>> for ValueParser {
427 fn from(value: std::ops::RangeTo<i64>) -> Self {
428 let inner = RangedI64ValueParser::<i64>::new().range(..value.end);
429 Self::from(inner)
430 }
431}
432
433impl From<std::ops::RangeToInclusive<i64>> for ValueParser {
458 fn from(value: std::ops::RangeToInclusive<i64>) -> Self {
459 let inner = RangedI64ValueParser::<i64>::new().range(..=value.end);
460 Self::from(inner)
461 }
462}
463
464impl From<std::ops::RangeFull> for ValueParser {
489 fn from(value: std::ops::RangeFull) -> Self {
490 let inner = RangedI64ValueParser::<i64>::new().range(value);
491 Self::from(inner)
492 }
493}
494
495impl<P, const C: usize> From<[P; C]> for ValueParser
521where
522 P: Into<super::PossibleValue>,
523{
524 fn from(values: [P; C]) -> Self {
525 let inner = PossibleValuesParser::from(values);
526 Self::from(inner)
527 }
528}
529
530impl<P> From<Vec<P>> for ValueParser
557where
558 P: Into<super::PossibleValue>,
559{
560 fn from(values: Vec<P>) -> Self {
561 let inner = PossibleValuesParser::from(values);
562 Self::from(inner)
563 }
564}
565
566impl std::fmt::Debug for ValueParser {
567 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
568 match &self.0 {
569 ValueParserInner::Bool => f.debug_struct("ValueParser::bool").finish(),
570 ValueParserInner::String => f.debug_struct("ValueParser::string").finish(),
571 ValueParserInner::OsString => f.debug_struct("ValueParser::os_string").finish(),
572 ValueParserInner::PathBuf => f.debug_struct("ValueParser::path_buf").finish(),
573 ValueParserInner::Other(o) => f.write_fmt(format_args!("ValueParser::other({0:?})", o.type_id()))write!(f, "ValueParser::other({:?})", o.type_id()),
574 }
575 }
576}
577
578impl Clone for ValueParser {
579 fn clone(&self) -> Self {
580 Self(match &self.0 {
581 ValueParserInner::Bool => ValueParserInner::Bool,
582 ValueParserInner::String => ValueParserInner::String,
583 ValueParserInner::OsString => ValueParserInner::OsString,
584 ValueParserInner::PathBuf => ValueParserInner::PathBuf,
585 ValueParserInner::Other(o) => ValueParserInner::Other(o.clone_any()),
586 })
587 }
588}
589
590trait AnyValueParser: Send + Sync + 'static {
592 fn parse_ref(
593 &self,
594 cmd: &crate::Command,
595 arg: Option<&crate::Arg>,
596 value: &std::ffi::OsStr,
597 ) -> Result<AnyValue, crate::Error>;
598
599 fn parse_ref_(
600 &self,
601 cmd: &crate::Command,
602 arg: Option<&crate::Arg>,
603 value: &std::ffi::OsStr,
604 _source: ValueSource,
605 ) -> Result<AnyValue, crate::Error> {
606 self.parse_ref(cmd, arg, value)
607 }
608
609 fn type_id(&self) -> AnyValueId;
611
612 fn possible_values(
613 &self,
614 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>>;
615
616 fn clone_any(&self) -> Box<dyn AnyValueParser>;
617}
618
619impl<T, P> AnyValueParser for P
620where
621 T: std::any::Any + Clone + Send + Sync + 'static,
622 P: TypedValueParser<Value = T>,
623{
624 fn parse_ref(
625 &self,
626 cmd: &crate::Command,
627 arg: Option<&crate::Arg>,
628 value: &std::ffi::OsStr,
629 ) -> Result<AnyValue, crate::Error> {
630 let value = match TypedValueParser::parse_ref(self, cmd, arg, value) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(TypedValueParser::parse_ref(self, cmd, arg, value));
631 Ok(AnyValue::new(value))
632 }
633
634 fn parse_ref_(
635 &self,
636 cmd: &crate::Command,
637 arg: Option<&crate::Arg>,
638 value: &std::ffi::OsStr,
639 source: ValueSource,
640 ) -> Result<AnyValue, crate::Error> {
641 let value = match TypedValueParser::parse_ref_(self, cmd, arg, value, source) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(TypedValueParser::parse_ref_(self, cmd, arg, value, source));
642 Ok(AnyValue::new(value))
643 }
644
645 fn type_id(&self) -> AnyValueId {
646 AnyValueId::of::<T>()
647 }
648
649 fn possible_values(
650 &self,
651 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
652 P::possible_values(self)
653 }
654
655 fn clone_any(&self) -> Box<dyn AnyValueParser> {
656 Box::new(self.clone())
657 }
658}
659
660pub trait TypedValueParser: Clone + Send + Sync + 'static {
712 type Value: Send + Sync + Clone;
714
715 fn parse_ref(
719 &self,
720 cmd: &crate::Command,
721 arg: Option<&crate::Arg>,
722 value: &std::ffi::OsStr,
723 ) -> Result<Self::Value, crate::Error>;
724
725 fn parse_ref_(
729 &self,
730 cmd: &crate::Command,
731 arg: Option<&crate::Arg>,
732 value: &std::ffi::OsStr,
733 _source: ValueSource,
734 ) -> Result<Self::Value, crate::Error> {
735 self.parse_ref(cmd, arg, value)
736 }
737
738 fn parse(
742 &self,
743 cmd: &crate::Command,
744 arg: Option<&crate::Arg>,
745 value: std::ffi::OsString,
746 ) -> Result<Self::Value, crate::Error> {
747 self.parse_ref(cmd, arg, &value)
748 }
749
750 fn parse_(
754 &self,
755 cmd: &crate::Command,
756 arg: Option<&crate::Arg>,
757 value: std::ffi::OsString,
758 _source: ValueSource,
759 ) -> Result<Self::Value, crate::Error> {
760 self.parse(cmd, arg, value)
761 }
762
763 fn possible_values(
768 &self,
769 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
770 None
771 }
772
773 fn map<T, F>(self, func: F) -> MapValueParser<Self, F>
811 where
812 T: Send + Sync + Clone,
813 F: Fn(Self::Value) -> T + Clone,
814 {
815 MapValueParser::new(self, func)
816 }
817
818 fn try_map<T, E, F>(self, func: F) -> TryMapValueParser<Self, F>
861 where
862 F: Fn(Self::Value) -> Result<T, E> + Clone + Send + Sync + 'static,
863 T: Send + Sync + Clone,
864 E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
865 {
866 TryMapValueParser::new(self, func)
867 }
868}
869
870impl<F, T, E> TypedValueParser for F
871where
872 F: Fn(&str) -> Result<T, E> + Clone + Send + Sync + 'static,
873 E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
874 T: Send + Sync + Clone,
875{
876 type Value = T;
877
878 fn parse_ref(
879 &self,
880 cmd: &crate::Command,
881 arg: Option<&crate::Arg>,
882 value: &std::ffi::OsStr,
883 ) -> Result<Self::Value, crate::Error> {
884 let value = match value.to_str().ok_or_else(||
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.to_str().ok_or_else(|| {
885 crate::Error::invalid_utf8(
886 cmd,
887 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
888 )
889 }));
890 let value = match (self)(value).map_err(|e|
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg, value.to_owned(),
e.into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!((self)(value).map_err(|e| {
891 let arg = arg
892 .map(|a| a.to_string())
893 .unwrap_or_else(|| "...".to_owned());
894 crate::Error::value_validation(arg, value.to_owned(), e.into()).with_cmd(cmd)
895 }));
896 Ok(value)
897 }
898}
899
900#[derive(#[automatically_derived]
impl ::core::marker::Copy for StringValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for StringValueParser {
#[inline]
fn clone(&self) -> StringValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for StringValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "StringValueParser")
}
}Debug)]
904#[non_exhaustive]
905pub struct StringValueParser {}
906
907impl StringValueParser {
908 pub fn new() -> Self {
910 Self {}
911 }
912}
913
914impl TypedValueParser for StringValueParser {
915 type Value = String;
916
917 fn parse_ref(
918 &self,
919 cmd: &crate::Command,
920 arg: Option<&crate::Arg>,
921 value: &std::ffi::OsStr,
922 ) -> Result<Self::Value, crate::Error> {
923 TypedValueParser::parse(self, cmd, arg, value.to_owned())
924 }
925
926 fn parse(
927 &self,
928 cmd: &crate::Command,
929 _arg: Option<&crate::Arg>,
930 value: std::ffi::OsString,
931 ) -> Result<Self::Value, crate::Error> {
932 let value = match value.into_string().map_err(|_|
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.into_string().map_err(|_| {
933 crate::Error::invalid_utf8(
934 cmd,
935 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
936 )
937 }));
938 Ok(value)
939 }
940}
941
942impl Default for StringValueParser {
943 fn default() -> Self {
944 Self::new()
945 }
946}
947
948#[derive(#[automatically_derived]
impl ::core::marker::Copy for OsStringValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for OsStringValueParser {
#[inline]
fn clone(&self) -> OsStringValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for OsStringValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "OsStringValueParser")
}
}Debug)]
952#[non_exhaustive]
953pub struct OsStringValueParser {}
954
955impl OsStringValueParser {
956 pub fn new() -> Self {
958 Self {}
959 }
960}
961
962impl TypedValueParser for OsStringValueParser {
963 type Value = std::ffi::OsString;
964
965 fn parse_ref(
966 &self,
967 cmd: &crate::Command,
968 arg: Option<&crate::Arg>,
969 value: &std::ffi::OsStr,
970 ) -> Result<Self::Value, crate::Error> {
971 TypedValueParser::parse(self, cmd, arg, value.to_owned())
972 }
973
974 fn parse(
975 &self,
976 _cmd: &crate::Command,
977 _arg: Option<&crate::Arg>,
978 value: std::ffi::OsString,
979 ) -> Result<Self::Value, crate::Error> {
980 Ok(value)
981 }
982}
983
984impl Default for OsStringValueParser {
985 fn default() -> Self {
986 Self::new()
987 }
988}
989
990#[derive(#[automatically_derived]
impl ::core::marker::Copy for PathBufValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for PathBufValueParser {
#[inline]
fn clone(&self) -> PathBufValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for PathBufValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "PathBufValueParser")
}
}Debug)]
994#[non_exhaustive]
995pub struct PathBufValueParser {}
996
997impl PathBufValueParser {
998 pub fn new() -> Self {
1000 Self {}
1001 }
1002}
1003
1004impl TypedValueParser for PathBufValueParser {
1005 type Value = std::path::PathBuf;
1006
1007 fn parse_ref(
1008 &self,
1009 cmd: &crate::Command,
1010 arg: Option<&crate::Arg>,
1011 value: &std::ffi::OsStr,
1012 ) -> Result<Self::Value, crate::Error> {
1013 TypedValueParser::parse(self, cmd, arg, value.to_owned())
1014 }
1015
1016 fn parse(
1017 &self,
1018 cmd: &crate::Command,
1019 arg: Option<&crate::Arg>,
1020 value: std::ffi::OsString,
1021 ) -> Result<Self::Value, crate::Error> {
1022 if value.is_empty() {
1023 return Err(crate::Error::empty_value(
1024 cmd,
1025 &[],
1026 arg.map(ToString::to_string)
1027 .unwrap_or_else(|| "...".to_owned()),
1028 ));
1029 }
1030 Ok(Self::Value::from(value))
1031 }
1032}
1033
1034impl Default for PathBufValueParser {
1035 fn default() -> Self {
1036 Self::new()
1037 }
1038}
1039
1040#[derive(#[automatically_derived]
impl<E: ::core::clone::Clone + crate::ValueEnum + Clone + Send + Sync +
'static> ::core::clone::Clone for EnumValueParser<E> {
#[inline]
fn clone(&self) -> EnumValueParser<E> {
EnumValueParser(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<E: ::core::fmt::Debug + crate::ValueEnum + Clone + Send + Sync + 'static>
::core::fmt::Debug for EnumValueParser<E> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"EnumValueParser", &&self.0)
}
}Debug)]
1079pub struct EnumValueParser<E: crate::ValueEnum + Clone + Send + Sync + 'static>(
1080 std::marker::PhantomData<E>,
1081);
1082
1083impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> EnumValueParser<E> {
1084 pub fn new() -> Self {
1086 let phantom: std::marker::PhantomData<E> = Default::default();
1087 Self(phantom)
1088 }
1089}
1090
1091impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for EnumValueParser<E> {
1092 type Value = E;
1093
1094 fn parse_ref(
1095 &self,
1096 cmd: &crate::Command,
1097 arg: Option<&crate::Arg>,
1098 value: &std::ffi::OsStr,
1099 ) -> Result<Self::Value, crate::Error> {
1100 let ignore_case = arg.map(|a| a.is_ignore_case_set()).unwrap_or(false);
1101 let possible_vals = || {
1102 E::value_variants()
1103 .iter()
1104 .filter_map(|v| v.to_possible_value())
1105 .filter(|v| !v.is_hide_set())
1106 .map(|v| v.get_name().to_owned())
1107 .collect::<Vec<_>>()
1108 };
1109
1110 let value = match value.to_str().ok_or_else(||
{
crate::Error::invalid_value(cmd,
value.to_string_lossy().into_owned(), &possible_vals(),
arg.map(ToString::to_string).unwrap_or_else(||
"...".to_owned()))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.to_str().ok_or_else(|| {
1111 crate::Error::invalid_value(
1112 cmd,
1113 value.to_string_lossy().into_owned(),
1114 &possible_vals(),
1115 arg.map(ToString::to_string)
1116 .unwrap_or_else(|| "...".to_owned()),
1117 )
1118 }));
1119 let value = match E::value_variants().iter().find(|v|
{
v.to_possible_value().expect("ValueEnum::value_variants contains only values with a corresponding ValueEnum::to_possible_value").matches(value,
ignore_case)
}).ok_or_else(||
{
crate::Error::invalid_value(cmd, value.to_owned(),
&possible_vals(),
arg.map(ToString::to_string).unwrap_or_else(||
"...".to_owned()))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(E::value_variants()
1120 .iter()
1121 .find(|v| {
1122 v.to_possible_value()
1123 .expect("ValueEnum::value_variants contains only values with a corresponding ValueEnum::to_possible_value")
1124 .matches(value, ignore_case)
1125 })
1126 .ok_or_else(|| {
1127 crate::Error::invalid_value(
1128 cmd,
1129 value.to_owned(),
1130 &possible_vals(),
1131 arg.map(ToString::to_string)
1132 .unwrap_or_else(|| "...".to_owned()),
1133 )
1134 }))
1135 .clone();
1136 Ok(value)
1137 }
1138
1139 fn possible_values(
1140 &self,
1141 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
1142 Some(Box::new(
1143 E::value_variants()
1144 .iter()
1145 .filter_map(|v| v.to_possible_value()),
1146 ))
1147 }
1148}
1149
1150impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueParser<E> {
1151 fn default() -> Self {
1152 Self::new()
1153 }
1154}
1155
1156#[derive(#[automatically_derived]
impl ::core::clone::Clone for PossibleValuesParser {
#[inline]
fn clone(&self) -> PossibleValuesParser {
PossibleValuesParser(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for PossibleValuesParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"PossibleValuesParser", &&self.0)
}
}Debug)]
1196pub struct PossibleValuesParser(Vec<super::PossibleValue>);
1197
1198impl PossibleValuesParser {
1199 pub fn new(values: impl Into<PossibleValuesParser>) -> Self {
1201 values.into()
1202 }
1203}
1204
1205impl TypedValueParser for PossibleValuesParser {
1206 type Value = String;
1207
1208 fn parse_ref(
1209 &self,
1210 cmd: &crate::Command,
1211 arg: Option<&crate::Arg>,
1212 value: &std::ffi::OsStr,
1213 ) -> Result<Self::Value, crate::Error> {
1214 TypedValueParser::parse(self, cmd, arg, value.to_owned())
1215 }
1216
1217 fn parse(
1218 &self,
1219 cmd: &crate::Command,
1220 arg: Option<&crate::Arg>,
1221 value: std::ffi::OsString,
1222 ) -> Result<String, crate::Error> {
1223 let value = match value.into_string().map_err(|_|
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.into_string().map_err(|_| {
1224 crate::Error::invalid_utf8(
1225 cmd,
1226 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
1227 )
1228 }));
1229
1230 let ignore_case = arg.map(|a| a.is_ignore_case_set()).unwrap_or(false);
1231 if self.0.iter().any(|v| v.matches(&value, ignore_case)) {
1232 Ok(value)
1233 } else {
1234 let possible_vals = self
1235 .0
1236 .iter()
1237 .filter(|v| !v.is_hide_set())
1238 .map(|v| v.get_name().to_owned())
1239 .collect::<Vec<_>>();
1240
1241 Err(crate::Error::invalid_value(
1242 cmd,
1243 value,
1244 &possible_vals,
1245 arg.map(ToString::to_string)
1246 .unwrap_or_else(|| "...".to_owned()),
1247 ))
1248 }
1249 }
1250
1251 fn possible_values(
1252 &self,
1253 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
1254 Some(Box::new(self.0.iter().cloned()))
1255 }
1256}
1257
1258impl<I, T> From<I> for PossibleValuesParser
1259where
1260 I: IntoIterator<Item = T>,
1261 T: Into<super::PossibleValue>,
1262{
1263 fn from(values: I) -> Self {
1264 Self(values.into_iter().map(|t| t.into()).collect())
1265 }
1266}
1267
1268#[derive(#[automatically_derived]
impl<T: ::core::marker::Copy + TryFrom<i64> + Clone + Send + Sync>
::core::marker::Copy for RangedI64ValueParser<T> {
}Copy, #[automatically_derived]
impl<T: ::core::clone::Clone + TryFrom<i64> + Clone + Send + Sync>
::core::clone::Clone for RangedI64ValueParser<T> {
#[inline]
fn clone(&self) -> RangedI64ValueParser<T> {
RangedI64ValueParser {
bounds: ::core::clone::Clone::clone(&self.bounds),
target: ::core::clone::Clone::clone(&self.target),
}
}
}Clone, #[automatically_derived]
impl<T: ::core::fmt::Debug + TryFrom<i64> + Clone + Send + Sync>
::core::fmt::Debug for RangedI64ValueParser<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"RangedI64ValueParser", "bounds", &self.bounds, "target",
&&self.target)
}
}Debug)]
1315pub struct RangedI64ValueParser<T: TryFrom<i64> + Clone + Send + Sync = i64> {
1316 bounds: (std::ops::Bound<i64>, std::ops::Bound<i64>),
1317 target: std::marker::PhantomData<T>,
1318}
1319
1320impl<T: TryFrom<i64> + Clone + Send + Sync> RangedI64ValueParser<T> {
1321 pub fn new() -> Self {
1323 Self::from(..)
1324 }
1325
1326 pub fn range<B: RangeBounds<i64>>(mut self, range: B) -> Self {
1328 let start = match range.start_bound() {
1332 l @ std::ops::Bound::Included(i) => {
1333 if true {
if !self.bounds.contains(i) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1334 self.bounds.contains(i),
1335 "{} must be in {:?}",
1336 i,
1337 self.bounds
1338 );
1339 l.cloned()
1340 }
1341 l @ std::ops::Bound::Excluded(i) => {
1342 if true {
if !self.bounds.contains(&i.saturating_add(1)) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1343 self.bounds.contains(&i.saturating_add(1)),
1344 "{} must be in {:?}",
1345 i,
1346 self.bounds
1347 );
1348 l.cloned()
1349 }
1350 std::ops::Bound::Unbounded => self.bounds.start_bound().cloned(),
1351 };
1352 let end = match range.end_bound() {
1353 l @ std::ops::Bound::Included(i) => {
1354 if true {
if !self.bounds.contains(i) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1355 self.bounds.contains(i),
1356 "{} must be in {:?}",
1357 i,
1358 self.bounds
1359 );
1360 l.cloned()
1361 }
1362 l @ std::ops::Bound::Excluded(i) => {
1363 if true {
if !self.bounds.contains(&i.saturating_sub(1)) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1364 self.bounds.contains(&i.saturating_sub(1)),
1365 "{} must be in {:?}",
1366 i,
1367 self.bounds
1368 );
1369 l.cloned()
1370 }
1371 std::ops::Bound::Unbounded => self.bounds.end_bound().cloned(),
1372 };
1373 self.bounds = (start, end);
1374 self
1375 }
1376
1377 fn format_bounds(&self) -> String {
1378 let mut result = match self.bounds.0 {
1379 std::ops::Bound::Included(i) => i.to_string(),
1380 std::ops::Bound::Excluded(i) => i.saturating_add(1).to_string(),
1381 std::ops::Bound::Unbounded => i64::MIN.to_string(),
1382 };
1383 result.push_str("..");
1384 match self.bounds.1 {
1385 std::ops::Bound::Included(i) => {
1386 result.push('=');
1387 result.push_str(&i.to_string());
1388 }
1389 std::ops::Bound::Excluded(i) => {
1390 result.push_str(&i.to_string());
1391 }
1392 std::ops::Bound::Unbounded => {
1393 result.push_str(&i64::MAX.to_string());
1394 }
1395 }
1396 result
1397 }
1398}
1399
1400impl<T: TryFrom<i64> + Clone + Send + Sync + 'static> TypedValueParser for RangedI64ValueParser<T>
1401where
1402 <T as TryFrom<i64>>::Error: Send + Sync + 'static + std::error::Error + ToString,
1403{
1404 type Value = T;
1405
1406 fn parse_ref(
1407 &self,
1408 cmd: &crate::Command,
1409 arg: Option<&crate::Arg>,
1410 raw_value: &std::ffi::OsStr,
1411 ) -> Result<Self::Value, crate::Error> {
1412 let value = match raw_value.to_str().ok_or_else(||
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(raw_value.to_str().ok_or_else(|| {
1413 crate::Error::invalid_utf8(
1414 cmd,
1415 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
1416 )
1417 }));
1418 let value = match value.parse::<i64>().map_err(|err|
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg,
raw_value.to_string_lossy().into_owned(),
err.into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.parse::<i64>().map_err(|err| {
1419 let arg = arg
1420 .map(|a| a.to_string())
1421 .unwrap_or_else(|| "...".to_owned());
1422 crate::Error::value_validation(
1423 arg,
1424 raw_value.to_string_lossy().into_owned(),
1425 err.into(),
1426 )
1427 .with_cmd(cmd)
1428 }));
1429 if !self.bounds.contains(&value) {
1430 let arg = arg
1431 .map(|a| a.to_string())
1432 .unwrap_or_else(|| "...".to_owned());
1433 return Err(crate::Error::value_validation(
1434 arg,
1435 raw_value.to_string_lossy().into_owned(),
1436 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is not in {1}", value,
self.format_bounds()))
})format!("{} is not in {}", value, self.format_bounds()).into(),
1437 )
1438 .with_cmd(cmd));
1439 }
1440
1441 let value: Result<Self::Value, _> = value.try_into();
1442 let value = match value.map_err(|err|
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg,
raw_value.to_string_lossy().into_owned(),
err.into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.map_err(|err| {
1443 let arg = arg
1444 .map(|a| a.to_string())
1445 .unwrap_or_else(|| "...".to_owned());
1446 crate::Error::value_validation(
1447 arg,
1448 raw_value.to_string_lossy().into_owned(),
1449 err.into(),
1450 )
1451 .with_cmd(cmd)
1452 }));
1453
1454 Ok(value)
1455 }
1456}
1457
1458impl<T: TryFrom<i64> + Clone + Send + Sync, B: RangeBounds<i64>> From<B>
1459 for RangedI64ValueParser<T>
1460{
1461 fn from(range: B) -> Self {
1462 Self {
1463 bounds: (range.start_bound().cloned(), range.end_bound().cloned()),
1464 target: Default::default(),
1465 }
1466 }
1467}
1468
1469impl<T: TryFrom<i64> + Clone + Send + Sync> Default for RangedI64ValueParser<T> {
1470 fn default() -> Self {
1471 Self::new()
1472 }
1473}
1474
1475#[derive(#[automatically_derived]
impl<T: ::core::marker::Copy + TryFrom<u64>> ::core::marker::Copy for
RangedU64ValueParser<T> {
}Copy, #[automatically_derived]
impl<T: ::core::clone::Clone + TryFrom<u64>> ::core::clone::Clone for
RangedU64ValueParser<T> {
#[inline]
fn clone(&self) -> RangedU64ValueParser<T> {
RangedU64ValueParser {
bounds: ::core::clone::Clone::clone(&self.bounds),
target: ::core::clone::Clone::clone(&self.target),
}
}
}Clone, #[automatically_derived]
impl<T: ::core::fmt::Debug + TryFrom<u64>> ::core::fmt::Debug for
RangedU64ValueParser<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"RangedU64ValueParser", "bounds", &self.bounds, "target",
&&self.target)
}
}Debug)]
1514pub struct RangedU64ValueParser<T: TryFrom<u64> = u64> {
1515 bounds: (std::ops::Bound<u64>, std::ops::Bound<u64>),
1516 target: std::marker::PhantomData<T>,
1517}
1518
1519impl<T: TryFrom<u64>> RangedU64ValueParser<T> {
1520 pub fn new() -> Self {
1522 Self::from(..)
1523 }
1524
1525 pub fn range<B: RangeBounds<u64>>(mut self, range: B) -> Self {
1527 let start = match range.start_bound() {
1531 l @ std::ops::Bound::Included(i) => {
1532 if true {
if !self.bounds.contains(i) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1533 self.bounds.contains(i),
1534 "{} must be in {:?}",
1535 i,
1536 self.bounds
1537 );
1538 l.cloned()
1539 }
1540 l @ std::ops::Bound::Excluded(i) => {
1541 if true {
if !self.bounds.contains(&i.saturating_add(1)) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1542 self.bounds.contains(&i.saturating_add(1)),
1543 "{} must be in {:?}",
1544 i,
1545 self.bounds
1546 );
1547 l.cloned()
1548 }
1549 std::ops::Bound::Unbounded => self.bounds.start_bound().cloned(),
1550 };
1551 let end = match range.end_bound() {
1552 l @ std::ops::Bound::Included(i) => {
1553 if true {
if !self.bounds.contains(i) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1554 self.bounds.contains(i),
1555 "{} must be in {:?}",
1556 i,
1557 self.bounds
1558 );
1559 l.cloned()
1560 }
1561 l @ std::ops::Bound::Excluded(i) => {
1562 if true {
if !self.bounds.contains(&i.saturating_sub(1)) {
{
::core::panicking::panic_fmt(format_args!("{0} must be in {1:?}",
i, self.bounds));
}
};
};debug_assert!(
1563 self.bounds.contains(&i.saturating_sub(1)),
1564 "{} must be in {:?}",
1565 i,
1566 self.bounds
1567 );
1568 l.cloned()
1569 }
1570 std::ops::Bound::Unbounded => self.bounds.end_bound().cloned(),
1571 };
1572 self.bounds = (start, end);
1573 self
1574 }
1575
1576 fn format_bounds(&self) -> String {
1577 let mut result = match self.bounds.0 {
1578 std::ops::Bound::Included(i) => i.to_string(),
1579 std::ops::Bound::Excluded(i) => i.saturating_add(1).to_string(),
1580 std::ops::Bound::Unbounded => u64::MIN.to_string(),
1581 };
1582 result.push_str("..");
1583 match self.bounds.1 {
1584 std::ops::Bound::Included(i) => {
1585 result.push('=');
1586 result.push_str(&i.to_string());
1587 }
1588 std::ops::Bound::Excluded(i) => {
1589 result.push_str(&i.to_string());
1590 }
1591 std::ops::Bound::Unbounded => {
1592 result.push_str(&u64::MAX.to_string());
1593 }
1594 }
1595 result
1596 }
1597}
1598
1599impl<T: TryFrom<u64> + Clone + Send + Sync + 'static> TypedValueParser for RangedU64ValueParser<T>
1600where
1601 <T as TryFrom<u64>>::Error: Send + Sync + 'static + std::error::Error + ToString,
1602{
1603 type Value = T;
1604
1605 fn parse_ref(
1606 &self,
1607 cmd: &crate::Command,
1608 arg: Option<&crate::Arg>,
1609 raw_value: &std::ffi::OsStr,
1610 ) -> Result<Self::Value, crate::Error> {
1611 let value = match raw_value.to_str().ok_or_else(||
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(raw_value.to_str().ok_or_else(|| {
1612 crate::Error::invalid_utf8(
1613 cmd,
1614 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
1615 )
1616 }));
1617 let value = match value.parse::<u64>().map_err(|err|
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg,
raw_value.to_string_lossy().into_owned(),
err.into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.parse::<u64>().map_err(|err| {
1618 let arg = arg
1619 .map(|a| a.to_string())
1620 .unwrap_or_else(|| "...".to_owned());
1621 crate::Error::value_validation(
1622 arg,
1623 raw_value.to_string_lossy().into_owned(),
1624 err.into(),
1625 )
1626 .with_cmd(cmd)
1627 }));
1628 if !self.bounds.contains(&value) {
1629 let arg = arg
1630 .map(|a| a.to_string())
1631 .unwrap_or_else(|| "...".to_owned());
1632 return Err(crate::Error::value_validation(
1633 arg,
1634 raw_value.to_string_lossy().into_owned(),
1635 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is not in {1}", value,
self.format_bounds()))
})format!("{} is not in {}", value, self.format_bounds()).into(),
1636 )
1637 .with_cmd(cmd));
1638 }
1639
1640 let value: Result<Self::Value, _> = value.try_into();
1641 let value = match value.map_err(|err|
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg,
raw_value.to_string_lossy().into_owned(),
err.into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.map_err(|err| {
1642 let arg = arg
1643 .map(|a| a.to_string())
1644 .unwrap_or_else(|| "...".to_owned());
1645 crate::Error::value_validation(
1646 arg,
1647 raw_value.to_string_lossy().into_owned(),
1648 err.into(),
1649 )
1650 .with_cmd(cmd)
1651 }));
1652
1653 Ok(value)
1654 }
1655}
1656
1657impl<T: TryFrom<u64>, B: RangeBounds<u64>> From<B> for RangedU64ValueParser<T> {
1658 fn from(range: B) -> Self {
1659 Self {
1660 bounds: (range.start_bound().cloned(), range.end_bound().cloned()),
1661 target: Default::default(),
1662 }
1663 }
1664}
1665
1666impl<T: TryFrom<u64>> Default for RangedU64ValueParser<T> {
1667 fn default() -> Self {
1668 Self::new()
1669 }
1670}
1671
1672#[derive(#[automatically_derived]
impl ::core::marker::Copy for BoolValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for BoolValueParser {
#[inline]
fn clone(&self) -> BoolValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for BoolValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "BoolValueParser")
}
}Debug)]
1676#[non_exhaustive]
1677pub struct BoolValueParser {}
1678
1679impl BoolValueParser {
1680 pub fn new() -> Self {
1682 Self {}
1683 }
1684
1685 fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
1686 ["true", "false"]
1687 .iter()
1688 .copied()
1689 .map(crate::builder::PossibleValue::new)
1690 }
1691}
1692
1693impl TypedValueParser for BoolValueParser {
1694 type Value = bool;
1695
1696 fn parse_ref(
1697 &self,
1698 cmd: &crate::Command,
1699 arg: Option<&crate::Arg>,
1700 value: &std::ffi::OsStr,
1701 ) -> Result<Self::Value, crate::Error> {
1702 let value = if value == std::ffi::OsStr::new("true") {
1703 true
1704 } else if value == std::ffi::OsStr::new("false") {
1705 false
1706 } else {
1707 let possible_vals = Self::possible_values()
1709 .map(|v| v.get_name().to_owned())
1710 .collect::<Vec<_>>();
1711
1712 return Err(crate::Error::invalid_value(
1713 cmd,
1714 value.to_string_lossy().into_owned(),
1715 &possible_vals,
1716 arg.map(ToString::to_string)
1717 .unwrap_or_else(|| "...".to_owned()),
1718 ));
1719 };
1720 Ok(value)
1721 }
1722
1723 fn possible_values(
1724 &self,
1725 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
1726 Some(Box::new(Self::possible_values()))
1727 }
1728}
1729
1730impl Default for BoolValueParser {
1731 fn default() -> Self {
1732 Self::new()
1733 }
1734}
1735
1736#[derive(#[automatically_derived]
impl ::core::marker::Copy for FalseyValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FalseyValueParser {
#[inline]
fn clone(&self) -> FalseyValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for FalseyValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "FalseyValueParser")
}
}Debug)]
1777#[non_exhaustive]
1778pub struct FalseyValueParser {}
1779
1780impl FalseyValueParser {
1781 pub fn new() -> Self {
1783 Self {}
1784 }
1785
1786 fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
1787 crate::util::TRUE_LITERALS
1788 .iter()
1789 .chain(crate::util::FALSE_LITERALS.iter())
1790 .copied()
1791 .map(|l| crate::builder::PossibleValue::new(l).hide(l != "true" && l != "false"))
1792 }
1793}
1794
1795impl TypedValueParser for FalseyValueParser {
1796 type Value = bool;
1797
1798 fn parse_ref(
1799 &self,
1800 cmd: &crate::Command,
1801 _arg: Option<&crate::Arg>,
1802 value: &std::ffi::OsStr,
1803 ) -> Result<Self::Value, crate::Error> {
1804 let value = match value.to_str().ok_or_else(||
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.to_str().ok_or_else(|| {
1805 crate::Error::invalid_utf8(
1806 cmd,
1807 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
1808 )
1809 }));
1810 let value = if value.is_empty() {
1811 false
1812 } else {
1813 crate::util::str_to_bool(value).unwrap_or(true)
1814 };
1815 Ok(value)
1816 }
1817
1818 fn possible_values(
1819 &self,
1820 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
1821 Some(Box::new(Self::possible_values()))
1822 }
1823}
1824
1825impl Default for FalseyValueParser {
1826 fn default() -> Self {
1827 Self::new()
1828 }
1829}
1830
1831#[derive(#[automatically_derived]
impl ::core::marker::Copy for BoolishValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for BoolishValueParser {
#[inline]
fn clone(&self) -> BoolishValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for BoolishValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "BoolishValueParser")
}
}Debug)]
1876#[non_exhaustive]
1877pub struct BoolishValueParser {}
1878
1879impl BoolishValueParser {
1880 pub fn new() -> Self {
1882 Self {}
1883 }
1884
1885 fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
1886 crate::util::TRUE_LITERALS
1887 .iter()
1888 .chain(crate::util::FALSE_LITERALS.iter())
1889 .copied()
1890 .map(|l| crate::builder::PossibleValue::new(l).hide(l != "true" && l != "false"))
1891 }
1892}
1893
1894impl TypedValueParser for BoolishValueParser {
1895 type Value = bool;
1896
1897 fn parse_ref(
1898 &self,
1899 cmd: &crate::Command,
1900 arg: Option<&crate::Arg>,
1901 value: &std::ffi::OsStr,
1902 ) -> Result<Self::Value, crate::Error> {
1903 let value = match value.to_str().ok_or_else(||
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.to_str().ok_or_else(|| {
1904 crate::Error::invalid_utf8(
1905 cmd,
1906 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
1907 )
1908 }));
1909 let value = match crate::util::str_to_bool(value).ok_or_else(||
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg, value.to_owned(),
"value was not a boolean".into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(crate::util::str_to_bool(value).ok_or_else(|| {
1910 let arg = arg
1911 .map(|a| a.to_string())
1912 .unwrap_or_else(|| "...".to_owned());
1913 crate::Error::value_validation(arg, value.to_owned(), "value was not a boolean".into())
1914 .with_cmd(cmd)
1915 }));
1916 Ok(value)
1917 }
1918
1919 fn possible_values(
1920 &self,
1921 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
1922 Some(Box::new(Self::possible_values()))
1923 }
1924}
1925
1926impl Default for BoolishValueParser {
1927 fn default() -> Self {
1928 Self::new()
1929 }
1930}
1931
1932#[derive(#[automatically_derived]
impl ::core::marker::Copy for NonEmptyStringValueParser { }Copy, #[automatically_derived]
impl ::core::clone::Clone for NonEmptyStringValueParser {
#[inline]
fn clone(&self) -> NonEmptyStringValueParser { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for NonEmptyStringValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "NonEmptyStringValueParser")
}
}Debug)]
1967#[non_exhaustive]
1968pub struct NonEmptyStringValueParser {}
1969
1970impl NonEmptyStringValueParser {
1971 pub fn new() -> Self {
1973 Self {}
1974 }
1975}
1976
1977impl TypedValueParser for NonEmptyStringValueParser {
1978 type Value = String;
1979
1980 fn parse_ref(
1981 &self,
1982 cmd: &crate::Command,
1983 arg: Option<&crate::Arg>,
1984 value: &std::ffi::OsStr,
1985 ) -> Result<Self::Value, crate::Error> {
1986 if value.is_empty() {
1987 return Err(crate::Error::empty_value(
1988 cmd,
1989 &[],
1990 arg.map(ToString::to_string)
1991 .unwrap_or_else(|| "...".to_owned()),
1992 ));
1993 }
1994 let value = match value.to_str().ok_or_else(||
{
crate::Error::invalid_utf8(cmd,
crate::output::Usage::new(cmd).create_usage_with_title(&[]))
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(value.to_str().ok_or_else(|| {
1995 crate::Error::invalid_utf8(
1996 cmd,
1997 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
1998 )
1999 }));
2000 Ok(value.to_owned())
2001 }
2002}
2003
2004impl Default for NonEmptyStringValueParser {
2005 fn default() -> Self {
2006 Self::new()
2007 }
2008}
2009
2010#[derive(#[automatically_derived]
impl<P: ::core::clone::Clone, F: ::core::clone::Clone> ::core::clone::Clone
for MapValueParser<P, F> {
#[inline]
fn clone(&self) -> MapValueParser<P, F> {
MapValueParser {
parser: ::core::clone::Clone::clone(&self.parser),
func: ::core::clone::Clone::clone(&self.func),
}
}
}Clone, #[automatically_derived]
impl<P: ::core::fmt::Debug, F: ::core::fmt::Debug> ::core::fmt::Debug for
MapValueParser<P, F> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"MapValueParser", "parser", &self.parser, "func", &&self.func)
}
}Debug)]
2014pub struct MapValueParser<P, F> {
2015 parser: P,
2016 func: F,
2017}
2018
2019impl<P, F, T> MapValueParser<P, F>
2020where
2021 P: TypedValueParser,
2022 P::Value: Send + Sync + Clone,
2023 F: Fn(P::Value) -> T + Clone,
2024 T: Send + Sync + Clone,
2025{
2026 fn new(parser: P, func: F) -> Self {
2027 Self { parser, func }
2028 }
2029}
2030
2031impl<P, F, T> TypedValueParser for MapValueParser<P, F>
2032where
2033 P: TypedValueParser,
2034 P::Value: Send + Sync + Clone,
2035 F: Fn(P::Value) -> T + Clone + Send + Sync + 'static,
2036 T: Send + Sync + Clone,
2037{
2038 type Value = T;
2039
2040 fn parse_ref(
2041 &self,
2042 cmd: &crate::Command,
2043 arg: Option<&crate::Arg>,
2044 value: &std::ffi::OsStr,
2045 ) -> Result<Self::Value, crate::Error> {
2046 let value = match self.parser.parse_ref(cmd, arg, value) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(self.parser.parse_ref(cmd, arg, value));
2047 let value = (self.func)(value);
2048 Ok(value)
2049 }
2050
2051 fn parse(
2052 &self,
2053 cmd: &crate::Command,
2054 arg: Option<&crate::Arg>,
2055 value: std::ffi::OsString,
2056 ) -> Result<Self::Value, crate::Error> {
2057 let value = match self.parser.parse(cmd, arg, value) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(self.parser.parse(cmd, arg, value));
2058 let value = (self.func)(value);
2059 Ok(value)
2060 }
2061
2062 fn possible_values(
2063 &self,
2064 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
2065 self.parser.possible_values()
2066 }
2067}
2068
2069#[derive(#[automatically_derived]
impl<P: ::core::clone::Clone, F: ::core::clone::Clone> ::core::clone::Clone
for TryMapValueParser<P, F> {
#[inline]
fn clone(&self) -> TryMapValueParser<P, F> {
TryMapValueParser {
parser: ::core::clone::Clone::clone(&self.parser),
func: ::core::clone::Clone::clone(&self.func),
}
}
}Clone, #[automatically_derived]
impl<P: ::core::fmt::Debug, F: ::core::fmt::Debug> ::core::fmt::Debug for
TryMapValueParser<P, F> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"TryMapValueParser", "parser", &self.parser, "func", &&self.func)
}
}Debug)]
2073pub struct TryMapValueParser<P, F> {
2074 parser: P,
2075 func: F,
2076}
2077
2078impl<P, F, T, E> TryMapValueParser<P, F>
2079where
2080 P: TypedValueParser,
2081 P::Value: Send + Sync + Clone,
2082 F: Fn(P::Value) -> Result<T, E> + Clone + Send + Sync + 'static,
2083 T: Send + Sync + Clone,
2084 E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
2085{
2086 fn new(parser: P, func: F) -> Self {
2087 Self { parser, func }
2088 }
2089}
2090
2091impl<P, F, T, E> TypedValueParser for TryMapValueParser<P, F>
2092where
2093 P: TypedValueParser,
2094 P::Value: Send + Sync + Clone,
2095 F: Fn(P::Value) -> Result<T, E> + Clone + Send + Sync + 'static,
2096 T: Send + Sync + Clone,
2097 E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
2098{
2099 type Value = T;
2100
2101 fn parse_ref(
2102 &self,
2103 cmd: &crate::Command,
2104 arg: Option<&crate::Arg>,
2105 value: &std::ffi::OsStr,
2106 ) -> Result<Self::Value, crate::Error> {
2107 let mid_value = match self.parser.parse_ref(cmd, arg, value) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!(self.parser.parse_ref(cmd, arg, value));
2108 let value = match (self.func)(mid_value).map_err(|e|
{
let arg =
arg.map(|a|
a.to_string()).unwrap_or_else(|| "...".to_owned());
crate::Error::value_validation(arg,
value.to_string_lossy().into_owned(),
e.into()).with_cmd(cmd)
}) {
Ok(val) => val,
Err(err) => { return Err(err); }
}ok!((self.func)(mid_value).map_err(|e| {
2109 let arg = arg
2110 .map(|a| a.to_string())
2111 .unwrap_or_else(|| "...".to_owned());
2112 crate::Error::value_validation(arg, value.to_string_lossy().into_owned(), e.into())
2113 .with_cmd(cmd)
2114 }));
2115 Ok(value)
2116 }
2117
2118 fn possible_values(
2119 &self,
2120 ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
2121 self.parser.possible_values()
2122 }
2123}
2124
2125#[derive(#[automatically_derived]
impl ::core::clone::Clone for UnknownArgumentValueParser {
#[inline]
fn clone(&self) -> UnknownArgumentValueParser {
UnknownArgumentValueParser {
arg: ::core::clone::Clone::clone(&self.arg),
suggestions: ::core::clone::Clone::clone(&self.suggestions),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for UnknownArgumentValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"UnknownArgumentValueParser", "arg", &self.arg, "suggestions",
&&self.suggestions)
}
}Debug)]
2159pub struct UnknownArgumentValueParser {
2160 arg: Option<Str>,
2161 suggestions: Vec<StyledStr>,
2162}
2163
2164impl UnknownArgumentValueParser {
2165 pub fn suggest_arg(arg: impl Into<Str>) -> Self {
2167 Self {
2168 arg: Some(arg.into()),
2169 suggestions: Default::default(),
2170 }
2171 }
2172
2173 pub fn suggest(text: impl Into<StyledStr>) -> Self {
2175 Self {
2176 arg: Default::default(),
2177 suggestions: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[text.into()]))vec![text.into()],
2178 }
2179 }
2180
2181 pub fn and_suggest(mut self, text: impl Into<StyledStr>) -> Self {
2183 self.suggestions.push(text.into());
2184 self
2185 }
2186}
2187
2188impl TypedValueParser for UnknownArgumentValueParser {
2189 type Value = String;
2190
2191 fn parse_ref(
2192 &self,
2193 cmd: &crate::Command,
2194 arg: Option<&crate::Arg>,
2195 value: &std::ffi::OsStr,
2196 ) -> Result<Self::Value, crate::Error> {
2197 TypedValueParser::parse_ref_(self, cmd, arg, value, ValueSource::CommandLine)
2198 }
2199
2200 fn parse_ref_(
2201 &self,
2202 cmd: &crate::Command,
2203 arg: Option<&crate::Arg>,
2204 _value: &std::ffi::OsStr,
2205 source: ValueSource,
2206 ) -> Result<Self::Value, crate::Error> {
2207 match source {
2208 ValueSource::DefaultValue => {
2209 TypedValueParser::parse_ref_(&StringValueParser::new(), cmd, arg, _value, source)
2210 }
2211 ValueSource::EnvVariable | ValueSource::CommandLine => {
2212 let arg = match arg {
2213 Some(arg) => arg.to_string(),
2214 None => "..".to_owned(),
2215 };
2216 let err = crate::Error::unknown_argument(
2217 cmd,
2218 arg,
2219 self.arg.as_ref().map(|s| (s.as_str().to_owned(), None)),
2220 false,
2221 crate::output::Usage::new(cmd).create_usage_with_title(&[]),
2222 );
2223 #[cfg(feature = "error-context")]
2224 let err = {
2225 if true {
match (&err.get(crate::error::ContextKind::Suggested), &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("Assuming `Error::unknown_argument` doesn\'t apply any `Suggested` so we can without caution")));
}
}
};
};debug_assert_eq!(
2226 err.get(crate::error::ContextKind::Suggested),
2227 None,
2228 "Assuming `Error::unknown_argument` doesn't apply any `Suggested` so we can without caution"
2229 );
2230 err.insert_context_unchecked(
2231 crate::error::ContextKind::Suggested,
2232 crate::error::ContextValue::StyledStrs(self.suggestions.clone()),
2233 )
2234 };
2235 Err(err)
2236 }
2237 }
2238 }
2239}
2240
2241pub trait ValueParserFactory {
2277 type Parser;
2282
2283 fn value_parser() -> Self::Parser;
2285}
2286impl ValueParserFactory for String {
2287 type Parser = ValueParser;
2288 fn value_parser() -> Self::Parser {
2289 ValueParser::string() }
2291}
2292impl ValueParserFactory for Box<str> {
2293 type Parser = MapValueParser<StringValueParser, fn(String) -> Box<str>>;
2294 fn value_parser() -> Self::Parser {
2295 StringValueParser::new().map(String::into_boxed_str)
2296 }
2297}
2298impl ValueParserFactory for std::ffi::OsString {
2299 type Parser = ValueParser;
2300 fn value_parser() -> Self::Parser {
2301 ValueParser::os_string() }
2303}
2304impl ValueParserFactory for Box<std::ffi::OsStr> {
2305 type Parser =
2306 MapValueParser<OsStringValueParser, fn(std::ffi::OsString) -> Box<std::ffi::OsStr>>;
2307 fn value_parser() -> Self::Parser {
2308 OsStringValueParser::new().map(std::ffi::OsString::into_boxed_os_str)
2309 }
2310}
2311impl ValueParserFactory for std::path::PathBuf {
2312 type Parser = ValueParser;
2313 fn value_parser() -> Self::Parser {
2314 ValueParser::path_buf() }
2316}
2317impl ValueParserFactory for Box<std::path::Path> {
2318 type Parser =
2319 MapValueParser<PathBufValueParser, fn(std::path::PathBuf) -> Box<std::path::Path>>;
2320 fn value_parser() -> Self::Parser {
2321 PathBufValueParser::new().map(std::path::PathBuf::into_boxed_path)
2322 }
2323}
2324impl ValueParserFactory for bool {
2325 type Parser = ValueParser;
2326 fn value_parser() -> Self::Parser {
2327 ValueParser::bool() }
2329}
2330impl ValueParserFactory for u8 {
2331 type Parser = RangedI64ValueParser<u8>;
2332 fn value_parser() -> Self::Parser {
2333 let start: i64 = u8::MIN.into();
2334 let end: i64 = u8::MAX.into();
2335 RangedI64ValueParser::new().range(start..=end)
2336 }
2337}
2338impl ValueParserFactory for i8 {
2339 type Parser = RangedI64ValueParser<i8>;
2340 fn value_parser() -> Self::Parser {
2341 let start: i64 = i8::MIN.into();
2342 let end: i64 = i8::MAX.into();
2343 RangedI64ValueParser::new().range(start..=end)
2344 }
2345}
2346impl ValueParserFactory for u16 {
2347 type Parser = RangedI64ValueParser<u16>;
2348 fn value_parser() -> Self::Parser {
2349 let start: i64 = u16::MIN.into();
2350 let end: i64 = u16::MAX.into();
2351 RangedI64ValueParser::new().range(start..=end)
2352 }
2353}
2354impl ValueParserFactory for i16 {
2355 type Parser = RangedI64ValueParser<i16>;
2356 fn value_parser() -> Self::Parser {
2357 let start: i64 = i16::MIN.into();
2358 let end: i64 = i16::MAX.into();
2359 RangedI64ValueParser::new().range(start..=end)
2360 }
2361}
2362impl ValueParserFactory for u32 {
2363 type Parser = RangedI64ValueParser<u32>;
2364 fn value_parser() -> Self::Parser {
2365 let start: i64 = u32::MIN.into();
2366 let end: i64 = u32::MAX.into();
2367 RangedI64ValueParser::new().range(start..=end)
2368 }
2369}
2370impl ValueParserFactory for i32 {
2371 type Parser = RangedI64ValueParser<i32>;
2372 fn value_parser() -> Self::Parser {
2373 let start: i64 = i32::MIN.into();
2374 let end: i64 = i32::MAX.into();
2375 RangedI64ValueParser::new().range(start..=end)
2376 }
2377}
2378impl ValueParserFactory for u64 {
2379 type Parser = RangedU64ValueParser<u64>;
2380 fn value_parser() -> Self::Parser {
2381 RangedU64ValueParser::new()
2382 }
2383}
2384impl ValueParserFactory for i64 {
2385 type Parser = RangedI64ValueParser<i64>;
2386 fn value_parser() -> Self::Parser {
2387 RangedI64ValueParser::new()
2388 }
2389}
2390impl<T> ValueParserFactory for std::num::Saturating<T>
2391where
2392 T: ValueParserFactory,
2393 <T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
2394 T: Send + Sync + Clone,
2395{
2396 type Parser =
2397 MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::num::Saturating<T>>;
2398 fn value_parser() -> Self::Parser {
2399 T::value_parser().map(std::num::Saturating)
2400 }
2401}
2402impl<T> ValueParserFactory for std::num::Wrapping<T>
2403where
2404 T: ValueParserFactory,
2405 <T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
2406 T: Send + Sync + Clone,
2407{
2408 type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::num::Wrapping<T>>;
2409 fn value_parser() -> Self::Parser {
2410 T::value_parser().map(std::num::Wrapping)
2411 }
2412}
2413impl<T> ValueParserFactory for Box<T>
2414where
2415 T: ValueParserFactory,
2416 <T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
2417 T: Send + Sync + Clone,
2418{
2419 type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> Box<T>>;
2420 fn value_parser() -> Self::Parser {
2421 T::value_parser().map(Box::new)
2422 }
2423}
2424impl<T> ValueParserFactory for std::sync::Arc<T>
2425where
2426 T: ValueParserFactory,
2427 <T as ValueParserFactory>::Parser: TypedValueParser<Value = T>,
2428 T: Send + Sync + Clone,
2429{
2430 type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::sync::Arc<T>>;
2431 fn value_parser() -> Self::Parser {
2432 T::value_parser().map(std::sync::Arc::new)
2433 }
2434}
2435
2436#[doc(hidden)]
2437#[derive(#[automatically_derived]
#[allow(non_camel_case_types)]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for _infer_ValueParser_for<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"_infer_ValueParser_for", &&self.0)
}
}Debug)]
2438#[allow(non_camel_case_types)]
2439pub struct _infer_ValueParser_for<T>(std::marker::PhantomData<T>);
2440
2441impl<T> _infer_ValueParser_for<T> {
2442 #[doc(hidden)]
2443 #[allow(clippy::new_without_default)]
2444 pub fn new() -> Self {
2445 Self(Default::default())
2446 }
2447}
2448
2449#[doc(hidden)]
2453#[derive(#[automatically_derived]
impl ::core::fmt::Debug for _AnonymousValueParser {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"_AnonymousValueParser", &&self.0)
}
}Debug)]
2454pub struct _AnonymousValueParser(ValueParser);
2455
2456#[doc(hidden)]
2457pub mod impl_prelude {
2458 use super::*;
2459
2460 #[doc(hidden)]
2461 #[allow(non_camel_case_types)]
2462 pub trait _impls_ValueParserFactory: private::_impls_ValueParserFactorySealed {
2463 type Parser;
2464 fn value_parser(&self) -> Self::Parser;
2465 }
2466 impl<P: ValueParserFactory> _impls_ValueParserFactory for &&&&&&_infer_ValueParser_for<P> {
2467 type Parser = P::Parser;
2468 fn value_parser(&self) -> Self::Parser {
2469 P::value_parser()
2470 }
2471 }
2472
2473 #[doc(hidden)]
2474 #[allow(non_camel_case_types)]
2475 pub trait _impls_ValueEnum: private::_impls_ValueEnumSealed {
2476 type Output;
2477
2478 fn value_parser(&self) -> Self::Output;
2479 }
2480 impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> _impls_ValueEnum
2481 for &&&&&_infer_ValueParser_for<E>
2482 {
2483 type Output = EnumValueParser<E>;
2484
2485 fn value_parser(&self) -> Self::Output {
2486 EnumValueParser::<E>::new()
2487 }
2488 }
2489
2490 #[doc(hidden)]
2491 #[allow(non_camel_case_types)]
2492 pub trait _impls_From_OsString: private::_impls_From_OsStringSealed {
2493 fn value_parser(&self) -> _AnonymousValueParser;
2494 }
2495 impl<FromOsString> _impls_From_OsString for &&&&_infer_ValueParser_for<FromOsString>
2496 where
2497 FromOsString: From<std::ffi::OsString> + std::any::Any + Clone + Send + Sync + 'static,
2498 {
2499 fn value_parser(&self) -> _AnonymousValueParser {
2500 _AnonymousValueParser(
2501 OsStringValueParser::new()
2502 .map(|s| FromOsString::from(s))
2503 .into(),
2504 )
2505 }
2506 }
2507
2508 #[doc(hidden)]
2509 #[allow(non_camel_case_types)]
2510 pub trait _impls_From_OsStr: private::_impls_From_OsStrSealed {
2511 fn value_parser(&self) -> _AnonymousValueParser;
2512 }
2513 impl<FromOsStr> _impls_From_OsStr for &&&_infer_ValueParser_for<FromOsStr>
2514 where
2515 FromOsStr:
2516 for<'s> From<&'s std::ffi::OsStr> + std::any::Any + Clone + Send + Sync + 'static,
2517 {
2518 fn value_parser(&self) -> _AnonymousValueParser {
2519 _AnonymousValueParser(
2520 OsStringValueParser::new()
2521 .map(|s| FromOsStr::from(&s))
2522 .into(),
2523 )
2524 }
2525 }
2526
2527 #[doc(hidden)]
2528 #[allow(non_camel_case_types)]
2529 pub trait _impls_From_String: private::_impls_From_StringSealed {
2530 fn value_parser(&self) -> _AnonymousValueParser;
2531 }
2532 impl<FromString> _impls_From_String for &&_infer_ValueParser_for<FromString>
2533 where
2534 FromString: From<String> + std::any::Any + Clone + Send + Sync + 'static,
2535 {
2536 fn value_parser(&self) -> _AnonymousValueParser {
2537 _AnonymousValueParser(StringValueParser::new().map(|s| FromString::from(s)).into())
2538 }
2539 }
2540
2541 #[doc(hidden)]
2542 #[allow(non_camel_case_types)]
2543 pub trait _impls_From_str: private::_impls_From_strSealed {
2544 fn value_parser(&self) -> _AnonymousValueParser;
2545 }
2546 impl<FromStr> _impls_From_str for &_infer_ValueParser_for<FromStr>
2547 where
2548 FromStr: for<'s> From<&'s str> + std::any::Any + Clone + Send + Sync + 'static,
2549 {
2550 fn value_parser(&self) -> _AnonymousValueParser {
2551 _AnonymousValueParser(StringValueParser::new().map(|s| FromStr::from(&s)).into())
2552 }
2553 }
2554
2555 #[doc(hidden)]
2556 #[allow(non_camel_case_types)]
2557 pub trait _impls_FromStr: private::_impls_FromStrSealed {
2558 fn value_parser(&self) -> _AnonymousValueParser;
2559 }
2560 impl<Parse> _impls_FromStr for _infer_ValueParser_for<Parse>
2561 where
2562 Parse: std::str::FromStr + std::any::Any + Clone + Send + Sync + 'static,
2563 <Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
2564 {
2565 fn value_parser(&self) -> _AnonymousValueParser {
2566 let func: fn(&str) -> Result<Parse, <Parse as std::str::FromStr>::Err> =
2567 Parse::from_str;
2568 _AnonymousValueParser(ValueParser::new(func))
2569 }
2570 }
2571}
2572
2573#[macro_export]
2626macro_rules! value_parser {
2627 ($name:ty) => {{
2628 use $crate::builder::impl_prelude::*;
2629 let auto = $crate::builder::_infer_ValueParser_for::<$name>::new();
2630 (&&&&&&auto).value_parser()
2631 }};
2632}
2633
2634mod private {
2635 use super::*;
2636
2637 #[allow(non_camel_case_types)]
2638 pub trait _impls_ValueParserFactorySealed {}
2639 impl<P: ValueParserFactory> _impls_ValueParserFactorySealed for &&&&&&_infer_ValueParser_for<P> {}
2640
2641 #[allow(non_camel_case_types)]
2642 pub trait _impls_ValueEnumSealed {}
2643 impl<E: crate::ValueEnum> _impls_ValueEnumSealed for &&&&&_infer_ValueParser_for<E> {}
2644
2645 #[allow(non_camel_case_types)]
2646 pub trait _impls_From_OsStringSealed {}
2647 impl<FromOsString> _impls_From_OsStringSealed for &&&&_infer_ValueParser_for<FromOsString> where
2648 FromOsString: From<std::ffi::OsString> + std::any::Any + Send + Sync + 'static
2649 {
2650 }
2651
2652 #[allow(non_camel_case_types)]
2653 pub trait _impls_From_OsStrSealed {}
2654 impl<FromOsStr> _impls_From_OsStrSealed for &&&_infer_ValueParser_for<FromOsStr> where
2655 FromOsStr: for<'s> From<&'s std::ffi::OsStr> + std::any::Any + Send + Sync + 'static
2656 {
2657 }
2658
2659 #[allow(non_camel_case_types)]
2660 pub trait _impls_From_StringSealed {}
2661 impl<FromString> _impls_From_StringSealed for &&_infer_ValueParser_for<FromString> where
2662 FromString: From<String> + std::any::Any + Send + Sync + 'static
2663 {
2664 }
2665
2666 #[allow(non_camel_case_types)]
2667 pub trait _impls_From_strSealed {}
2668 impl<FromStr> _impls_From_strSealed for &_infer_ValueParser_for<FromStr> where
2669 FromStr: for<'s> From<&'s str> + std::any::Any + Send + Sync + 'static
2670 {
2671 }
2672
2673 #[allow(non_camel_case_types)]
2674 pub trait _impls_FromStrSealed {}
2675 impl<Parse> _impls_FromStrSealed for _infer_ValueParser_for<Parse>
2676 where
2677 Parse: std::str::FromStr + std::any::Any + Send + Sync + 'static,
2678 <Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
2679 {
2680 }
2681}
2682
2683#[cfg(test)]
2684mod test {
2685 use super::*;
2686
2687 #[test]
2688 fn ensure_typed_applies_to_parse() {
2689 fn parse(_: &str) -> Result<usize, std::io::Error> {
2690 Ok(10)
2691 }
2692 let cmd = crate::Command::new("cmd");
2693 let arg = None;
2694 assert_eq!(
2695 TypedValueParser::parse_ref(&parse, &cmd, arg, std::ffi::OsStr::new("foo")).unwrap(),
2696 10
2697 );
2698 }
2699}