fix!: Remove lifetime from PossibleValue

Another step towards #1041

This isn't the long term type for `PossibleValue::help`, I just wanted
to get the lifetime out of the way first before figuring out how help
will work.
This commit is contained in:
Ed Page 2022-08-15 14:18:38 -05:00
parent 99d3bb8f74
commit 8cc164dafb
22 changed files with 104 additions and 92 deletions

View file

@ -231,7 +231,7 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES
Self::Menu, Self::Menu,
] ]
} }
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>> { fn to_possible_value(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
match self { match self {
Self::Normal => { Self::Normal => {
let value = "9"; let value = "9";

View file

@ -127,7 +127,7 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
} }
/// Get the possible values for completion /// Get the possible values for completion
pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::builder::PossibleValue<'help>>> { pub fn possible_values(a: &Arg<'_>) -> Option<Vec<clap::builder::PossibleValue>> {
if !a.get_num_args().expect("built").takes_values() { if !a.get_num_args().expect("built").takes_values() {
None None
} else { } else {

View file

@ -1,6 +1,5 @@
use std::{fmt::Write as _, io::Write}; use std::{fmt::Write as _, io::Write};
use clap::builder::PossibleValue;
use clap::*; use clap::*;
use crate::generator::{utils, Generator}; use crate::generator::{utils, Generator};
@ -180,7 +179,7 @@ fn vals_for(o: &Arg) -> String {
"$(compgen -W \"{}\" -- \"${{cur}}\")", "$(compgen -W \"{}\" -- \"${{cur}}\")",
vals.iter() vals.iter()
.filter(|pv| !pv.is_hide_set()) .filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name) .map(|n| n.get_name().as_str())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" ") .join(" ")
) )

View file

@ -164,7 +164,8 @@ fn value_completion(option: &Arg) -> String {
Some(format!( Some(format!(
"{}\t{}", "{}\t{}",
escape_string(value.get_name()).as_str(), escape_string(value.get_name()).as_str(),
escape_string(value.get_help().unwrap_or_default()).as_str() escape_string(value.get_help().map(|s| s.as_str()).unwrap_or_default())
.as_str()
)) ))
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()

View file

@ -57,7 +57,7 @@ impl ValueEnum for Shell {
] ]
} }
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> { fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
Some(match self { Some(match self {
Shell::Bash => PossibleValue::new("bash"), Shell::Bash => PossibleValue::new("bash"),
Shell::Elvish => PossibleValue::new("elvish"), Shell::Elvish => PossibleValue::new("elvish"),

View file

@ -1,6 +1,5 @@
use std::io::Write; use std::io::Write;
use clap::builder::PossibleValue;
use clap::*; use clap::*;
use crate::generator::{utils, Generator}; use crate::generator::{utils, Generator};
@ -375,8 +374,12 @@ fn value_completion(arg: &Arg) -> Option<String> {
} else { } else {
Some(format!( Some(format!(
r#"{name}\:"{tooltip}""#, r#"{name}\:"{tooltip}""#,
name = escape_value(value.get_name()), name = escape_value(value.get_name().as_str()),
tooltip = value.get_help().map(escape_help).unwrap_or_default() tooltip = value
.get_help()
.map(|s| s.as_str())
.map(escape_help)
.unwrap_or_default()
)) ))
} }
}) })
@ -389,7 +392,7 @@ fn value_completion(arg: &Arg) -> Option<String> {
values values
.iter() .iter()
.filter(|pv| !pv.is_hide_set()) .filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name) .map(|n| n.get_name().as_str())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" ") .join(" ")
)) ))

View file

