diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-17 00:12:48 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-17 00:12:48 +0800 |
| commit | 73c01a36faced96cd99ef6696cb2e44e8d8e5310 (patch) | |
| tree | ecc5ec80ab660c8c1cb6fba37d7f9bfb88f2d5e6 | |
| parent | d31a8181bd4ac80ef678c29e6fab5607628bb76e (diff) | |
refactor(value): extract Flag into its own module
| -rw-r--r-- | mingling_picker/src/value.rs | 147 | ||||
| -rw-r--r-- | mingling_picker/src/value/flag.rs | 145 |
2 files changed, 147 insertions, 145 deletions
diff --git a/mingling_picker/src/value.rs b/mingling_picker/src/value.rs index ee0d6ee..c16e6a9 100644 --- a/mingling_picker/src/value.rs +++ b/mingling_picker/src/value.rs @@ -1,145 +1,2 @@ -use std::{ - fmt::{Debug, Display}, - ops::{Deref, Not}, -}; - -/// Parsed result of a boolean-style command-line flag. -/// -/// `Flag` is a **value type** that can be declared in [`PickerArg`]. -/// When the user passes `--verbose` on the command line, the parsed result is `Flag::Active`; -/// when the flag is absent, the result is `Flag::Inactive`. -/// -/// # Why not just `bool`? -/// -/// Unlike a raw `bool`, `Flag` carries **explicit semantics** about whether -/// the flag was actually provided by the user (`Active`) or simply omitted -/// (`Inactive`). This distinction matters when you want to distinguish -/// "the user intentionally omitted the flag" from "the flag was processed but -/// resolved to false" — the `Pickable` implementation for `Flag` always -/// returns `Parsed(Flag::Inactive)` when no matching argument is found, -/// rather than `NotFound`, making it always succeed with a meaningful default. -/// -/// # Conversions -/// -/// `Flag` interoperates seamlessly with `bool`: `Flag::Active` is `true`, -/// `Flag::Inactive` is `false`. The [`Deref`] impl allows using a `Flag` -/// directly in boolean contexts: -/// -/// ``` -/// # use mingling_picker::value::Flag; -/// let flag = Flag::Active; -/// if *flag { /* runs */ } -/// ``` -/// -/// [`PickerArg`]: crate::PickerArg -#[derive(Default, Clone, Copy, PartialEq, Eq)] -pub enum Flag { - /// The flag was **not** present on the command line. - /// - /// This is the default state, equivalent to `false`. - #[default] - Inactive, - - /// The flag **was** present on the command line. - /// - /// Equivalent to `true`. - Active, -} - -impl Debug for Flag { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Inactive => write!(f, "inactive"), - Self::Active => write!(f, "active"), - } - } -} - -impl Display for Flag { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Inactive => write!(f, "inactive"), - Self::Active => write!(f, "active"), - } - } -} - -impl Flag { - /// Converts this `Flag` into a `bool`. - /// - /// Returns `true` if the flag is [`Active`], `false` if [`Inactive`]. - /// - /// # Examples - /// - /// ``` - /// # use mingling_picker::value::Flag; - /// assert!(Flag::Active.bool()); - /// assert!(!Flag::Inactive.bool()); - /// ``` - /// - /// [`Active`]: Flag::Active - /// [`Inactive`]: Flag::Inactive - #[must_use] - #[inline(always)] - pub fn bool(&self) -> bool { - *self == Flag::Active - } -} - -impl PartialEq<bool> for Flag { - fn eq(&self, other: &bool) -> bool { - self.bool() == *other - } -} - -/// Compares `bool` with `Flag` using `==`. -impl PartialEq<Flag> for bool { - fn eq(&self, other: &Flag) -> bool { - *self == other.bool() - } -} - -impl From<bool> for Flag { - fn from(value: bool) -> Self { - if value { Flag::Active } else { Flag::Inactive } - } -} - -impl From<Flag> for bool { - fn from(val: Flag) -> Self { - val == Flag::Active - } -} - -/// Allows `Flag` to be used in boolean contexts via `*flag`. -/// -/// # Examples -/// -/// ``` -/// # use mingling_picker::value::Flag; -/// let flag = Flag::Active; -/// if *flag { -/// println!("flag is set"); -/// } -/// ``` -impl Deref for Flag { - type Target = bool; - - fn deref(&self) -> &bool { - match self { - Flag::Active => &true, - Flag::Inactive => &false, - } - } -} - -impl Not for Flag { - type Output = Flag; - - fn not(self) -> Flag { - match self { - Flag::Active => Flag::Inactive, - Flag::Inactive => Flag::Active, - } - } -} +mod flag; +pub use flag::*; diff --git a/mingling_picker/src/value/flag.rs b/mingling_picker/src/value/flag.rs new file mode 100644 index 0000000..ee0d6ee --- /dev/null +++ b/mingling_picker/src/value/flag.rs @@ -0,0 +1,145 @@ +use std::{ + fmt::{Debug, Display}, + ops::{Deref, Not}, +}; + +/// Parsed result of a boolean-style command-line flag. +/// +/// `Flag` is a **value type** that can be declared in [`PickerArg`]. +/// When the user passes `--verbose` on the command line, the parsed result is `Flag::Active`; +/// when the flag is absent, the result is `Flag::Inactive`. +/// +/// # Why not just `bool`? +/// +/// Unlike a raw `bool`, `Flag` carries **explicit semantics** about whether +/// the flag was actually provided by the user (`Active`) or simply omitted +/// (`Inactive`). This distinction matters when you want to distinguish +/// "the user intentionally omitted the flag" from "the flag was processed but +/// resolved to false" — the `Pickable` implementation for `Flag` always +/// returns `Parsed(Flag::Inactive)` when no matching argument is found, +/// rather than `NotFound`, making it always succeed with a meaningful default. +/// +/// # Conversions +/// +/// `Flag` interoperates seamlessly with `bool`: `Flag::Active` is `true`, +/// `Flag::Inactive` is `false`. The [`Deref`] impl allows using a `Flag` +/// directly in boolean contexts: +/// +/// ``` +/// # use mingling_picker::value::Flag; +/// let flag = Flag::Active; +/// if *flag { /* runs */ } +/// ``` +/// +/// [`PickerArg`]: crate::PickerArg +#[derive(Default, Clone, Copy, PartialEq, Eq)] +pub enum Flag { + /// The flag was **not** present on the command line. + /// + /// This is the default state, equivalent to `false`. + #[default] + Inactive, + + /// The flag **was** present on the command line. + /// + /// Equivalent to `true`. + Active, +} + +impl Debug for Flag { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Inactive => write!(f, "inactive"), + Self::Active => write!(f, "active"), + } + } +} + +impl Display for Flag { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Inactive => write!(f, "inactive"), + Self::Active => write!(f, "active"), + } + } +} + +impl Flag { + /// Converts this `Flag` into a `bool`. + /// + /// Returns `true` if the flag is [`Active`], `false` if [`Inactive`]. + /// + /// # Examples + /// + /// ``` + /// # use mingling_picker::value::Flag; + /// assert!(Flag::Active.bool()); + /// assert!(!Flag::Inactive.bool()); + /// ``` + /// + /// [`Active`]: Flag::Active + /// [`Inactive`]: Flag::Inactive + #[must_use] + #[inline(always)] + pub fn bool(&self) -> bool { + *self == Flag::Active + } +} + +impl PartialEq<bool> for Flag { + fn eq(&self, other: &bool) -> bool { + self.bool() == *other + } +} + +/// Compares `bool` with `Flag` using `==`. +impl PartialEq<Flag> for bool { + fn eq(&self, other: &Flag) -> bool { + *self == other.bool() + } +} + +impl From<bool> for Flag { + fn from(value: bool) -> Self { + if value { Flag::Active } else { Flag::Inactive } + } +} + +impl From<Flag> for bool { + fn from(val: Flag) -> Self { + val == Flag::Active + } +} + +/// Allows `Flag` to be used in boolean contexts via `*flag`. +/// +/// # Examples +/// +/// ``` +/// # use mingling_picker::value::Flag; +/// let flag = Flag::Active; +/// if *flag { +/// println!("flag is set"); +/// } +/// ``` +impl Deref for Flag { + type Target = bool; + + fn deref(&self) -> &bool { + match self { + Flag::Active => &true, + Flag::Inactive => &false, + } + } +} + +impl Not for Flag { + type Output = Flag; + + fn not(self) -> Flag { + match self { + Flag::Active => Flag::Inactive, + Flag::Inactive => Flag::Active, + } + } +} |
