diff --git a/clap_complete/src/generator/mod.rs b/clap_complete/src/generator/mod.rs index 1bdb6da1..dd9c0584 100644 --- a/clap_complete/src/generator/mod.rs +++ b/clap_complete/src/generator/mod.rs @@ -170,7 +170,7 @@ pub fn generate_to( ) -> Result where G: Generator, - S: Into, + S: Into, T: Into, { cmd.set_bin_name(bin_name); @@ -223,7 +223,7 @@ where pub fn generate(gen: G, cmd: &mut clap::Command, bin_name: S, buf: &mut dyn Write) where G: Generator, - S: Into, + S: Into, { cmd.set_bin_name(bin_name); _generate::(gen, cmd, buf) @@ -232,7 +232,7 @@ where fn _generate(gen: G, cmd: &mut clap::Command, buf: &mut dyn Write) where G: Generator, - S: Into, + S: Into, { cmd.build(); diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index ea2ad1bd..46a26da2 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -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)); diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index e614a38f..150860ea 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -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)); diff --git a/src/builder/arg.rs b/src/builder/arg.rs index 2a4ac4bd..aa9fdf35 100644 --- a/src/builder/arg.rs +++ b/src/builder/arg.rs @@ -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; @@ -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 diff --git a/src/builder/arg_predicate.rs b/src/builder/arg_predicate.rs index 34768659..39a7e23e 100644 --- a/src/builder/arg_predicate.rs +++ b/src/builder/arg_predicate.rs @@ -1,4 +1,4 @@ -use crate::OsStr; +use crate::builder::OsStr; /// Operations to perform on argument values /// diff --git a/src/builder/command.rs b/src/builder/command.rs index 7599f006..c98f4d79 100644 --- a/src/builder/command.rs +++ b/src/builder/command.rs @@ -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; @@ -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)] diff --git a/src/builder/debug_asserts.rs b/src/builder/debug_asserts.rs index 93dc39bd..140a5ad9 100644 --- a/src/builder/debug_asserts.rs +++ b/src/builder/debug_asserts.rs @@ -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}; diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 23ad15a9..a8c2613c 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -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; @@ -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; @@ -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}; diff --git a/src/util/os_str.rs b/src/builder/os_str.rs similarity index 91% rename from src/util/os_str.rs rename to src/builder/os_str.rs index cf75823f..9e815d4d 100644 --- a/src/util/os_str.rs +++ b/src/builder/os_str.rs @@ -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 { @@ -41,35 +43,35 @@ impl From<&'_ OsStr> for OsStr { } #[cfg(feature = "perf")] -impl From for OsStr { - fn from(id: crate::Str) -> Self { +impl From 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 for OsStr { - fn from(id: crate::Str) -> Self { +impl From 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())) } } diff --git a/src/builder/possible_value.rs b/src/builder/possible_value.rs index d645bb5a..acf671b2 100644 --- a/src/builder/possible_value.rs +++ b/src/builder/possible_value.rs @@ -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. /// diff --git a/src/builder/resettable.rs b/src/builder/resettable.rs index 297bae48..e7b111d0 100644 --- a/src/builder/resettable.rs +++ b/src/builder/resettable.rs @@ -1,6 +1,9 @@ // Unlike `impl Into>` or `Option>`, 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 { @@ -40,8 +43,8 @@ pub trait IntoResettable { fn into_resettable(self) -> Resettable; } -impl IntoResettable for Option<&'static str> { - fn into_resettable(self) -> Resettable { +impl IntoResettable for Option<&'static str> { + fn into_resettable(self) -> Resettable { match self { Some(s) => Resettable::Value(s.into()), None => Resettable::Reset, @@ -49,8 +52,8 @@ impl IntoResettable for Option<&'static str> { } } -impl IntoResettable for Option<&'static str> { - fn into_resettable(self) -> Resettable { +impl IntoResettable for Option<&'static str> { + fn into_resettable(self) -> Resettable { match self { Some(s) => Resettable::Value(s.into()), None => Resettable::Reset, @@ -64,14 +67,14 @@ impl IntoResettable for Resettable { } } -impl> IntoResettable for I { - fn into_resettable(self) -> Resettable { +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { Resettable::Value(self.into()) } } -impl> IntoResettable for I { - fn into_resettable(self) -> Resettable { +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { Resettable::Value(self.into()) } } diff --git a/src/util/str.rs b/src/builder/str.rs similarity index 100% rename from src/util/str.rs rename to src/builder/str.rs diff --git a/src/lib.rs b/src/lib.rs index 7553d6b6..0f9d25a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; diff --git a/src/macros.rs b/src/macros.rs index dfd232ab..0bb8eaff 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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) } }}; () => { diff --git a/src/mkeymap.rs b/src/mkeymap.rs index 19fac98a..3c660171 100644 --- a/src/mkeymap.rs +++ b/src/mkeymap.rs @@ -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)] diff --git a/src/output/help.rs b/src/output/help.rs index 66d84496..9f04451f 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -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 diff --git a/src/parser/matches/arg_matches.rs b/src/parser/matches/arg_matches.rs index 31734290..858e6ba8 100644 --- a/src/parser/matches/arg_matches.rs +++ b/src/parser/matches/arg_matches.rs @@ -6,6 +6,7 @@ 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; @@ -13,7 +14,6 @@ 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. diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 9bbfb738..7731735a 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -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; @@ -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> { @@ -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( diff --git a/src/util/id.rs b/src/util/id.rs index c5925906..a6ee26ef 100644 --- a/src/util/id.rs +++ b/src/util/id.rs @@ -1,4 +1,4 @@ -use crate::Str; +use crate::builder::Str; /// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier /// diff --git a/src/util/mod.rs b/src/util/mod.rs index 6f77c37a..ed38fbc5 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -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;