From 3a366931579cbda5fef32f1ae9d2ea09377c60af Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 11 Apr 2026 19:17:31 +0800 Subject: Rename boolean enums and simplify pick_bool function --- mingling/src/parser/picker/bools.rs | 77 ++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/mingling/src/parser/picker/bools.rs b/mingling/src/parser/picker/bools.rs index f0a2622..4015cd9 100644 --- a/mingling/src/parser/picker/bools.rs +++ b/mingling/src/parser/picker/bools.rs @@ -1,113 +1,109 @@ use crate::parser::Pickable; #[derive(Debug, Default)] -pub enum YesOrNo { +pub enum Yes { Yes, #[default] No, } -impl From for YesOrNo { +impl From for Yes { fn from(b: bool) -> Self { - if b { YesOrNo::Yes } else { YesOrNo::No } + if b { Yes::Yes } else { Yes::No } } } -impl From for bool { - fn from(val: YesOrNo) -> Self { +impl From for bool { + fn from(val: Yes) -> Self { match val { - YesOrNo::Yes => true, - YesOrNo::No => false, + Yes::Yes => true, + Yes::No => false, } } } -impl std::ops::Deref for YesOrNo { +impl std::ops::Deref for Yes { type Target = bool; fn deref(&self) -> &Self::Target { static TRUE: bool = true; static FALSE: bool = false; match self { - YesOrNo::Yes => &TRUE, - YesOrNo::No => &FALSE, + Yes::Yes => &TRUE, + Yes::No => &FALSE, } } } -impl YesOrNo { +impl Yes { pub fn is_yes(&self) -> bool { - matches!(self, YesOrNo::Yes) + matches!(self, Yes::Yes) } pub fn is_no(&self) -> bool { - matches!(self, YesOrNo::No) + matches!(self, Yes::No) } } -impl Pickable for YesOrNo { - type Output = YesOrNo; +impl Pickable for Yes { + type Output = Yes; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option { - let value = pick_bool(args, flag, &["y", "yes"], &["n", "no"]); + let value = pick_bool(args, flag, &["y", "yes"]); Some(value.into()) } } #[derive(Debug, Default)] -pub enum TrueOrFalse { +pub enum True { True, #[default] False, } -impl From for TrueOrFalse { +impl From for True { fn from(b: bool) -> Self { - if b { - TrueOrFalse::True - } else { - TrueOrFalse::False - } + if b { True::True } else { True::False } } } -impl From for bool { - fn from(val: TrueOrFalse) -> Self { +impl From for bool { + fn from(val: True) -> Self { match val { - TrueOrFalse::True => true, - TrueOrFalse::False => false, + True::True => true, + True::False => false, } } } -impl std::ops::Deref for TrueOrFalse { +impl std::ops::Deref for True { type Target = bool; fn deref(&self) -> &Self::Target { static TRUE: bool = true; static FALSE: bool = false; match self { - TrueOrFalse::True => &TRUE, - TrueOrFalse::False => &FALSE, + True::True => &TRUE, + True::False => &FALSE, } } } -impl TrueOrFalse { +impl True { pub fn is_true(&self) -> bool { - matches!(self, TrueOrFalse::True) + matches!(self, True::True) } pub fn is_false(&self) -> bool { - matches!(self, TrueOrFalse::False) + matches!(self, True::False) } } -impl Pickable for TrueOrFalse { - type Output = TrueOrFalse; +impl Pickable for True { + type Output = True; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option { - let value = pick_bool(args, flag, &["true", "t"], &["false", "f"]); + let value = pick_bool(args, flag, &["true", "t"]); Some(value.into()) } } @@ -116,7 +112,6 @@ fn pick_bool( args: &mut crate::parser::Argument, flag: mingling_core::Flag, positive: &[&str], - negative: &[&str], ) -> bool { let has_flag = args.pick_flag(flag.clone()); if !has_flag { @@ -124,13 +119,7 @@ fn pick_bool( match content { Some(content) => { let s = content.as_str(); - if positive.contains(&s) { - true - } else if negative.contains(&s) { - false - } else { - false - } + positive.contains(&s) } None => false, } -- cgit