From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/clap_builder/src/builder/resettable.rs | 212 ++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 vendor/clap_builder/src/builder/resettable.rs (limited to 'vendor/clap_builder/src/builder/resettable.rs') diff --git a/vendor/clap_builder/src/builder/resettable.rs b/vendor/clap_builder/src/builder/resettable.rs new file mode 100644 index 0000000..0572740 --- /dev/null +++ b/vendor/clap_builder/src/builder/resettable.rs @@ -0,0 +1,212 @@ +// Unlike `impl Into>` or `Option>`, this isn't ambiguous for the `None` +// case. + +use crate::builder::ArgAction; +use crate::builder::OsStr; +use crate::builder::Str; +use crate::builder::StyledStr; +use crate::builder::ValueHint; +use crate::builder::ValueParser; +use crate::builder::ValueRange; + +/// Clearable builder value +/// +/// This allows a builder function to both accept any value that can [`Into::into`] `T` (like +/// `&str` into `OsStr`) as well as `None` to reset it to the default. This is needed to +/// workaround a limitation where you can't have a function argument that is `impl Into>` +/// where `T` is `impl Into` accept `None` as its type is ambiguous. +/// +/// # Example +/// +/// ```rust +/// # use clap_builder as clap; +/// # use clap::Command; +/// # use clap::Arg; +/// fn common() -> Command { +/// Command::new("cli") +/// .arg(Arg::new("input").short('i').long("input")) +/// } +/// let mut command = common(); +/// command.mut_arg("input", |arg| arg.short(None)); +/// ``` +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Resettable { + /// Overwrite builder value + Value(T), + /// Reset builder value + Reset, +} + +impl Resettable { + pub(crate) fn into_option(self) -> Option { + match self { + Self::Value(t) => Some(t), + Self::Reset => None, + } + } +} + +impl From for Resettable { + fn from(other: T) -> Self { + Self::Value(other) + } +} + +impl From> for Resettable { + fn from(other: Option) -> Self { + match other { + Some(inner) => Self::Value(inner), + None => Self::Reset, + } + } +} + +/// Convert to the intended resettable type +pub trait IntoResettable { + /// Convert to the intended resettable type + fn into_resettable(self) -> Resettable; +} + +impl IntoResettable for Option { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option<&'static str> { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s.into()), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option<&'static str> { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s.into()), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Option<&'static str> { + fn into_resettable(self) -> Resettable { + match self { + Some(s) => Resettable::Value(s.into()), + None => Resettable::Reset, + } + } +} + +impl IntoResettable for Resettable { + fn into_resettable(self) -> Resettable { + self + } +} + +impl IntoResettable for char { + fn into_resettable(self) -> Resettable { + Resettable::Value(self) + } +} + +impl IntoResettable for usize { + fn into_resettable(self) -> Resettable { + Resettable::Value(self) + } +} + +impl IntoResettable for ArgAction { + fn into_resettable(self) -> Resettable { + Resettable::Value(self) + } +} + +impl IntoResettable for ValueHint { + fn into_resettable(self) -> Resettable { + Resettable::Value(self) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} + +impl> IntoResettable for I { + fn into_resettable(self) -> Resettable { + Resettable::Value(self.into()) + } +} -- cgit v1.2.3