Skip to content

Commit

Permalink
Merge pull request #4107 from epage/help
Browse files Browse the repository at this point in the history
fix: Don't pollute root with str types
  • Loading branch information
epage authored Aug 23, 2022
2 parents c6b8a7b + 276c75c commit d4ec9ca
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 53 deletions.
6 changes: 3 additions & 3 deletions clap_complete/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub fn generate_to<G, S, T>(
) -> Result<PathBuf, Error>
where
G: Generator,
S: Into<clap::Str>,
S: Into<clap::builder::Str>,
T: Into<OsString>,
{
cmd.set_bin_name(bin_name);
Expand Down Expand Up @@ -223,7 +223,7 @@ where
pub fn generate<G, S>(gen: G, cmd: &mut clap::Command, bin_name: S, buf: &mut dyn Write)
where
G: Generator,
S: Into<clap::Str>,
S: Into<clap::builder::Str>,
{
cmd.set_bin_name(bin_name);
_generate::<G, S>(gen, cmd, buf)
Expand All @@ -232,7 +232,7 @@ where
fn _generate<G, S>(gen: G, cmd: &mut clap::Command, buf: &mut dyn Write)
where
G: Generator,
S: Into<clap::Str>,
S: Into<clap::builder::Str>,
{
cmd.build();

Expand Down
4 changes: 2 additions & 2 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ pub fn gen_augment(
let next_display_order = attrs.next_display_order();
if override_required {
Some(quote_spanned! { kind.span()=>
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Args>::augment_args_for_update(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
})
} else {
Some(quote_spanned! { kind.span()=>
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Args>::augment_args(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
Expand Down
4 changes: 2 additions & 2 deletions clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ fn gen_augment(
let next_display_order = attrs.next_display_order();
let subcommand = if override_required {
quote! {
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Subcommand>::augment_subcommands_for_update(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
}
} else {
quote! {
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Subcommand>::augment_subcommands(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
Expand Down
12 changes: 6 additions & 6 deletions src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use std::{
use super::{ArgFlags, ArgSettings};
use crate::builder::ArgPredicate;
use crate::builder::IntoResettable;
use crate::builder::OsStr;
use crate::builder::PossibleValue;
use crate::builder::Str;
use crate::builder::ValueRange;
use crate::ArgAction;
use crate::Id;
use crate::OsStr;
use crate::Str;
use crate::ValueHint;
use crate::INTERNAL_ERROR_MSG;

Expand Down Expand Up @@ -3721,14 +3721,14 @@ impl Arg {
/// # Examples
///
/// ```rust
/// # use clap::OsStr;
/// # use std::ffi::OsStr;
/// # use clap::Arg;
/// let arg = Arg::new("foo").env("ENVIRONMENT");
/// assert_eq!(arg.get_env(), Some(&OsStr::from("ENVIRONMENT")));
/// assert_eq!(arg.get_env(), Some(OsStr::new("ENVIRONMENT")));
/// ```
#[cfg(feature = "env")]
pub fn get_env(&self) -> Option<&OsStr> {
self.env.as_ref().map(|x| &x.0)
pub fn get_env(&self) -> Option<&std::ffi::OsStr> {
self.env.as_ref().map(|x| x.0.as_os_str())
}

/// Get the default values specified for this argument, if any
Expand Down
2 changes: 1 addition & 1 deletion src/builder/arg_predicate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::OsStr;
use crate::builder::OsStr;

/// Operations to perform on argument values
///
Expand Down
2 changes: 1 addition & 1 deletion src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::builder::arg_settings::ArgSettings;
use crate::builder::ArgAction;
use crate::builder::IntoResettable;
use crate::builder::PossibleValue;
use crate::builder::Str;
use crate::builder::{Arg, ArgGroup, ArgPredicate};
use crate::error::ErrorKind;
use crate::error::Result as ClapResult;
Expand All @@ -22,7 +23,6 @@ use crate::parser::{ArgMatcher, ArgMatches, Parser};
use crate::util::ChildGraph;
use crate::util::FlatMap;
use crate::util::{color::ColorChoice, Id};
use crate::Str;
use crate::{Error, INTERNAL_ERROR_MSG};

#[cfg(debug_assertions)]
Expand Down
2 changes: 1 addition & 1 deletion src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::cmp::Ordering;

use clap_lex::RawOsStr;

use crate::builder::OsStr;
use crate::builder::ValueRange;
use crate::mkeymap::KeyType;
use crate::util::FlatSet;
use crate::util::Id;
use crate::ArgAction;
use crate::OsStr;
use crate::INTERNAL_ERROR_MSG;
use crate::{Arg, Command, ValueHint};

Expand Down
6 changes: 6 additions & 0 deletions src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ mod arg_group;
mod arg_predicate;
mod arg_settings;
mod command;
mod os_str;
mod possible_value;
mod range;
mod resettable;
mod str;
mod value_hint;
mod value_parser;

Expand All @@ -19,11 +21,13 @@ mod debug_asserts;
#[cfg(test)]
mod tests;

pub use self::str::Str;
pub use action::ArgAction;
pub use arg::Arg;
pub use arg_group::ArgGroup;
pub use arg_predicate::ArgPredicate;
pub use command::Command;
pub use os_str::OsStr;
pub use possible_value::PossibleValue;
pub use range::ValueRange;
pub use resettable::IntoResettable;
Expand All @@ -48,6 +52,8 @@ pub use value_parser::ValueParser;
pub use value_parser::ValueParserFactory;
pub use value_parser::_AnonymousValueParser;

#[allow(unused_imports)]
pub(crate) use self::str::Inner as StrInner;
pub(crate) use action::CountType;
pub(crate) use arg::render_arg_val;
pub(crate) use arg_settings::{ArgFlags, ArgSettings};
26 changes: 14 additions & 12 deletions src/util/os_str.rs → src/builder/os_str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::builder::Str;

/// A UTF-8-encoded fixed string
#[derive(Default, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct OsStr {
Expand Down Expand Up @@ -41,35 +43,35 @@ impl From<&'_ OsStr> for OsStr {
}

#[cfg(feature = "perf")]
impl From<crate::Str> for OsStr {
fn from(id: crate::Str) -> Self {
impl From<Str> for OsStr {
fn from(id: 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())),
crate::builder::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)),
crate::builder::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())),
}
}
}

#[cfg(not(feature = "perf"))]
impl From<crate::Str> for OsStr {
fn from(id: crate::Str) -> Self {
impl From<Str> for OsStr {
fn from(id: Str) -> Self {
Self::from_ref(std::ffi::OsStr::new(id.as_str()))
}
}

#[cfg(feature = "perf")]
impl From<&'_ crate::Str> for OsStr {
fn from(id: &'_ crate::Str) -> Self {
impl From<&'_ Str> for OsStr {
fn from(id: &'_ 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())),
crate::builder::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)),
crate::builder::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())),
}
}
}

#[cfg(not(feature = "perf"))]
impl From<&'_ crate::Str> for OsStr {
fn from(id: &'_ crate::Str) -> Self {
impl From<&'_ Str> for OsStr {
fn from(id: &'_ Str) -> Self {
Self::from_ref(std::ffi::OsStr::new(id.as_str()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/possible_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, iter};

use crate::builder::Str;
use crate::util::eq_ignore_case;
use crate::Str;

/// A possible value of an argument.
///
Expand Down
19 changes: 11 additions & 8 deletions src/builder/resettable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Unlike `impl Into<Option<T>>` or `Option<impl Into<T>>`, this isn't ambiguous for the `None`
// case.

use crate::builder::OsStr;
use crate::builder::Str;

/// Clearable builder value
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Resettable<T> {
Expand Down Expand Up @@ -40,17 +43,17 @@ pub trait IntoResettable<T> {
fn into_resettable(self) -> Resettable<T>;
}

impl IntoResettable<crate::OsStr> for Option<&'static str> {
fn into_resettable(self) -> Resettable<crate::OsStr> {
impl IntoResettable<OsStr> for Option<&'static str> {
fn into_resettable(self) -> Resettable<OsStr> {
match self {
Some(s) => Resettable::Value(s.into()),
None => Resettable::Reset,
}
}
}

impl IntoResettable<crate::Str> for Option<&'static str> {
fn into_resettable(self) -> Resettable<crate::Str> {
impl IntoResettable<Str> for Option<&'static str> {
fn into_resettable(self) -> Resettable<Str> {
match self {
Some(s) => Resettable::Value(s.into()),
None => Resettable::Reset,
Expand All @@ -64,14 +67,14 @@ impl<T> IntoResettable<T> for Resettable<T> {
}
}

impl<I: Into<crate::OsStr>> IntoResettable<crate::OsStr> for I {
fn into_resettable(self) -> Resettable<crate::OsStr> {
impl<I: Into<OsStr>> IntoResettable<OsStr> for I {
fn into_resettable(self) -> Resettable<OsStr> {
Resettable::Value(self.into())
}
}

impl<I: Into<crate::Str>> IntoResettable<crate::Str> for I {
fn into_resettable(self) -> Resettable<crate::Str> {
impl<I: Into<Str>> IntoResettable<Str> for I {
fn into_resettable(self) -> Resettable<Str> {
Resettable::Value(self.into())
}
}
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ pub use crate::util::color::ColorChoice;
#[allow(unused_imports)]
pub(crate) use crate::util::color::ColorChoice;
pub use crate::util::Id;
pub use crate::util::OsStr;
pub use crate::util::Str;

pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};

Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ macro_rules! crate_authors {
($sep:expr) => {{
let authors = env!("CARGO_PKG_AUTHORS");
if authors.contains(':') {
$crate::Str::from(authors.replace(':', $sep))
$crate::builder::Str::from(authors.replace(':', $sep))
} else {
$crate::Str::from(authors)
$crate::builder::Str::from(authors)
}
}};
() => {
Expand Down
2 changes: 1 addition & 1 deletion src/mkeymap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::iter::Iterator;
use std::ops::Index;

use crate::builder::OsStr;
use crate::Arg;
use crate::OsStr;
use crate::INTERNAL_ERROR_MSG;

#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion src/output/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::usize;

// Internal
use crate::builder::PossibleValue;
use crate::builder::Str;
use crate::builder::{render_arg_val, Arg, Command};
use crate::output::{fmt::Colorizer, Usage};
use crate::util::FlatSet;
use crate::util::Str;
use crate::ArgAction;

// Third party
Expand Down
2 changes: 1 addition & 1 deletion src/parser/matches/arg_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::iter::{Cloned, Flatten, Map};
use std::slice::Iter;

// Internal
use crate::builder::Str;
use crate::parser::AnyValue;
use crate::parser::AnyValueId;
use crate::parser::MatchedArg;
use crate::parser::MatchesError;
use crate::parser::ValueSource;
use crate::util::FlatMap;
use crate::util::Id;
use crate::Str;
use crate::INTERNAL_ERROR_MSG;

/// Container for parse results.
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use clap_lex::RawOsStr;
use clap_lex::RawOsString;

// Internal
use crate::builder::Str;
use crate::builder::{Arg, Command};
use crate::error::Error as ClapError;
use crate::error::Result as ClapResult;
Expand All @@ -21,7 +22,6 @@ use crate::parser::{ArgMatcher, SubCommand};
use crate::parser::{Validator, ValueSource};
use crate::util::Id;
use crate::ArgAction;
use crate::Str;
use crate::{INTERNAL_ERROR_MSG, INVALID_UTF8};

pub(crate) struct Parser<'cmd> {
Expand Down Expand Up @@ -1425,7 +1425,7 @@ impl<'cmd> Parser<'cmd> {
let arg_values: Vec<_> = arg
.default_vals
.iter()
.map(crate::OsStr::to_os_string)
.map(crate::builder::OsStr::to_os_string)
.collect();
let trailing_idx = None;
let _ = self.react(
Expand Down
2 changes: 1 addition & 1 deletion src/util/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Str;
use crate::builder::Str;

/// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier
///
Expand Down
6 changes: 0 additions & 6 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ mod flat_map;
mod flat_set;
mod graph;
mod id;
mod os_str;
mod str;
mod str_to_bool;

pub use self::id::Id;
pub use self::os_str::OsStr;
pub use self::str::Str;

pub(crate) use self::flat_map::Entry;
pub(crate) use self::flat_map::FlatMap;
pub(crate) use self::flat_set::FlatSet;
pub(crate) use self::graph::ChildGraph;
#[allow(unused_imports)]
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::FALSE_LITERALS;
pub(crate) use self::str_to_bool::TRUE_LITERALS;
Expand Down

0 comments on commit d4ec9ca

Please sign in to comment.