@ -499,7 +499,7 @@ impl Attrs {
quote_spanned!(ident.span()=> { quote_spanned!(ident.span()=> {
{ {
let val: #ty = #val; let val: #ty = #val;
clap::ValueEnum::to_possible_value(&val).unwrap().get_name() clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
} }
}) })
} else { } else {
@ -544,17 +544,15 @@ impl Attrs {
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) { let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
quote_spanned!(ident.span()=> { quote_spanned!(ident.span()=> {
{ {
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> Vec<&'static str> fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
where where
T: ::std::borrow::Borrow<#inner_type> T: ::std::borrow::Borrow<#inner_type>
{ {
iterable iterable
.into_iter() .into_iter()
.map(|val| { .map(|val| {
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name() clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name().to_owned()
}) })
.collect()
} }
iter_to_vals(#expr) iter_to_vals(#expr)
@ -603,7 +601,7 @@ impl Attrs {
quote_spanned!(ident.span()=> { quote_spanned!(ident.span()=> {
{ {
let val: #ty = #val; let val: #ty = #val;
clap::ValueEnum::to_possible_value(&val).unwrap().get_name() clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
} }
}) })
} else { } else {
@ -648,18 +646,15 @@ impl Attrs {
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) { let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
quote_spanned!(ident.span()=> { quote_spanned!(ident.span()=> {
{ {
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> Vec<&'static ::std::ffi::OsStr> fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
where where
T: ::std::borrow::Borrow<#inner_type> T: ::std::borrow::Borrow<#inner_type>
{ {
iterable iterable
.into_iter() .into_iter()
.map(|val| { .map(|val| {
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name() clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name().to_owned()
}) })
.map(::std::ffi::OsStr::new)
.collect()
} }
iter_to_vals(#expr) iter_to_vals(#expr)

View file

@ -114,7 +114,7 @@ fn gen_to_possible_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip(); let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip();
quote! { quote! {
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>> { fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
match self { match self {
#(Self::#variant => Some(#lit),)* #(Self::#variant => Some(#lit),)*
_ => None _ => None

View file

@ -82,7 +82,7 @@ pub fn value_enum(name: &Ident) {
fn from_str(_input: &str, _ignore_case: bool) -> ::std::result::Result<Self, String> { fn from_str(_input: &str, _ignore_case: bool) -> ::std::result::Result<Self, String> {
unimplemented!() unimplemented!()
} }
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>>{ fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue>{
unimplemented!() unimplemented!()
} }
} }

View file

@ -12,7 +12,7 @@ impl ValueEnum for Mode {
&[Mode::Fast, Mode::Slow] &[Mode::Fast, Mode::Slow]
} }
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> { fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
Some(match self { Some(match self {
Mode::Fast => PossibleValue::new("fast"), Mode::Fast => PossibleValue::new("fast"),
Mode::Slow => PossibleValue::new("slow"), Mode::Slow => PossibleValue::new("slow"),

View file

@ -3659,7 +3659,7 @@ impl<'help> Arg<'help> {
Some(longs) Some(longs)
} }
pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue<'help>> { pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue> {
if !self.is_takes_value_set() { if !self.is_takes_value_set() {
vec![] vec![]
} else { } else {

View file

@ -1,6 +1,7 @@
use std::{borrow::Cow, iter}; use std::{borrow::Cow, iter};
use crate::util::eq_ignore_case; use crate::util::eq_ignore_case;
use crate::Str;
/// A possible value of an argument. /// A possible value of an argument.
/// ///
@ -27,14 +28,14 @@ use crate::util::eq_ignore_case;
/// [hide]: PossibleValue::hide() /// [hide]: PossibleValue::hide()
/// [help]: PossibleValue::help() /// [help]: PossibleValue::help()
#[derive(Debug, Default, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct PossibleValue<'help> { pub struct PossibleValue {
name: &'help str, name: Str,
help: Option<&'help str>, help: Option<Str>,
aliases: Vec<&'help str>, // (name, visible) aliases: Vec<Str>, // (name, visible)
hide: bool, hide: bool,
} }
impl<'help> PossibleValue<'help> { impl PossibleValue {
/// Create a [`PossibleValue`] with its name. /// Create a [`PossibleValue`] with its name.
/// ///
/// The name will be used to decide whether this value was provided by the user to an argument. /// The name will be used to decide whether this value was provided by the user to an argument.
@ -52,9 +53,9 @@ impl<'help> PossibleValue<'help> {
/// [hidden]: PossibleValue::hide /// [hidden]: PossibleValue::hide
/// [possible value]: crate::builder::PossibleValuesParser /// [possible value]: crate::builder::PossibleValuesParser
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values() /// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
pub fn new(name: &'help str) -> Self { pub fn new(name: impl Into<Str>) -> Self {
PossibleValue { PossibleValue {
name, name: name.into(),
..Default::default() ..Default::default()
} }
} }
@ -74,8 +75,8 @@ impl<'help> PossibleValue<'help> {
/// ``` /// ```
#[inline] #[inline]
#[must_use] #[must_use]
pub fn help(mut self, help: &'help str) -> Self { pub fn help(mut self, help: impl Into<Str>) -> Self {
self.help = Some(help); self.help = Some(help.into());
self self
} }
@ -111,8 +112,8 @@ impl<'help> PossibleValue<'help> {
/// # ; /// # ;
/// ``` /// ```
#[must_use] #[must_use]
pub fn alias(mut self, name: &'help str) -> Self { pub fn alias(mut self, name: impl Into<Str>) -> Self {
self.aliases.push(name); self.aliases.push(name.into());
self self
} }
@ -127,35 +128,32 @@ impl<'help> PossibleValue<'help> {
/// # ; /// # ;
/// ``` /// ```
#[must_use] #[must_use]
pub fn aliases<I>(mut self, names: I) -> Self pub fn aliases(mut self, names: impl IntoIterator<Item = impl Into<Str>>) -> Self {
where self.aliases.extend(names.into_iter().map(|a| a.into()));
I: IntoIterator<Item = &'help str>,
{
self.aliases.extend(names.into_iter());
self self
} }
} }
/// Reflection /// Reflection
impl<'help> PossibleValue<'help> { impl PossibleValue {
/// Get the name of the argument value /// Get the name of the argument value
#[inline] #[inline]
pub fn get_name(&self) -> &'help str { pub fn get_name(&self) -> &Str {
self.name &self.name
} }
/// Get the help specified for this argument, if any /// Get the help specified for this argument, if any
#[inline] #[inline]
pub fn get_help(&self) -> Option<&'help str> { pub fn get_help(&self) -> Option<&Str> {
self.help self.help.as_ref()
} }
/// Get the help specified for this argument, if any and the argument /// Get the help specified for this argument, if any and the argument
/// value is not hidden /// value is not hidden
#[inline] #[inline]
pub(crate) fn get_visible_help(&self) -> Option<&'help str> { pub(crate) fn get_visible_help(&self) -> Option<&str> {
if !self.hide { if !self.hide {
self.help self.help.as_deref()
} else { } else {
None None
} }
@ -174,12 +172,12 @@ impl<'help> PossibleValue<'help> {
/// Get the name if argument value is not hidden, `None` otherwise, /// Get the name if argument value is not hidden, `None` otherwise,
/// but wrapped in quotes if it contains whitespace /// but wrapped in quotes if it contains whitespace
pub(crate) fn get_visible_quoted_name(&self) -> Option<Cow<'help, str>> { pub(crate) fn get_visible_quoted_name(&self) -> Option<Cow<'_, str>> {
if !self.hide { if !self.hide {
Some(if self.name.contains(char::is_whitespace) { Some(if self.name.contains(char::is_whitespace) {
format!("{:?}", self.name).into() format!("{:?}", self.name).into()
} else { } else {
self.name.into() self.name.as_str().into()
}) })
} else { } else {
None None
@ -189,8 +187,8 @@ impl<'help> PossibleValue<'help> {
/// Returns all valid values of the argument value. /// Returns all valid values of the argument value.
/// ///
/// Namely the name and all aliases. /// Namely the name and all aliases.
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &'help str> + '_ { pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
iter::once(&self.name).chain(&self.aliases).copied() iter::once(&self.name).chain(self.aliases.iter())
} }
/// Tests if the value is valid for this argument value /// Tests if the value is valid for this argument value
@ -212,21 +210,15 @@ impl<'help> PossibleValue<'help> {
pub fn matches(&self, value: &str, ignore_case: bool) -> bool { pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
if ignore_case { if ignore_case {
self.get_name_and_aliases() self.get_name_and_aliases()
.any(|name| eq_ignore_case(name, value)) .any(|name| eq_ignore_case(name.as_str(), value))
} else { } else {
self.get_name_and_aliases().any(|name| name == value) self.get_name_and_aliases().any(|name| name == value)
} }
} }
} }
impl<'help> From<&'help str> for PossibleValue<'help> { impl<S: Into<Str>> From<S> for PossibleValue {
fn from(s: &'help str) -> Self { fn from(s: S) -> Self {
Self::new(s)
}
}
impl<'help> From<&'_ &'help str> for PossibleValue<'help> {
fn from(s: &'_ &'help str) -> Self {
Self::new(s) Self::new(s)
} }
} }

View file

@ -243,7 +243,7 @@ impl ValueParser {
/// applications like errors and completion. /// applications like errors and completion.
pub fn possible_values( pub fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
self.any_value_parser().possible_values() self.any_value_parser().possible_values()
} }
@ -501,7 +501,7 @@ impl From<std::ops::RangeFull> for ValueParser {
/// ``` /// ```
impl<P, const C: usize> From<[P; C]> for ValueParser impl<P, const C: usize> From<[P; C]> for ValueParser
where where
P: Into<super::PossibleValue<'static>>, P: Into<super::PossibleValue>,
{ {
fn from(values: [P; C]) -> Self { fn from(values: [P; C]) -> Self {
let inner = PossibleValuesParser::from(values); let inner = PossibleValuesParser::from(values);
@ -554,7 +554,7 @@ trait AnyValueParser: Send + Sync + 'static {
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>>; ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>>;
fn clone_any(&self) -> Box<dyn AnyValueParser>; fn clone_any(&self) -> Box<dyn AnyValueParser>;
} }
@ -590,7 +590,7 @@ where
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
P::possible_values(self) P::possible_values(self)
} }
@ -632,7 +632,7 @@ pub trait TypedValueParser: Clone + Send + Sync + 'static {
/// applications like errors and completion. /// applications like errors and completion.
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
None None
} }
@ -876,7 +876,7 @@ impl Default for PathBufValueParser {
/// &[Self::Always, Self::Auto, Self::Never] /// &[Self::Always, Self::Auto, Self::Never]
/// } /// }
/// ///
/// fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue<'a>> { /// fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
/// match self { /// match self {
/// Self::Always => Some(clap::builder::PossibleValue::new("always")), /// Self::Always => Some(clap::builder::PossibleValue::new("always")),
/// Self::Auto => Some(clap::builder::PossibleValue::new("auto")), /// Self::Auto => Some(clap::builder::PossibleValue::new("auto")),
@ -936,7 +936,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
.iter() .iter()
.filter_map(|v| v.to_possible_value()) .filter_map(|v| v.to_possible_value())
.filter(|v| !v.is_hide_set()) .filter(|v| !v.is_hide_set())
.map(|v| v.get_name()) .map(|v| v.get_name().as_str().to_owned())
.collect::<Vec<_>>() .collect::<Vec<_>>()
}; };
@ -971,7 +971,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new( Some(Box::new(
E::value_variants() E::value_variants()
.iter() .iter()
@ -1022,7 +1022,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueP
/// assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("never")).unwrap(), "never"); /// assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("never")).unwrap(), "never");
/// ``` /// ```
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct PossibleValuesParser(Vec<super::PossibleValue<'static>>); pub struct PossibleValuesParser(Vec<super::PossibleValue>);
impl PossibleValuesParser { impl PossibleValuesParser {
/// Verify the value is from an enumerated set pf [`PossibleValue`][crate::builder::PossibleValue]. /// Verify the value is from an enumerated set pf [`PossibleValue`][crate::builder::PossibleValue].
@ -1064,7 +1064,7 @@ impl TypedValueParser for PossibleValuesParser {
.0 .0
.iter() .iter()
.filter(|v| !v.is_hide_set()) .filter(|v| !v.is_hide_set())
.map(crate::builder::PossibleValue::get_name) .map(|v| v.get_name().as_str().to_owned())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Err(crate::Error::invalid_value( Err(crate::Error::invalid_value(
@ -1079,7 +1079,7 @@ impl TypedValueParser for PossibleValuesParser {
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(self.0.iter().cloned())) Some(Box::new(self.0.iter().cloned()))
} }
} }
@ -1087,7 +1087,7 @@ impl TypedValueParser for PossibleValuesParser {
impl<I, T> From<I> for PossibleValuesParser impl<I, T> From<I> for PossibleValuesParser
where where
I: IntoIterator<Item = T>, I: IntoIterator<Item = T>,
T: Into<super::PossibleValue<'static>>, T: Into<super::PossibleValue>,
{ {
fn from(values: I) -> Self { fn from(values: I) -> Self {
Self(values.into_iter().map(|t| t.into()).collect()) Self(values.into_iter().map(|t| t.into()).collect())
@ -1501,7 +1501,7 @@ impl BoolValueParser {
Self {} Self {}
} }
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue<'static>> { fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
["true", "false"] ["true", "false"]
.iter() .iter()
.copied() .copied()
@ -1525,7 +1525,7 @@ impl TypedValueParser for BoolValueParser {
} else { } else {
// Intentionally showing hidden as we hide all of them // Intentionally showing hidden as we hide all of them
let possible_vals = Self::possible_values() let possible_vals = Self::possible_values()
.map(|v| v.get_name()) .map(|v| v.get_name().as_str().to_owned())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
return Err(crate::Error::invalid_value( return Err(crate::Error::invalid_value(
@ -1541,7 +1541,7 @@ impl TypedValueParser for BoolValueParser {
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(Self::possible_values())) Some(Box::new(Self::possible_values()))
} }
} }
@ -1600,7 +1600,7 @@ impl FalseyValueParser {
Self {} Self {}
} }
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue<'static>> { fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
crate::util::TRUE_LITERALS crate::util::TRUE_LITERALS
.iter() .iter()
.chain(crate::util::FALSE_LITERALS.iter()) .chain(crate::util::FALSE_LITERALS.iter())
@ -1634,7 +1634,7 @@ impl TypedValueParser for FalseyValueParser {
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(Self::possible_values())) Some(Box::new(Self::possible_values()))
} }
} }
@ -1697,7 +1697,7 @@ impl BoolishValueParser {
Self {} Self {}
} }
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue<'static>> { fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
crate::util::TRUE_LITERALS crate::util::TRUE_LITERALS
.iter() .iter()
.chain(crate::util::FALSE_LITERALS.iter()) .chain(crate::util::FALSE_LITERALS.iter())
@ -1733,7 +1733,7 @@ impl TypedValueParser for BoolishValueParser {
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(Self::possible_values())) Some(Box::new(Self::possible_values()))
} }
} }
@ -1874,7 +1874,7 @@ where
fn possible_values( fn possible_values(
&self, &self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> { ) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
self.parser.possible_values() self.parser.possible_values()
} }
} }
@ -2130,7 +2130,7 @@ pub mod via_prelude {
/// # fn value_variants<'a>() -> &'a [Self] { /// # fn value_variants<'a>() -> &'a [Self] {
/// # &[Self::Always, Self::Auto, Self::Never] /// # &[Self::Always, Self::Auto, Self::Never]
/// # } /// # }
/// # fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue<'a>> { /// # fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
/// # match self { /// # match self {
/// # Self::Always => Some(clap::builder::PossibleValue::new("always")), /// # Self::Always => Some(clap::builder::PossibleValue::new("always")),
/// # Self::Auto => Some(clap::builder::PossibleValue::new("auto")), /// # Self::Auto => Some(clap::builder::PossibleValue::new("auto")),

View file

@ -387,7 +387,7 @@ pub trait ValueEnum: Sized + Clone {
/// The canonical argument value. /// The canonical argument value.
/// ///
/// The value is `None` for skipped variants. /// The value is `None` for skipped variants.
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>>; fn to_possible_value(&self) -> Option<PossibleValue>;
} }
impl<T: Parser> Parser for Box<T> { impl<T: Parser> Parser for Box<T> {

View file

@ -243,7 +243,7 @@ impl Error {
]) ])
} }
pub(crate) fn empty_value(cmd: &Command, good_vals: &[&str], arg: String) -> Self { pub(crate) fn empty_value(cmd: &Command, good_vals: &[String], arg: String) -> Self {
Self::invalid_value(cmd, "".to_owned(), good_vals, arg) Self::invalid_value(cmd, "".to_owned(), good_vals, arg)
} }
@ -259,7 +259,7 @@ impl Error {
pub(crate) fn invalid_value( pub(crate) fn invalid_value(
cmd: &Command, cmd: &Command,
bad_val: String, bad_val: String,
good_vals: &[&str], good_vals: &[String],
arg: String, arg: String,
) -> Self { ) -> Self {
let suggestion = suggestions::did_you_mean(&bad_val, good_vals.iter()).pop(); let suggestion = suggestions::did_you_mean(&bad_val, good_vals.iter()).pop();

View file

@ -110,7 +110,7 @@ pub use crate::util::color::ColorChoice;
pub(crate) use crate::util::color::ColorChoice; pub(crate) use crate::util::color::ColorChoice;
pub use crate::util::Id; pub use crate::util::Id;
pub use crate::util::OsStr; pub use crate::util::OsStr;
pub(crate) use crate::util::Str; pub use crate::util::Str;
pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum}; pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};

View file

@ -504,7 +504,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
self.none("\n")?; self.none("\n")?;
self.spaces(spaces)?; self.spaces(spaces)?;
self.none("- ")?; self.none("- ")?;
self.good(pv.get_name())?; self.good(pv.get_name().as_str())?;
if let Some(help) = pv.get_help() { if let Some(help) = pv.get_help() {
debug!("Help::help: Possible Value help"); debug!("Help::help: Possible Value help");

View file

@ -9,7 +9,6 @@ use clap_lex::RawOsStr;
use clap_lex::RawOsString; use clap_lex::RawOsString;
// Internal // Internal
use crate::builder::PossibleValue;
use crate::builder::{Arg, Command}; use crate::builder::{Arg, Command};
use crate::error::Error as ClapError; use crate::error::Error as ClapError;
use crate::error::Result as ClapResult; use crate::error::Result as ClapResult;
@ -1266,7 +1265,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
&super::get_possible_values_cli(arg) &super::get_possible_values_cli(arg)
.iter() .iter()
.filter(|pv| !pv.is_hide_set()) .filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name) .map(|n| n.get_name().as_str().to_owned())
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
arg.to_string(), arg.to_string(),
)); ));

View file

@ -45,7 +45,7 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
&get_possible_values_cli(o) &get_possible_values_cli(o)
.iter() .iter()
.filter(|pv| !pv.is_hide_set()) .filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name) .map(|n| n.get_name().as_str().to_owned())
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
o.to_string(), o.to_string(),
)); ));
@ -455,7 +455,7 @@ impl Conflicts {
} }
} }
pub(crate) fn get_possible_values_cli<'help>(a: &Arg<'help>) -> Vec<PossibleValue<'help>> { pub(crate) fn get_possible_values_cli(a: &Arg<'_>) -> Vec<PossibleValue> {
if !a.is_takes_value_set() { if !a.is_takes_value_set() {
vec![] vec![]
} else { } else {

View file

@ -16,6 +16,7 @@ pub(crate) use self::flat_map::Entry;
pub(crate) use self::flat_map::FlatMap; pub(crate) use self::flat_map::FlatMap;
pub(crate) use self::flat_set::FlatSet; pub(crate) use self::flat_set::FlatSet;
pub(crate) use self::graph::ChildGraph; pub(crate) use self::graph::ChildGraph;
pub(crate) use self::str::Inner as StrInner;
pub(crate) use self::str_to_bool::str_to_bool; pub(crate) use self::str_to_bool::str_to_bool;
pub(crate) use self::str_to_bool::FALSE_LITERALS; pub(crate) use self::str_to_bool::FALSE_LITERALS;
pub(crate) use self::str_to_bool::TRUE_LITERALS; pub(crate) use self::str_to_bool::TRUE_LITERALS;

View file

@ -40,6 +40,24 @@ impl From<&'_ OsStr> for OsStr {
} }
} }
impl From<crate::Str> for OsStr {
fn from(id: crate::Str) -> Self {
match id.into_inner() {
crate::util::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)),
crate::util::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())),
}
}
}
impl From<&'_ crate::Str> for OsStr {
fn from(id: &'_ crate::Str) -> Self {
match id.clone().into_inner() {
crate::util::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)),
crate::util::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())),
}
}
}
impl From<std::ffi::OsString> for OsStr { impl From<std::ffi::OsString> for OsStr {
fn from(name: std::ffi::OsString) -> Self { fn from(name: std::ffi::OsString) -> Self {
Self::from_string(name) Self::from_string(name)

View file

@ -23,6 +23,10 @@ impl Str {
} }
} }
pub(crate) fn into_inner(self) -> Inner {
self.name
}
/// Get the raw string of the `Str` /// Get the raw string of the `Str`
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
self.name.as_str() self.name.as_str()
@ -139,7 +143,7 @@ impl PartialEq<std::string::String> for Str {
} }
#[derive(Clone)] #[derive(Clone)]
enum Inner { pub(crate) enum Inner {
Static(&'static str), Static(&'static str),
Owned(Box<str>), Owned(Box<str>),
} }