diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 28ec97c7..f8d638f1 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -1,5 +1,6 @@ mod settings; -pub use self::settings::{AppFlags, AppSettings}; + +pub use self::settings::AppSettings; // Std use std::collections::HashMap; @@ -15,7 +16,7 @@ use std::process; use yaml_rust::Yaml; // Internal -use crate::build::{Arg, ArgGroup, ArgSettings}; +use crate::build::{app::settings::AppFlags, Arg, ArgGroup, ArgSettings}; use crate::mkeymap::MKeyMap; use crate::output::fmt::ColorWhen; use crate::output::{Help, Usage}; @@ -26,9 +27,10 @@ use crate::INTERNAL_ERROR_MSG; type Id = u64; -#[doc(hidden)] +// FIXME (@CreepySkeleton): some of this variants are never constructed #[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum Propagation { +#[allow(unused)] +pub(crate) enum Propagation { To(Id), Full, NextLevel, @@ -65,56 +67,37 @@ pub enum Propagation { /// [`App::get_matches`]: ./struct.App.html#method.get_matches #[derive(Default, Debug, Clone)] pub struct App<'b> { - #[doc(hidden)] - pub id: Id, + pub(crate) id: Id, #[doc(hidden)] pub name: String, #[doc(hidden)] pub bin_name: Option, - #[doc(hidden)] - pub author: Option<&'b str>, - #[doc(hidden)] - pub version: Option<&'b str>, - #[doc(hidden)] - pub long_version: Option<&'b str>, + pub(crate) author: Option<&'b str>, + pub(crate) version: Option<&'b str>, + pub(crate) long_version: Option<&'b str>, #[doc(hidden)] pub about: Option<&'b str>, - #[doc(hidden)] - pub long_about: Option<&'b str>, - #[doc(hidden)] - pub more_help: Option<&'b str>, - #[doc(hidden)] - pub pre_help: Option<&'b str>, + pub(crate) long_about: Option<&'b str>, + pub(crate) more_help: Option<&'b str>, + pub(crate) pre_help: Option<&'b str>, #[doc(hidden)] pub aliases: Option>, // (name, visible) - #[doc(hidden)] - pub usage_str: Option<&'b str>, - #[doc(hidden)] - pub usage: Option, - #[doc(hidden)] - pub help_str: Option<&'b str>, - #[doc(hidden)] - pub disp_ord: usize, - #[doc(hidden)] - pub term_w: Option, - #[doc(hidden)] - pub max_w: Option, - #[doc(hidden)] - pub template: Option<&'b str>, - #[doc(hidden)] - pub settings: AppFlags, - #[doc(hidden)] - pub g_settings: AppFlags, + pub(crate) usage_str: Option<&'b str>, + pub(crate) usage: Option, + pub(crate) help_str: Option<&'b str>, + pub(crate) disp_ord: usize, + pub(crate) term_w: Option, + pub(crate) max_w: Option, + pub(crate) template: Option<&'b str>, + pub(crate) settings: AppFlags, + pub(crate) g_settings: AppFlags, #[doc(hidden)] pub args: MKeyMap<'b>, #[doc(hidden)] pub subcommands: Vec>, - #[doc(hidden)] - pub replacers: HashMap<&'b str, &'b [&'b str]>, - #[doc(hidden)] - pub groups: Vec>, - #[doc(hidden)] - pub help_headings: Vec>, + pub(crate) replacers: HashMap<&'b str, &'b [&'b str]>, + pub(crate) groups: Vec>, + pub(crate) help_headings: Vec>, } impl<'b> App<'b> { @@ -1532,7 +1515,7 @@ impl<'b> App<'b> { true } - pub fn _propagate(&mut self, prop: Propagation) { + pub(crate) fn _propagate(&mut self, prop: Propagation) { macro_rules! propagate_subcmd { ($_self:expr, $sc:expr) => {{ // We have to create a new scope in order to tell rustc the borrow of `sc` is @@ -1794,7 +1777,7 @@ impl<'b> App<'b> { // Should we color the output? None=determined by output location, true=yes, false=no #[doc(hidden)] - pub fn color(&self) -> ColorWhen { + pub(crate) fn color(&self) -> ColorWhen { debugln!("App::color;"); debug!("App::color: Color setting..."); if self.is_set(AppSettings::ColorNever) { @@ -1821,55 +1804,31 @@ impl<'b> App<'b> { self.settings.is_set(s) || self.g_settings.is_set(s) } - pub fn set(&mut self, s: AppSettings) { - self.settings.set(s) - } - - pub fn set_global(&mut self, s: AppSettings) { - self.g_settings.set(s) - } - - pub fn unset_global(&mut self, s: AppSettings) { - self.g_settings.unset(s) - } - - pub fn unset(&mut self, s: AppSettings) { - self.settings.unset(s) - } - pub fn has_subcommands(&self) -> bool { !self.subcommands.is_empty() } - pub fn has_args(&self) -> bool { + pub(crate) fn set(&mut self, s: AppSettings) { + self.settings.set(s) + } + + pub(crate) fn unset(&mut self, s: AppSettings) { + self.settings.unset(s) + } + + pub(crate) fn has_args(&self) -> bool { !self.args.is_empty() } - pub fn has_opts(&self) -> bool { + pub(crate) fn has_opts(&self) -> bool { opts!(self).count() > 0 } - pub fn has_flags(&self) -> bool { + pub(crate) fn has_flags(&self) -> bool { flags!(self).count() > 0 } - pub fn has_positionals(&self) -> bool { - positionals!(self).count() > 0 - } - - pub fn has_visible_opts(&self) -> bool { - opts!(self).any(|o| !o.is_set(ArgSettings::Hidden)) - } - - pub fn has_visible_flags(&self) -> bool { - flags!(self).any(|o| !o.is_set(ArgSettings::Hidden)) - } - - pub fn has_visible_positionals(&self) -> bool { - positionals!(self).any(|o| !o.is_set(ArgSettings::Hidden)) - } - - pub fn has_visible_subcommands(&self) -> bool { + pub(crate) fn has_visible_subcommands(&self) -> bool { subcommands!(self) .filter(|sc| sc.name != "help") .any(|sc| !sc.is_set(AppSettings::Hidden)) diff --git a/src/build/app/settings.rs b/src/build/app/settings.rs index 82185fff..a0edf7a0 100644 --- a/src/build/app/settings.rs +++ b/src/build/app/settings.rs @@ -52,7 +52,7 @@ bitflags! { #[doc(hidden)] #[derive(Debug, Copy, Clone, PartialEq)] -pub struct AppFlags(Flags); +pub(crate) struct AppFlags(Flags); impl BitOr for AppFlags { type Output = Self; @@ -67,15 +67,6 @@ impl Default for AppFlags { } } -impl AppFlags { - pub fn new() -> Self { - AppFlags::default() - } - pub fn zeroed() -> Self { - AppFlags(Flags::empty()) - } -} - impl_settings! { AppSettings, AppFlags, ArgRequiredElseHelp("argrequiredelsehelp") => Flags::A_REQUIRED_ELSE_HELP, @@ -659,7 +650,7 @@ pub enum AppSettings { HidePossibleValuesInHelp, /// Tells `clap` to panic if help strings are omitted - /// + /// /// # Examples /// /// ```rust diff --git a/src/build/arg/mod.rs b/src/build/arg/mod.rs index f3d230a7..1d11669a 100644 --- a/src/build/arg/mod.rs +++ b/src/build/arg/mod.rs @@ -1,5 +1,6 @@ mod settings; -pub use self::settings::{ArgFlags, ArgSettings}; + +pub use self::settings::ArgSettings; // Std use std::borrow::Cow; @@ -18,7 +19,7 @@ use crate::util::VecMap; use yaml_rust; // Internal -use crate::build::UsageParser; +use crate::build::{arg::settings::ArgFlags, usage_parser::UsageParser}; use crate::util::Key; #[cfg(any(target_os = "windows", target_arch = "wasm32"))] use crate::util::OsStrExt3; @@ -54,70 +55,46 @@ type Id = u64; #[allow(missing_debug_implementations)] #[derive(Default, Clone)] pub struct Arg<'help> { - #[doc(hidden)] - pub id: Id, + pub(crate) id: Id, #[doc(hidden)] pub name: &'help str, #[doc(hidden)] pub help: Option<&'help str>, - #[doc(hidden)] - pub long_help: Option<&'help str>, + pub(crate) long_help: Option<&'help str>, #[doc(hidden)] pub blacklist: Option>, - #[doc(hidden)] - pub settings: ArgFlags, - #[doc(hidden)] - pub r_unless: Option>, - #[doc(hidden)] - pub overrides: Option>, - #[doc(hidden)] - pub groups: Option>, - #[doc(hidden)] - pub requires: Option, Id)>>, + pub(crate) settings: ArgFlags, + pub(crate) r_unless: Option>, + pub(crate) overrides: Option>, + pub(crate) groups: Option>, + pub(crate) requires: Option, Id)>>, #[doc(hidden)] pub short: Option, #[doc(hidden)] pub long: Option<&'help str>, - #[doc(hidden)] - pub aliases: Option>, // (name, visible) - #[doc(hidden)] - pub disp_ord: usize, - #[doc(hidden)] - pub unified_ord: usize, + pub(crate) aliases: Option>, // (name, visible) + pub(crate) disp_ord: usize, + pub(crate) unified_ord: usize, #[doc(hidden)] pub possible_vals: Option>, - #[doc(hidden)] - pub val_names: Option>, - #[doc(hidden)] - pub num_vals: Option, - #[doc(hidden)] - pub max_vals: Option, - #[doc(hidden)] - pub min_vals: Option, - #[doc(hidden)] - pub validator: Option, - #[doc(hidden)] - pub validator_os: Option, - #[doc(hidden)] - pub val_delim: Option, - #[doc(hidden)] - pub default_vals: Option>, - #[doc(hidden)] - pub default_vals_ifs: Option, &'help OsStr)>>, - #[doc(hidden)] - pub env: Option<(&'help OsStr, Option)>, - #[doc(hidden)] - pub terminator: Option<&'help str>, + pub(crate) val_names: Option>, + pub(crate) num_vals: Option, + pub(crate) max_vals: Option, + pub(crate) min_vals: Option, + pub(crate) validator: Option, + pub(crate) validator_os: Option, + pub(crate) val_delim: Option, + pub(crate) default_vals: Option>, + pub(crate) default_vals_ifs: Option, &'help OsStr)>>, + pub(crate) env: Option<(&'help OsStr, Option)>, + pub(crate) terminator: Option<&'help str>, #[doc(hidden)] pub index: Option, - #[doc(hidden)] - pub r_ifs: Option>, + pub(crate) r_ifs: Option>, #[doc(hidden)] pub help_heading: Option<&'help str>, - #[doc(hidden)] - pub global: bool, - #[doc(hidden)] - pub exclusive: bool, + pub(crate) global: bool, + pub(crate) exclusive: bool, } impl<'help> Arg<'help> { @@ -817,7 +794,7 @@ impl<'help> Arg<'help> { /// assert!(res.is_err()); /// assert_eq!(res.unwrap_err().kind, ErrorKind::ArgumentConflict); /// ``` - /// + /// /// [`Arg::conflicts_with_all(names)`]: ./struct.Arg.html#method.conflicts_with_all /// [`Arg::exclusive(true)`]: ./struct.Arg.html#method.exclusive @@ -4066,6 +4043,7 @@ impl<'help> Arg<'help> { self } + // FIXME: (@CreepySkeleton) #[doc(hidden)] pub fn _build(&mut self) { if (self.is_set(ArgSettings::UseValueDelimiter) @@ -4092,30 +4070,25 @@ impl<'help> Arg<'help> { } // @TODO @p6 @naming @internal: rename to set_mut - #[doc(hidden)] - pub fn setb(&mut self, s: ArgSettings) { + pub(crate) fn setb(&mut self, s: ArgSettings) { self.settings.set(s); } // @TODO @p6 @naming @internal: rename to unset_mut - #[doc(hidden)] - pub fn unsetb(&mut self, s: ArgSettings) { + pub(crate) fn unsetb(&mut self, s: ArgSettings) { self.settings.unset(s); } - #[doc(hidden)] - pub fn has_switch(&self) -> bool { + pub(crate) fn has_switch(&self) -> bool { self.short.is_some() || self.long.is_some() } - #[doc(hidden)] - pub fn longest_filter(&self) -> bool { + pub(crate) fn longest_filter(&self) -> bool { self.is_set(ArgSettings::TakesValue) || self.long.is_some() || self.short.is_none() } // Used for positionals when printing - #[doc(hidden)] - pub fn multiple_str(&self) -> &str { + pub(crate) fn multiple_str(&self) -> &str { let mult_vals = self .val_names .as_ref() @@ -4131,8 +4104,7 @@ impl<'help> Arg<'help> { } // Used for positionals when printing - #[doc(hidden)] - pub fn name_no_brackets(&self) -> Cow { + pub(crate) fn name_no_brackets(&self) -> Cow { debugln!("PosBuilder::name_no_brackets:{}", self.name); let mut delim = String::new(); delim.push(if self.is_set(ArgSettings::RequireDelimiter) { diff --git a/src/build/arg/settings.rs b/src/build/arg/settings.rs index 520859e6..4a87b27f 100644 --- a/src/build/arg/settings.rs +++ b/src/build/arg/settings.rs @@ -31,13 +31,7 @@ bitflags! { #[doc(hidden)] #[derive(Debug, Clone, Copy)] -pub struct ArgFlags(Flags); - -impl ArgFlags { - pub fn new() -> Self { - ArgFlags::default() - } -} +pub(crate) struct ArgFlags(Flags); // @TODO @p6 @internal: Reorder alphabetically impl_settings! { ArgSettings, ArgFlags, diff --git a/src/build/arg_group.rs b/src/build/arg_group.rs index 9e223c9e..c3b2a986 100644 --- a/src/build/arg_group.rs +++ b/src/build/arg_group.rs @@ -82,25 +82,17 @@ type Id = u64; /// [requirement]: ./struct.Arg.html#method.requires #[derive(Default)] pub struct ArgGroup<'a> { - #[doc(hidden)] - pub id: Id, - #[doc(hidden)] - pub name: &'a str, - #[doc(hidden)] - pub args: Vec, - #[doc(hidden)] - pub required: bool, - #[doc(hidden)] - pub requires: Option>, - #[doc(hidden)] - pub conflicts: Option>, - #[doc(hidden)] - pub multiple: bool, + pub(crate) id: Id, + pub(crate) name: &'a str, + pub(crate) args: Vec, + pub(crate) required: bool, + pub(crate) requires: Option>, + pub(crate) conflicts: Option>, + pub(crate) multiple: bool, } impl<'a> ArgGroup<'a> { - #[doc(hidden)] - pub fn _with_id(id: Id) -> Self { + pub(crate) fn _with_id(id: Id) -> Self { ArgGroup { id, ..ArgGroup::default() diff --git a/src/build/mod.rs b/src/build/mod.rs index 3d67ba6b..ee6641d4 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -7,7 +7,6 @@ pub mod arg; mod arg_group; mod usage_parser; -pub use self::app::{App, AppFlags, AppSettings, Propagation}; -pub use self::arg::{Arg, ArgFlags, ArgSettings}; +pub use self::app::{App, AppSettings}; +pub use self::arg::{Arg, ArgSettings}; pub use self::arg_group::ArgGroup; -pub use self::usage_parser::UsageParser; diff --git a/src/build/usage_parser.rs b/src/build/usage_parser.rs index e12551a6..ce95d9a9 100644 --- a/src/build/usage_parser.rs +++ b/src/build/usage_parser.rs @@ -15,9 +15,8 @@ enum UsageToken { Default, } -#[doc(hidden)] #[derive(Debug)] -pub struct UsageParser<'a> { +pub(crate) struct UsageParser<'a> { usage: &'a str, pos: usize, start: usize, @@ -37,12 +36,12 @@ impl<'a> UsageParser<'a> { } } - pub fn from_usage(usage: &'a str) -> Self { + pub(crate) fn from_usage(usage: &'a str) -> Self { debugln!("UsageParser::from_usage;"); UsageParser::new(usage) } - pub fn parse(mut self) -> Arg<'a> { + pub(crate) fn parse(mut self) -> Arg<'a> { debugln!("UsageParser::parse;"); let mut arg = Arg::default(); arg.disp_ord = 999; diff --git a/src/lib.rs b/src/lib.rs index a129e2a5..1ad6b327 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -445,11 +445,10 @@ )] #[cfg(not(feature = "std"))] -compile_error!("`std` feature is currently required to build this crate"); +compile_error!("`std` feature is currently required to build `clap`"); -pub use crate::build::{App, AppSettings, Arg, ArgGroup, ArgSettings, Propagation}; +pub use crate::build::{App, AppSettings, Arg, ArgGroup, ArgSettings}; pub use crate::derive::{Clap, FromArgMatches, IntoApp, Subcommand}; -pub use crate::output::fmt::Format; pub use crate::parse::errors::{Error, ErrorKind, Result}; pub use crate::parse::{ArgMatches, OsValues, SubCommand, Values}; @@ -464,9 +463,6 @@ pub use clap_derive::{self, *}; #[cfg_attr(feature = "derive", doc(hidden))] pub use lazy_static; -#[doc(hidden)] -pub use mkeymap::KeyType; - #[macro_use] #[allow(missing_docs)] pub mod macros; diff --git a/src/macros.rs b/src/macros.rs index e97a4b3a..33d87d50 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -829,19 +829,19 @@ macro_rules! impl_settings { $( $setting:ident($str:expr) => $flag:path ),+ ) => { impl $flags { - pub fn set(&mut self, s: $settings) { + pub(crate) fn set(&mut self, s: $settings) { match s { $($settings::$setting => self.0.insert($flag)),* } } - pub fn unset(&mut self, s: $settings) { + pub(crate) fn unset(&mut self, s: $settings) { match s { $($settings::$setting => self.0.remove($flag)),* } } - pub fn is_set(&self, s: $settings) -> bool { + pub(crate) fn is_set(&self, s: $settings) -> bool { match s { $($settings::$setting => self.0.contains($flag)),* } @@ -927,7 +927,7 @@ macro_rules! flags { $app.args .args .$how() - .filter(|a| !a.settings.is_set($crate::ArgSettings::TakesValue) && a.index.is_none()) + .filter(|a| !a.is_set($crate::ArgSettings::TakesValue) && a.index.is_none()) .filter(|a| !a.help_heading.is_some()) }}; ($app:expr) => { @@ -942,7 +942,7 @@ macro_rules! opts { $app.args .args .$how() - .filter(|a| a.settings.is_set($crate::ArgSettings::TakesValue) && a.index.is_none()) + .filter(|a| a.is_set($crate::ArgSettings::TakesValue) && a.index.is_none()) .filter(|a| !a.help_heading.is_some()) }}; ($app:expr) => { diff --git a/src/mkeymap.rs b/src/mkeymap.rs index f279deb6..2f0aa39a 100644 --- a/src/mkeymap.rs +++ b/src/mkeymap.rs @@ -4,21 +4,22 @@ use std::ffi::{OsStr, OsString}; type Id = u64; #[derive(PartialEq, Debug, Clone)] -pub struct Key { - pub key: KeyType, - pub index: usize, +pub(crate) struct Key { + pub(crate) key: KeyType, + pub(crate) index: usize, } #[derive(Default, PartialEq, Debug, Clone)] pub struct MKeyMap<'b> { - pub keys: Vec, + pub(crate) keys: Vec, pub args: Vec>, + + // FIXME (@CreepySkeleton): this seems useless built: bool, // mutation isn't possible after being built } -#[doc(hidden)] #[derive(Debug, PartialEq, Eq, Hash, Clone)] -pub enum KeyType { +pub(crate) enum KeyType { Short(char), Long(OsString), Position(u64), @@ -52,36 +53,17 @@ impl PartialEq for KeyType { } impl<'b> MKeyMap<'b> { - pub fn new() -> Self { - MKeyMap::default() - } //TODO ::from(x), ::with_capacity(n) etc //? set theory ops? - #[deprecated(since = "3.0.0", note = "Use `contains` instead")] - pub fn contains_long(&self, l: &str) -> bool { - self.contains(l) - } - - #[deprecated(since = "3.0.0", note = "Use `contains` instead")] - pub fn contains_short(&self, c: char) -> bool { - self.contains(c) - } - - pub fn contains(&self, key: K) -> bool + pub(crate) fn contains(&self, key: K) -> bool where KeyType: PartialEq, { self.keys.iter().any(|x| x.key == key) } - pub fn insert(&mut self, key: KeyType, value: Arg<'b>) -> usize { - let index = self.push(value); - self.keys.push(Key { key, index }); - index - } - - pub fn push(&mut self, value: Arg<'b>) -> usize { + pub(crate) fn push(&mut self, value: Arg<'b>) -> usize { if self.built { panic!("Cannot add Args to the map after the map is built"); } @@ -93,7 +75,7 @@ impl<'b> MKeyMap<'b> { } //TODO ::push_many([x, y]) - pub fn insert_key(&mut self, key: KeyType, index: usize) { + pub(crate) fn insert_key(&mut self, key: KeyType, index: usize) { if index >= self.args.len() { panic!("Index out of bounds"); } @@ -104,7 +86,7 @@ impl<'b> MKeyMap<'b> { // ! Arg mutation functionality - pub fn get(&self, key: &KeyType) -> Option<&Arg<'b>> { + pub(crate) fn get(&self, key: &KeyType) -> Option<&Arg<'b>> { self.keys .iter() .find(|k| k.key == *key) @@ -112,33 +94,11 @@ impl<'b> MKeyMap<'b> { } //TODO ::get_first([KeyA, KeyB]) - pub fn get_mut(&mut self, key: &KeyType) -> Option<&mut Arg<'b>> { - let key = self.keys.iter().find(|k| k.key == *key); - - match key { - Some(k) => self.args.get_mut(k.index), - None => None, - } - } - - pub fn is_empty(&self) -> bool { + pub(crate) fn is_empty(&self) -> bool { self.keys.is_empty() && self.args.is_empty() } - pub fn remove_key(&mut self, key: &KeyType) { - self.keys - .iter() - .position(|k| k.key == *key) - .map(|i| self.keys.swap_remove(i)); - } - - pub fn insert_key_by_name(&mut self, key: KeyType, name: &str) { - let index = self.find_by_name(name); - - self.keys.push(Key { key, index }); - } - - pub fn _build(&mut self) { + pub(crate) fn _build(&mut self) { self.built = true; for (i, arg) in self.args.iter_mut().enumerate() { @@ -148,63 +108,11 @@ impl<'b> MKeyMap<'b> { } } - pub fn make_entries_by_index(&mut self, index: usize) { - let short; - let positional; - let mut longs: Vec<_>; - - let arg = &self.args[index]; - short = arg.short.map(KeyType::Short); - positional = arg.index.map(KeyType::Position); - - longs = arg - .aliases - .clone() - .map(|v| { - v.iter() - .map(|(n, _)| KeyType::Long(OsString::from(n))) - .collect() - }) - .unwrap_or_default(); - longs.extend(arg.long.map(|l| KeyType::Long(OsString::from(l)))); - - if let Some(s) = short { - self.insert_key(s, index) - } - if let Some(p) = positional { - self.insert_key(p, index) - } - } - - pub fn find_by_name(&mut self, name: &str) -> usize { - self.args - .iter() - .position(|x| x.name == name) - .expect("No such name found") - } - - pub fn remove(&mut self, key: &KeyType) -> Option> { - if self.built { - panic!("Cannot remove args after being built"); - } - - let idx = self.keys.iter().position(|k| k.key == *key); - - if let Some(idx) = idx { - let arg = self.args.swap_remove(idx); - for key in _get_keys(&arg) { - self.remove_key(&key); - } - return Some(arg); - } - None - } - //TODO ::remove_many([KeyA, KeyB]) //? probably shouldn't add a possibility for removal? //? or remove by replacement by some dummy object, so the order is preserved - pub fn remove_by_name(&mut self, _name: Id) -> Option> { + pub(crate) fn remove_by_name(&mut self, _name: Id) -> Option> { if self.built { panic!("Cannot remove args after being built"); } @@ -239,130 +147,3 @@ fn _get_keys(arg: &Arg) -> Vec { keys } - -#[cfg(test)] -mod tests { - use self::KeyType::*; - use super::*; - - #[test] - fn get_some_value() { - let mut map: MKeyMap = MKeyMap::new(); - - map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - assert_eq!( - map.get(&Long(OsString::from("One"))), - Some(&Arg::with_name("Value1")) - ); - } - - #[test] - fn get_none_value() { - let mut map: MKeyMap = MKeyMap::new(); - - map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - map.get(&Long(OsString::from("Two"))); - - assert_eq!(map.get(&Long(OsString::from("Two"))), None); - } - - // #[test] - // fn insert_delete_value() { - // let mut map = MKeyMap::new(); - // map.insert("One", clap::Arg::with_name("Value1")); - // assert_eq!(map.remove("One"), Some(clap::Arg::with_name("Value1"))); - // assert!(map.is_empty()); - // } - - #[test] - fn insert_duplicate_key() { - let mut map: MKeyMap = MKeyMap::new(); - - map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - assert_eq!( - map.insert(Long(OsString::from("One")), Arg::with_name("Value2")), - 1 - ); - } - - #[test] - // #[should_panic(expected = "Len changed")] - fn insert_duplicate_value() { - let mut map: MKeyMap = MKeyMap::new(); - - map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - let orig_len = map.args.len(); - - map.insert(Long(OsString::from("Two")), Arg::with_name("Value1")); - - assert_eq!(map.args.len(), orig_len + 1 /* , "Len changed" */); - // assert_eq!( - // map.get(&Long(OsString::from("One"))), - // map.get(&Long(OsString::from("Two"))) - // ); - } - - // #[test] - // fn insert_delete_none() { - // let mut map = MKeyMap::new(); - // map.insert("One", clap::Arg::with_name("Value1")); - // assert_eq!(map.remove("Two"), None); - // assert!(!map.is_empty()); - // assert_eq!(map.get("One"), Some(clap::Arg::with_name("Value1"))); - // } - - #[test] - fn insert_multiple_keys() { - let mut map: MKeyMap = MKeyMap::new(); - let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - map.insert_key(Long(OsString::from("Two")), index); - - assert_eq!( - map.get(&Long(OsString::from("One"))), - map.get(&Long(OsString::from("Two"))) - ); - assert_eq!(map.args.len(), 1); - } - - // #[test] - // fn insert_by_name() { - // let mut map: MKeyMap = MKeyMap::new(); - // let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - // map.insert_key_by_name(Long(OsString::from("Two")), "Value1"); - - // assert_eq!( - // map.get(Long(OsString::from("One"))), - // map.get(Long(OsString::from("Two"))) - // ); - // assert_eq!(map.values.len(), 1); - // } - - #[test] - fn get_mutable() { - let mut map: MKeyMap = MKeyMap::new(); - - map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - assert_eq!( - map.get_mut(&Long(OsString::from("One"))), - Some(&mut Arg::with_name("Value1")) - ); - } - - #[test] - fn remove_key() { - let mut map: MKeyMap = MKeyMap::new(); - let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1")); - - map.insert_key(Long(OsString::from("Two")), index); - map.remove_key(&Long(OsString::from("One"))); - - assert_eq!(map.keys.len(), 1); - assert_eq!(map.args.len(), 1); - } -} diff --git a/src/output/fmt.rs b/src/output/fmt.rs index 4f1de0fa..4f8e324d 100644 --- a/src/output/fmt.rs +++ b/src/output/fmt.rs @@ -9,8 +9,8 @@ use atty; use std::env; use std::fmt; -#[doc(hidden)] #[derive(Debug, Copy, Clone, PartialEq)] +#[doc(hidden)] pub enum ColorWhen { Auto, Always, @@ -18,7 +18,7 @@ pub enum ColorWhen { } #[cfg(feature = "color")] -pub fn is_a_tty(stderr: bool) -> bool { +pub(crate) fn is_a_tty(stderr: bool) -> bool { debugln!("is_a_tty: stderr={:?}", stderr); let stream = if stderr { atty::Stream::Stderr @@ -29,23 +29,21 @@ pub fn is_a_tty(stderr: bool) -> bool { } #[cfg(not(feature = "color"))] -pub fn is_a_tty(_: bool) -> bool { +pub(crate) fn is_a_tty(_: bool) -> bool { debugln!("is_a_tty;"); false } -pub fn is_term_dumb() -> bool { +pub(crate) fn is_term_dumb() -> bool { env::var("TERM").ok() == Some(String::from("dumb")) } -#[doc(hidden)] -pub struct ColorizerOption { - pub use_stderr: bool, - pub when: ColorWhen, +pub(crate) struct ColorizerOption { + pub(crate) use_stderr: bool, + pub(crate) when: ColorWhen, } -#[doc(hidden)] -pub struct Colorizer { +pub(crate) struct Colorizer { when: ColorWhen, } @@ -60,7 +58,7 @@ macro_rules! color { } impl Colorizer { - pub fn new(option: &ColorizerOption) -> Colorizer { + pub(crate) fn new(option: &ColorizerOption) -> Colorizer { let is_a_tty = is_a_tty(option.use_stderr); let is_term_dumb = is_term_dumb(); Colorizer { @@ -72,7 +70,7 @@ impl Colorizer { } } - pub fn good(&self, msg: T) -> Format + pub(crate) fn good(&self, msg: T) -> Format where T: fmt::Display + AsRef, { @@ -80,7 +78,7 @@ impl Colorizer { color!(self, Good, msg) } - pub fn warning(&self, msg: T) -> Format + pub(crate) fn warning(&self, msg: T) -> Format where T: fmt::Display + AsRef, { @@ -88,7 +86,7 @@ impl Colorizer { color!(self, Warning, msg) } - pub fn error(&self, msg: T) -> Format + pub(crate) fn error(&self, msg: T) -> Format where T: fmt::Display + AsRef, { @@ -96,7 +94,7 @@ impl Colorizer { color!(self, Error, msg) } - pub fn none(&self, msg: T) -> Format + pub(crate) fn none(&self, msg: T) -> Format where T: fmt::Display + AsRef, { @@ -117,8 +115,7 @@ impl Default for Colorizer { /// Defines styles for different types of error messages. Defaults to Error=Red, Warning=Yellow, /// and Good=Green #[derive(Debug)] -#[doc(hidden)] -pub enum Format { +pub(crate) enum Format { /// Defines the style used for errors, defaults to Red Error(T), /// Defines the style used for warnings, defaults to Yellow diff --git a/src/output/help.rs b/src/output/help.rs index 3de4cd62..4a29d323 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -22,7 +22,7 @@ use unicode_width::UnicodeWidthStr; #[cfg(not(feature = "wrap_help"))] mod term_size { - pub fn dimensions() -> Option<(usize, usize)> { + pub(crate) fn dimensions() -> Option<(usize, usize)> { None } } @@ -36,7 +36,7 @@ const TAB: &str = " "; /// `clap` Help Writer. /// /// Wraps a writer stream providing different methods to generate help for `clap` objects. -pub struct Help<'b, 'c, 'd, 'w> { +pub(crate) struct Help<'b, 'c, 'd, 'w> { writer: &'w mut dyn Write, parser: &'d Parser<'b, 'c>, next_line_help: bool, @@ -52,7 +52,7 @@ pub struct Help<'b, 'c, 'd, 'w> { // Public Functions impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { /// Create a new `Help` instance. - pub fn new( + pub(crate) fn new( w: &'w mut dyn Write, parser: &'d Parser<'b, 'c>, use_long: bool, @@ -92,7 +92,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { } /// Writes the parser help to the wrapped stream. - pub fn write_help(&mut self) -> ClapResult<()> { + pub(crate) fn write_help(&mut self) -> ClapResult<()> { debugln!("Help::write_help;"); if let Some(h) = self.parser.app.help_str { write!(self.writer, "{}", h).map_err(Error::from)?; @@ -639,7 +639,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { /// Writes help for all arguments (options, flags, args, subcommands) /// including titles of a Parser Object to the wrapped stream. - pub fn write_all_args(&mut self) -> ClapResult<()> { + pub(crate) fn write_all_args(&mut self) -> ClapResult<()> { debugln!("Help::write_all_args;"); let flags = self.parser.has_flags(); // Strange filter/count vs fold... https://github.com/rust-lang/rust/issues/33038 @@ -804,7 +804,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { } /// Writes default help for a Parser Object to the wrapped stream. - pub fn write_default_help(&mut self) -> ClapResult<()> { + pub(crate) fn write_default_help(&mut self) -> ClapResult<()> { debugln!("Help::write_default_help;"); if let Some(h) = self.parser.app.pre_help { self.write_before_after_help(h)?; diff --git a/src/output/mod.rs b/src/output/mod.rs index 71db550a..44acfe1f 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -3,5 +3,5 @@ mod usage; pub mod fmt; -pub use self::help::Help; -pub use self::usage::Usage; +pub(crate) use self::help::Help; +pub(crate) use self::usage::Usage; diff --git a/src/output/usage.rs b/src/output/usage.rs index 2b540947..82cc3e1f 100644 --- a/src/output/usage.rs +++ b/src/output/usage.rs @@ -9,7 +9,7 @@ use crate::INTERNAL_ERROR_MSG; type Id = u64; -pub struct Usage<'b, 'c, 'z> +pub(crate) struct Usage<'b, 'c, 'z> where 'b: 'c, 'c: 'z, @@ -18,13 +18,13 @@ where } impl<'b, 'c, 'z> Usage<'b, 'c, 'z> { - pub fn new(p: &'z Parser<'b, 'c>) -> Self { + pub(crate) fn new(p: &'z Parser<'b, 'c>) -> Self { Usage { p } } // Creates a usage string for display. This happens just after all arguments were parsed, but before // any subcommands have been parsed (so as to give subcommands their own usage recursively) - pub fn create_usage_with_title(&self, used: &[Id]) -> String { + pub(crate) fn create_usage_with_title(&self, used: &[Id]) -> String { debugln!("usage::create_usage_with_title;"); let mut usage = String::with_capacity(75); usage.push_str("USAGE:\n "); @@ -33,7 +33,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> { } // Creates a usage string (*without title*) if one was not provided by the user manually. - pub fn create_usage_no_title(&self, used: &[Id]) -> String { + pub(crate) fn create_usage_no_title(&self, used: &[Id]) -> String { debugln!("usage::create_usage_no_title;"); if let Some(u) = self.p.app.usage_str { String::from(&*u) @@ -45,7 +45,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> { } // Creates a usage string for display in help messages (i.e. not for errors) - pub fn create_help_usage(&self, incl_reqs: bool) -> String { + pub(crate) fn create_help_usage(&self, incl_reqs: bool) -> String { debugln!("Usage::create_help_usage; incl_reqs={:?}", incl_reqs); let mut usage = String::with_capacity(75); let name = self @@ -310,7 +310,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> { // `incl_last`: should we incldue args that are Arg::Last? (i.e. `prog [foo] -- [last]). We // can't do that for required usages being built for subcommands because it would look like: // `prog [foo] -- [last] ` which is totally wrong. - pub fn get_required_usage_from( + pub(crate) fn get_required_usage_from( &self, incls: &[Id], matcher: Option<&ArgMatcher>, diff --git a/src/parse/parser.rs b/src/parse/parser.rs index a08e0fd4..caecdfda 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -11,8 +11,7 @@ use crate::build::app::Propagation; use crate::build::AppSettings as AS; use crate::build::{App, Arg, ArgSettings}; use crate::mkeymap::KeyType; -use crate::output::Help; -use crate::output::Usage; +use crate::output::{Help, Usage}; use crate::parse::errors::Error as ClapError; use crate::parse::errors::ErrorKind; use crate::parse::errors::Result as ClapResult;