From 5ebd15d0de7740567cbd27ddc2e42c40f4edc56e Mon Sep 17 00:00:00 2001 From: Corentin Henry Date: Sat, 21 Apr 2018 11:12:00 -0700 Subject: [PATCH] convert Result<(), OsString> -> Result<(), String> fix https://github.com/kbknapp/clap-rs/issues/848 --- src/app/validator.rs | 2 +- src/args/arg.rs | 106 ++++++++++++++++++++++--------------------- 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/src/app/validator.rs b/src/app/validator.rs index 0b897e567e83..4446e990f8c9 100644 --- a/src/app/validator.rs +++ b/src/app/validator.rs @@ -135,7 +135,7 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> { sdebugln!("error"); return Err(Error::value_validation( Some(arg), - (*e).to_string_lossy().to_string(), + (*e).to_string(), self.0.app.color(), )); } else { diff --git a/src/args/arg.rs b/src/args/arg.rs index d827826fdbb7..997ecfcb09e8 100644 --- a/src/args/arg.rs +++ b/src/args/arg.rs @@ -1,23 +1,23 @@ +#[cfg(any(target_os = "windows", target_arch = "wasm32"))] +use osstringext::OsStrExt3; +use std::borrow::Cow; +use std::cmp::{Ord, Ordering}; #[cfg(feature = "yaml")] use std::collections::BTreeMap; -use std::rc::Rc; -use std::borrow::Cow; -use std::fmt::{self, Display, Formatter}; +use std::env; use std::ffi::{OsStr, OsString}; -#[cfg(any(target_os = "windows", target_arch = "wasm32"))] -use osstringext::OsStrExt3; +use std::fmt::{self, Display, Formatter}; #[cfg(not(any(target_os = "windows", target_arch = "wasm32")))] use std::os::unix::ffi::OsStrExt; -use std::env; -use std::cmp::{Ord, Ordering}; +use std::rc::Rc; use std::str; +use map::VecMap; #[cfg(feature = "yaml")] use yaml_rust::Yaml; -use map::VecMap; -use usage_parser::UsageParser; use args::settings::{ArgFlags, ArgSettings}; +use usage_parser::UsageParser; use INTERNAL_ERROR_MSG; /// The abstract representation of a command line argument. Used to set all the options and @@ -89,7 +89,7 @@ where #[doc(hidden)] pub validator: Option Result<(), String>>>, #[doc(hidden)] - pub validator_os: Option Result<(), OsString>>>, + pub validator_os: Option Result<(), String>>>, #[doc(hidden)] pub val_delim: Option, #[doc(hidden)] @@ -1497,7 +1497,6 @@ impl<'a, 'b> Arg<'a, 'b> { self } - /// Specifies a value that *stops* parsing multiple values of a give argument. By default when /// one sets [`multiple(true)`] on an argument, clap will continue parsing values for that /// argument until it reaches another valid argument, or one of the other more specific settings @@ -1831,10 +1830,13 @@ impl<'a, 'b> Arg<'a, 'b> { /// [`Err(String)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err /// [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html pub fn validator(mut self, f: F) -> Self - where F: Fn(String) -> Result + 'static, - E: ToString + where + F: Fn(String) -> Result + 'static, + E: ToString, { - self.validator = Some(Rc::new(move |s| f(s).map(|_| ()).map_err(|e| e.to_string()))); + self.validator = Some(Rc::new(move |s| { + f(s).map(|_| ()).map_err(|e| e.to_string()) + })); self } @@ -1848,9 +1850,9 @@ impl<'a, 'b> Arg<'a, 'b> { /// # use clap::{App, Arg}; /// # use std::ffi::{OsStr, OsString}; /// # use std::os::unix::ffi::OsStrExt; - /// fn has_ampersand(v: &OsStr) -> Result<(), OsString> { + /// fn has_ampersand(v: &OsStr) -> Result<(), String> { /// if v.as_bytes().iter().any(|b| *b == b'&') { return Ok(()); } - /// Err(OsString::from("The value did not contain the required & sigil")) + /// Err(String::from("The value did not contain the required & sigil")) /// } /// let res = App::new("prog") /// .arg(Arg::with_name("file") @@ -1869,7 +1871,8 @@ impl<'a, 'b> Arg<'a, 'b> { /// [`Err(String)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err /// [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html pub fn validator_os(mut self, f: F) -> Self - where F: Fn(&OsStr) -> Result + 'static + where + F: Fn(&OsStr) -> Result + 'static, { self.validator_os = Some(Rc::new(move |s| f(s).map(|_| ()))); self @@ -3175,7 +3178,10 @@ impl<'a, 'b> Arg<'a, 'b> { } /// **Deprecated** - #[deprecated(since="2.30.0", note="Use `Arg::setting(ArgSettings::AllowEmptyValues)` instead. Will be removed in v3.0-beta")] + #[deprecated( + since = "2.30.0", + note = "Use `Arg::setting(ArgSettings::AllowEmptyValues)` instead. Will be removed in v3.0-beta" + )] pub fn empty_values(mut self, ev: bool) -> Self { if ev { self.setting(ArgSettings::AllowEmptyValues) @@ -3747,7 +3753,7 @@ impl<'a, 'b> Arg<'a, 'b> { self.unset_setting(ArgSettings::MultipleOccurrences) } } - + /// Indicates that all parameters passed after this should not be parsed /// individually, but rather passed in their entirety. It is worth noting /// that setting this requires all values to come after a `--` to indicate they @@ -3767,9 +3773,7 @@ impl<'a, 'b> Arg<'a, 'b> { /// [`Arg::allow_hyphen_values(true)`]: ./struct.Arg.html#method.allow_hyphen_values /// [`Arg::last(true)`]: ./struct.Arg.html#method.last /// [`AppSettings::TrailingVarArg`]: ./enum.AppSettings.html#variant.TrailingVarArg - pub fn raw(self, raw: bool) -> Self { - self.multiple(raw).allow_hyphen_values(raw).last(raw) - } + pub fn raw(self, raw: bool) -> Self { self.multiple(raw).allow_hyphen_values(raw).last(raw) } /// Hides an argument from short help message output. /// @@ -3777,7 +3781,7 @@ impl<'a, 'b> Arg<'a, 'b> { /// /// **NOTE:** Setting this option will cause next-line-help output style to be used /// when long help (`--help`) is called. - /// + /// /// # Examples /// /// ```rust @@ -3812,9 +3816,9 @@ impl<'a, 'b> Arg<'a, 'b> { /// -h, --help Prints help information /// -V, --version Prints version information /// ``` - /// + /// /// However, when --help is called - /// + /// /// ```rust /// # use clap::{App, Arg}; /// let m = App::new("prog") @@ -3826,24 +3830,24 @@ impl<'a, 'b> Arg<'a, 'b> { /// "prog", "--help" /// ]); /// ``` - /// + /// /// Then the following would be displayed - /// + /// /// ```notrust /// helptest - /// + /// /// USAGE: /// helptest [FLAGS] - /// + /// /// FLAGS: /// --config Some help text describing the --config arg /// -h, --help Prints help information /// -V, --version Prints version information /// ``` pub fn hidden_short_help(self, hide: bool) -> Self { - if hide { + if hide { self.set(ArgSettings::HiddenShortHelp) - } else { + } else { self.unset(ArgSettings::HiddenShortHelp) } } @@ -3854,7 +3858,7 @@ impl<'a, 'b> Arg<'a, 'b> { /// /// **NOTE:** Setting this option will cause next-line-help output style to be used /// when long help (`--help`) is called. - /// + /// /// # Examples /// /// ```rust @@ -3889,9 +3893,9 @@ impl<'a, 'b> Arg<'a, 'b> { /// -h, --help Prints help information /// -V, --version Prints version information /// ``` - /// + /// /// However, when -h is called - /// + /// /// ```rust /// # use clap::{App, Arg}; /// let m = App::new("prog") @@ -3903,15 +3907,15 @@ impl<'a, 'b> Arg<'a, 'b> { /// "prog", "-h" /// ]); /// ``` - /// + /// /// Then the following would be displayed - /// + /// /// ```notrust /// helptest - /// + /// /// USAGE: /// helptest [FLAGS] - /// + /// /// FLAGS: /// --config Some help text describing the --config arg /// -h, --help Prints help information @@ -3963,7 +3967,8 @@ impl<'a, 'b> Arg<'a, 'b> { #[doc(hidden)] pub fn _build(&mut self) { if (self.is_set(ArgSettings::UseValueDelimiter) - || self.is_set(ArgSettings::RequireDelimiter)) && self.val_delim.is_none() { + || self.is_set(ArgSettings::RequireDelimiter)) && self.val_delim.is_none() + { self.val_delim = Some(','); } if self.index.is_some() || (self.short.is_none() && self.long.is_none()) { @@ -4004,8 +4009,9 @@ impl<'a, 'b> Arg<'a, 'b> { let mult_vals = self.val_names .as_ref() .map_or(true, |names| names.len() < 2); - if (self.is_set(ArgSettings::MultipleValues) || self.is_set(ArgSettings::MultipleOccurrences)) - && mult_vals { + if (self.is_set(ArgSettings::MultipleValues) + || self.is_set(ArgSettings::MultipleOccurrences)) && mult_vals + { "..." } else { "" @@ -4045,29 +4051,28 @@ impl<'a, 'b> Arg<'a, 'b> { // Deprecations // @TODO @v3-beta: remove impl<'a, 'b> Arg<'a, 'b> { - /// **Deprecated** - #[deprecated(since="2.30.0", note="Renamed to `Arg::setting`. Will be removed in v3.0-beta")] + #[deprecated(since = "2.30.0", note = "Renamed to `Arg::setting`. Will be removed in v3.0-beta")] pub fn set(mut self, s: ArgSettings) -> Self { self.setb(s); self } /// **Deprecated** - #[deprecated(since="2.30.0", note="Renamed to `Arg::unset_setting`. Will be removed in v3.0-beta")] + #[deprecated( + since = "2.30.0", note = "Renamed to `Arg::unset_setting`. Will be removed in v3.0-beta" + )] pub fn unset(mut self, s: ArgSettings) -> Self { self.unsetb(s); self } - /// **Deprecated** - #[deprecated(since="2.30.0", note="Use `Arg::from` instead. Will be removed in v3.0-beta")] + #[deprecated(since = "2.30.0", note = "Use `Arg::from` instead. Will be removed in v3.0-beta")] pub fn from_usage(u: &'a str) -> Self { let parser = UsageParser::from_usage(u); parser.parse() } - } impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for Arg<'a, 'b> { @@ -4103,8 +4108,7 @@ impl<'n, 'e> Display for Arg<'n, 'e> { } if self.settings.is_set(ArgSettings::MultipleValues) && (self.val_names.is_none() - || (self.val_names.is_some() - && self.val_names.as_ref().unwrap().len() == 1)) + || (self.val_names.is_some() && self.val_names.as_ref().unwrap().len() == 1)) { write!(f, "...")?; } @@ -4234,9 +4238,9 @@ impl<'n, 'e> fmt::Debug for Arg<'n, 'e> { // Flags #[cfg(test)] mod test { - use map::VecMap; - use args::settings::ArgSettings; use super::Arg; + use args::settings::ArgSettings; + use map::VecMap; #[test] fn flag_display() {