From 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:39 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/clap_builder/src/util/color.rs | 109 ++++++++++++ vendor/clap_builder/src/util/flat_map.rs | 254 ++++++++++++++++++++++++++++ vendor/clap_builder/src/util/flat_set.rs | 107 ++++++++++++ vendor/clap_builder/src/util/graph.rs | 49 ++++++ vendor/clap_builder/src/util/id.rs | 164 ++++++++++++++++++ vendor/clap_builder/src/util/mod.rs | 44 +++++ vendor/clap_builder/src/util/str_to_bool.rs | 21 +++ 7 files changed, 748 insertions(+) create mode 100644 vendor/clap_builder/src/util/color.rs create mode 100644 vendor/clap_builder/src/util/flat_map.rs create mode 100644 vendor/clap_builder/src/util/flat_set.rs create mode 100644 vendor/clap_builder/src/util/graph.rs create mode 100644 vendor/clap_builder/src/util/id.rs create mode 100644 vendor/clap_builder/src/util/mod.rs create mode 100644 vendor/clap_builder/src/util/str_to_bool.rs (limited to 'vendor/clap_builder/src/util') diff --git a/vendor/clap_builder/src/util/color.rs b/vendor/clap_builder/src/util/color.rs new file mode 100644 index 000000000..7a0e837e7 --- /dev/null +++ b/vendor/clap_builder/src/util/color.rs @@ -0,0 +1,109 @@ +use crate::builder::PossibleValue; +use crate::derive::ValueEnum; + +/// Represents the color preferences for program output +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum ColorChoice { + /// Enables colored output only when the output is going to a terminal or TTY. + /// + /// **NOTE:** This is the default behavior of `clap`. + /// + /// # Platform Specific + /// + /// This setting only applies to Unix, Linux, and macOS (i.e. non-Windows platforms). + /// + /// # Examples + /// + /// ```rust + /// # #[cfg(feature = "color")] { + /// # use clap_builder as clap; + /// # use clap::{Command, ColorChoice}; + /// Command::new("myprog") + /// .color(ColorChoice::Auto) + /// .get_matches(); + /// # } + /// ``` + Auto, + + /// Enables colored output regardless of whether or not the output is going to a terminal/TTY. + /// + /// # Platform Specific + /// + /// This setting only applies to Unix, Linux, and macOS (i.e. non-Windows platforms). + /// + /// # Examples + /// + /// ```rust + /// # #[cfg(feature = "color")] { + /// # use clap_builder as clap; + /// # use clap::{Command, ColorChoice}; + /// Command::new("myprog") + /// .color(ColorChoice::Always) + /// .get_matches(); + /// # } + /// ``` + Always, + + /// Disables colored output no matter if the output is going to a terminal/TTY, or not. + /// + /// # Platform Specific + /// + /// This setting only applies to Unix, Linux, and macOS (i.e. non-Windows platforms) + /// + /// # Examples + /// + /// ```rust + /// # #[cfg(feature = "color")] { + /// # use clap_builder as clap; + /// # use clap::{Command, ColorChoice}; + /// Command::new("myprog") + /// .color(ColorChoice::Never) + /// .get_matches(); + /// # } + /// ``` + Never, +} + +impl Default for ColorChoice { + fn default() -> Self { + Self::Auto + } +} + +impl std::fmt::Display for ColorChoice { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.to_possible_value() + .expect("no values are skipped") + .get_name() + .fmt(f) + } +} + +impl std::str::FromStr for ColorChoice { + type Err = String; + + fn from_str(s: &str) -> Result { + for variant in Self::value_variants() { + if variant.to_possible_value().unwrap().matches(s, false) { + return Ok(*variant); + } + } + Err(format!("invalid variant: {s}")) + } +} + +impl ValueEnum for ColorChoice { + fn value_variants<'a>() -> &'a [Self] { + &[Self::Auto, Self::Always, Self::Never] + } + + fn to_possible_value(&self) -> Option { + Some(match self { + Self::Auto => { + PossibleValue::new("auto").help("Use colored output if writing to a terminal/TTY") + } + Self::Always => PossibleValue::new("always").help("Always use colored output"), + Self::Never => PossibleValue::new("never").help("Never use colored output"), + }) + } +} diff --git a/vendor/clap_builder/src/util/flat_map.rs b/vendor/clap_builder/src/util/flat_map.rs new file mode 100644 index 000000000..468f0a9d8 --- /dev/null +++ b/vendor/clap_builder/src/util/flat_map.rs @@ -0,0 +1,254 @@ +#![allow(dead_code)] + +use std::borrow::Borrow; + +/// Flat (Vec) backed map +/// +/// This preserves insertion order +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct FlatMap { + keys: Vec, + values: Vec, +} + +impl FlatMap { + pub(crate) fn new() -> Self { + Default::default() + } + + pub(crate) fn insert(&mut self, key: K, mut value: V) -> Option { + for (index, existing) in self.keys.iter().enumerate() { + if *existing == key { + std::mem::swap(&mut self.values[index], &mut value); + return Some(value); + } + } + + self.insert_unchecked(key, value); + None + } + + pub(crate) fn insert_unchecked(&mut self, key: K, value: V) { + self.keys.push(key); + self.values.push(value); + } + + pub(crate) fn extend_unchecked(&mut self, iter: impl IntoIterator) { + for (key, value) in iter { + self.insert_unchecked(key, value); + } + } + + pub fn contains_key(&self, key: &Q) -> bool + where + K: Borrow, + Q: Eq, + { + for existing in &self.keys { + if existing.borrow() == key { + return true; + } + } + false + } + + pub fn remove(&mut self, key: &Q) -> Option + where + K: Borrow, + Q: std::hash::Hash + Eq, + { + self.remove_entry(key).map(|(_, v)| v) + } + + pub fn remove_entry(&mut self, key: &Q) -> Option<(K, V)> + where + K: Borrow, + Q: std::hash::Hash + Eq, + { + let index = some!(self + .keys + .iter() + .enumerate() + .find_map(|(i, k)| (k.borrow() == key).then_some(i))); + let key = self.keys.remove(index); + let value = self.values.remove(index); + Some((key, value)) + } + + pub(crate) fn is_empty(&self) -> bool { + self.keys.is_empty() + } + + pub fn entry(&mut self, key: K) -> Entry { + for (index, existing) in self.keys.iter().enumerate() { + if *existing == key { + return Entry::Occupied(OccupiedEntry { v: self, index }); + } + } + Entry::Vacant(VacantEntry { v: self, key }) + } + + pub fn get(&self, k: &Q) -> Option<&V> + where + K: Borrow, + Q: Eq, + { + for (index, existing) in self.keys.iter().enumerate() { + if existing.borrow() == k { + return Some(&self.values[index]); + } + } + None + } + + pub fn get_mut(&mut self, k: &Q) -> Option<&mut V> + where + K: Borrow, + Q: Eq, + { + for (index, existing) in self.keys.iter().enumerate() { + if existing.borrow() == k { + return Some(&mut self.values[index]); + } + } + None + } + + pub fn keys(&self) -> std::slice::Iter<'_, K> { + self.keys.iter() + } + + pub fn iter(&self) -> Iter { + Iter { + keys: self.keys.iter(), + values: self.values.iter(), + } + } + + pub fn iter_mut(&mut self) -> IterMut { + IterMut { + keys: self.keys.iter_mut(), + values: self.values.iter_mut(), + } + } +} + +impl Default for FlatMap { + fn default() -> Self { + Self { + keys: Default::default(), + values: Default::default(), + } + } +} + +pub enum Entry<'a, K: 'a, V: 'a> { + Vacant(VacantEntry<'a, K, V>), + Occupied(OccupiedEntry<'a, K, V>), +} + +impl<'a, K: 'a, V: 'a> Entry<'a, K, V> { + pub fn or_insert(self, default: V) -> &'a mut V { + match self { + Entry::Occupied(entry) => &mut entry.v.values[entry.index], + Entry::Vacant(entry) => { + entry.v.keys.push(entry.key); + entry.v.values.push(default); + entry.v.values.last_mut().unwrap() + } + } + } + + pub fn or_insert_with V>(self, default: F) -> &'a mut V { + match self { + Entry::Occupied(entry) => &mut entry.v.values[entry.index], + Entry::Vacant(entry) => { + entry.v.keys.push(entry.key); + entry.v.values.push(default()); + entry.v.values.last_mut().unwrap() + } + } + } +} + +pub struct VacantEntry<'a, K: 'a, V: 'a> { + v: &'a mut FlatMap, + key: K, +} + +pub struct OccupiedEntry<'a, K: 'a, V: 'a> { + v: &'a mut FlatMap, + index: usize, +} + +pub struct Iter<'a, K: 'a, V: 'a> { + keys: std::slice::Iter<'a, K>, + values: std::slice::Iter<'a, V>, +} + +impl<'a, K, V> Iterator for Iter<'a, K, V> { + type Item = (&'a K, &'a V); + + fn next(&mut self) -> Option<(&'a K, &'a V)> { + match self.keys.next() { + Some(k) => { + let v = self.values.next().unwrap(); + Some((k, v)) + } + None => None, + } + } + fn size_hint(&self) -> (usize, Option) { + self.keys.size_hint() + } +} + +impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> { + fn next_back(&mut self) -> Option<(&'a K, &'a V)> { + match self.keys.next_back() { + Some(k) => { + let v = self.values.next_back().unwrap(); + Some((k, v)) + } + None => None, + } + } +} + +impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {} + +pub struct IterMut<'a, K: 'a, V: 'a> { + keys: std::slice::IterMut<'a, K>, + values: std::slice::IterMut<'a, V>, +} + +impl<'a, K, V> Iterator for IterMut<'a, K, V> { + type Item = (&'a K, &'a mut V); + + fn next(&mut self) -> Option<(&'a K, &'a mut V)> { + match self.keys.next() { + Some(k) => { + let v = self.values.next().unwrap(); + Some((k, v)) + } + None => None, + } + } + fn size_hint(&self) -> (usize, Option) { + self.keys.size_hint() + } +} + +impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> { + fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> { + match self.keys.next_back() { + Some(k) => { + let v = self.values.next_back().unwrap(); + Some((k, v)) + } + None => None, + } + } +} + +impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {} diff --git a/vendor/clap_builder/src/util/flat_set.rs b/vendor/clap_builder/src/util/flat_set.rs new file mode 100644 index 000000000..3e0b23dae --- /dev/null +++ b/vendor/clap_builder/src/util/flat_set.rs @@ -0,0 +1,107 @@ +#![allow(dead_code)] + +use std::borrow::Borrow; + +/// Flat (Vec) backed set +/// +/// This preserves insertion order +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct FlatSet { + inner: Vec, +} + +impl FlatSet { + pub(crate) fn new() -> Self { + Default::default() + } + + pub(crate) fn insert(&mut self, value: T) -> bool { + for existing in &self.inner { + if *existing == value { + return false; + } + } + self.inner.push(value); + true + } + + pub fn contains(&self, value: &Q) -> bool + where + T: Borrow, + Q: Eq, + { + for existing in &self.inner { + if existing.borrow() == value { + return true; + } + } + false + } + + pub fn retain(&mut self, f: F) + where + F: FnMut(&T) -> bool, + { + self.inner.retain(f); + } + + pub(crate) fn is_empty(&self) -> bool { + self.inner.is_empty() + } + + pub(crate) fn iter(&self) -> std::slice::Iter<'_, T> { + self.inner.iter() + } + + pub fn sort_by_key(&mut self, f: F) + where + F: FnMut(&T) -> K, + K: Ord, + { + self.inner.sort_by_key(f); + } +} + +impl Default for FlatSet { + fn default() -> Self { + Self { + inner: Default::default(), + } + } +} + +impl IntoIterator for FlatSet { + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.inner.into_iter() + } +} + +impl<'s, T: PartialEq + Eq> IntoIterator for &'s FlatSet { + type Item = &'s T; + type IntoIter = std::slice::Iter<'s, T>; + + fn into_iter(self) -> Self::IntoIter { + self.inner.iter() + } +} + +impl Extend for FlatSet { + fn extend>(&mut self, iter: I) { + for value in iter { + self.insert(value); + } + } +} + +impl FromIterator for FlatSet { + fn from_iter>(iter: I) -> Self { + let mut set = Self::new(); + for value in iter { + set.insert(value); + } + set + } +} diff --git a/vendor/clap_builder/src/util/graph.rs b/vendor/clap_builder/src/util/graph.rs new file mode 100644 index 000000000..d646400b0 --- /dev/null +++ b/vendor/clap_builder/src/util/graph.rs @@ -0,0 +1,49 @@ +#[derive(Debug)] +struct Child { + id: T, + children: Vec, +} + +impl Child { + fn new(id: T) -> Self { + Child { + id, + children: vec![], + } + } +} + +#[derive(Debug)] +pub(crate) struct ChildGraph(Vec>); + +impl ChildGraph +where + T: Sized + PartialEq + Clone, +{ + pub(crate) fn with_capacity(s: usize) -> Self { + ChildGraph(Vec::with_capacity(s)) + } + + pub(crate) fn insert(&mut self, req: T) -> usize { + self.0.iter().position(|e| e.id == req).unwrap_or_else(|| { + let idx = self.0.len(); + self.0.push(Child::new(req)); + idx + }) + } + + pub(crate) fn insert_child(&mut self, parent: usize, child: T) -> usize { + let c_idx = self.0.len(); + self.0.push(Child::new(child)); + self.0[parent].children.push(c_idx); + c_idx + } + + pub(crate) fn iter(&self) -> impl Iterator { + self.0.iter().map(|r| &r.id) + } + + pub(crate) fn contains(&self, req: &T) -> bool { + self.0.iter().any(|r| r.id == *req) + } +} diff --git a/vendor/clap_builder/src/util/id.rs b/vendor/clap_builder/src/util/id.rs new file mode 100644 index 000000000..710d2ea7d --- /dev/null +++ b/vendor/clap_builder/src/util/id.rs @@ -0,0 +1,164 @@ +use crate::builder::Str; + +/// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier +/// +/// This is used for accessing the value in [`ArgMatches`][crate::ArgMatches] or defining +/// relationships between `Arg`s and `ArgGroup`s with functions like +/// [`Arg::conflicts_with`][crate::Arg::conflicts_with]. +#[derive(Default, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] +pub struct Id(Str); + +impl Id { + pub(crate) const HELP: &'static str = "help"; + pub(crate) const VERSION: &'static str = "version"; + pub(crate) const EXTERNAL: &'static str = ""; + + pub(crate) fn from_static_ref(name: &'static str) -> Self { + Self(Str::from_static_ref(name)) + } + + /// Get the raw string of the `Id` + pub fn as_str(&self) -> &str { + self.0.as_str() + } + + pub(crate) fn as_internal_str(&self) -> &Str { + &self.0 + } +} + +impl From<&'_ Id> for Id { + fn from(id: &'_ Id) -> Self { + id.clone() + } +} + +impl From for Id { + fn from(name: Str) -> Self { + Self(name) + } +} + +impl From<&'_ Str> for Id { + fn from(name: &'_ Str) -> Self { + Self(name.into()) + } +} + +#[cfg(feature = "string")] +impl From for Id { + fn from(name: std::string::String) -> Self { + Self(name.into()) + } +} + +#[cfg(feature = "string")] +impl From<&'_ std::string::String> for Id { + fn from(name: &'_ std::string::String) -> Self { + Self(name.into()) + } +} + +impl From<&'static str> for Id { + fn from(name: &'static str) -> Self { + Self(name.into()) + } +} + +impl From<&'_ &'static str> for Id { + fn from(name: &'_ &'static str) -> Self { + Self(name.into()) + } +} + +impl From for Str { + fn from(name: Id) -> Self { + name.0 + } +} + +impl From for String { + fn from(name: Id) -> Self { + Str::from(name).into() + } +} + +impl std::fmt::Display for Id { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.as_str(), f) + } +} + +impl std::fmt::Debug for Id { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(self.as_str(), f) + } +} + +impl AsRef for Id { + #[inline] + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl std::borrow::Borrow for Id { + #[inline] + fn borrow(&self) -> &str { + self.as_str() + } +} + +impl PartialEq for Id { + #[inline] + fn eq(&self, other: &str) -> bool { + PartialEq::eq(self.as_str(), other) + } +} +impl PartialEq for str { + #[inline] + fn eq(&self, other: &Id) -> bool { + PartialEq::eq(self, other.as_str()) + } +} + +impl PartialEq<&'_ str> for Id { + #[inline] + fn eq(&self, other: &&str) -> bool { + PartialEq::eq(self.as_str(), *other) + } +} +impl PartialEq for &'_ str { + #[inline] + fn eq(&self, other: &Id) -> bool { + PartialEq::eq(*self, other.as_str()) + } +} + +impl PartialEq for Id { + #[inline] + fn eq(&self, other: &Str) -> bool { + PartialEq::eq(self.as_str(), other.as_str()) + } +} +impl PartialEq for Str { + #[inline] + fn eq(&self, other: &Id) -> bool { + PartialEq::eq(self.as_str(), other.as_str()) + } +} + +impl PartialEq for Id { + #[inline] + fn eq(&self, other: &std::string::String) -> bool { + PartialEq::eq(self.as_str(), other.as_str()) + } +} +impl PartialEq for std::string::String { + #[inline] + fn eq(&self, other: &Id) -> bool { + PartialEq::eq(other, self) + } +} diff --git a/vendor/clap_builder/src/util/mod.rs b/vendor/clap_builder/src/util/mod.rs new file mode 100644 index 000000000..e6a8f70ed --- /dev/null +++ b/vendor/clap_builder/src/util/mod.rs @@ -0,0 +1,44 @@ +#![allow(clippy::single_component_path_imports)] + +pub(crate) mod flat_map; +pub(crate) mod flat_set; +mod graph; +mod id; +mod str_to_bool; + +pub use self::id::Id; + +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; +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; + +pub(crate) mod color; + +pub(crate) const SUCCESS_CODE: i32 = 0; +// While sysexists.h defines EX_USAGE as 64, this doesn't seem to be used much in practice but +// instead 2 seems to be frequently used. +// Examples +// - GNU `ls` returns 2 +// - Python's `argparse` returns 2 +pub(crate) const USAGE_CODE: i32 = 2; + +pub(crate) fn safe_exit(code: i32) -> ! { + use std::io::Write; + + let _ = std::io::stdout().lock().flush(); + let _ = std::io::stderr().lock().flush(); + + std::process::exit(code) +} + +#[cfg(not(feature = "unicode"))] +pub(crate) fn eq_ignore_case(left: &str, right: &str) -> bool { + left.eq_ignore_ascii_case(right) +} + +#[cfg(feature = "unicode")] +pub(crate) use unicase::eq as eq_ignore_case; diff --git a/vendor/clap_builder/src/util/str_to_bool.rs b/vendor/clap_builder/src/util/str_to_bool.rs new file mode 100644 index 000000000..1fbdc7531 --- /dev/null +++ b/vendor/clap_builder/src/util/str_to_bool.rs @@ -0,0 +1,21 @@ +/// True values are `y`, `yes`, `t`, `true`, `on`, and `1`. +pub(crate) const TRUE_LITERALS: [&str; 6] = ["y", "yes", "t", "true", "on", "1"]; + +/// False values are `n`, `no`, `f`, `false`, `off`, and `0`. +pub(crate) const FALSE_LITERALS: [&str; 6] = ["n", "no", "f", "false", "off", "0"]; + +/// Converts a string literal representation of truth to true or false. +/// +/// `false` values are `n`, `no`, `f`, `false`, `off`, and `0` (case insensitive). +/// +/// Any other value will be considered as `true`. +pub(crate) fn str_to_bool(val: impl AsRef) -> Option { + let pat: &str = &val.as_ref().to_lowercase(); + if TRUE_LITERALS.contains(&pat) { + Some(true) + } else if FALSE_LITERALS.contains(&pat) { + Some(false) + } else { + None + } +} -- cgit v1.2.3