diff options
Diffstat (limited to 'mingling/src/parser/picker')
| -rw-r--r-- | mingling/src/parser/picker/bools.rs | 42 | ||||
| -rw-r--r-- | mingling/src/parser/picker/builtin.rs | 21 | ||||
| -rw-r--r-- | mingling/src/parser/picker/path.rs | 15 | ||||
| -rw-r--r-- | mingling/src/parser/picker/path/rule.rs | 24 |
4 files changed, 49 insertions, 53 deletions
diff --git a/mingling/src/parser/picker/bools.rs b/mingling/src/parser/picker/bools.rs index 0525c52..6f866ab 100644 --- a/mingling/src/parser/picker/bools.rs +++ b/mingling/src/parser/picker/bools.rs @@ -16,7 +16,7 @@ pub enum Yes { impl From<bool> for Yes { fn from(b: bool) -> Self { - if b { Yes::Yes } else { Yes::No } + if b { Self::Yes } else { Self::No } } } @@ -36,26 +36,26 @@ impl std::ops::Deref for Yes { static TRUE: bool = true; static FALSE: bool = false; match self { - Yes::Yes => &TRUE, - Yes::No => &FALSE, + Self::Yes => &TRUE, + Self::No => &FALSE, } } } impl Yes { #[must_use] - pub fn is_yes(&self) -> bool { - matches!(self, Yes::Yes) + pub const fn is_yes(&self) -> bool { + matches!(self, Self::Yes) } #[must_use] - pub fn is_no(&self) -> bool { - matches!(self, Yes::No) + pub const fn is_no(&self) -> bool { + matches!(self, Self::No) } } impl Pickable for Yes { - type Output = Yes; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let value = pick_bool(args, flag, &["y", "yes"]); @@ -79,7 +79,7 @@ pub enum True { impl From<bool> for True { fn from(b: bool) -> Self { - if b { True::True } else { True::False } + if b { Self::True } else { Self::False } } } @@ -99,26 +99,26 @@ impl std::ops::Deref for True { static TRUE: bool = true; static FALSE: bool = false; match self { - True::True => &TRUE, - True::False => &FALSE, + Self::True => &TRUE, + Self::False => &FALSE, } } } impl True { #[must_use] - pub fn is_true(&self) -> bool { - matches!(self, True::True) + pub const fn is_true(&self) -> bool { + matches!(self, Self::True) } #[must_use] - pub fn is_false(&self) -> bool { - matches!(self, True::False) + pub const fn is_false(&self) -> bool { + matches!(self, Self::False) } } impl Pickable for True { - type Output = True; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let value = pick_bool(args, flag, &["true", "t"]); @@ -132,11 +132,11 @@ fn pick_bool( positive: &[&str], ) -> bool { let content = args.pick_argument(flag); - match content { - Some(content) => { + content.map_or_else( + || false, + |content| { let s = content.as_str(); positive.contains(&s) - } - None => false, - } + }, + ) } diff --git a/mingling/src/parser/picker/builtin.rs b/mingling/src/parser/picker/builtin.rs index 6194955..2a5d569 100644 --- a/mingling/src/parser/picker/builtin.rs +++ b/mingling/src/parser/picker/builtin.rs @@ -3,7 +3,7 @@ use size::Size; use crate::parser::{Argument, Pickable}; impl Pickable for String { - type Output = String; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { args.pick_argument(flag) @@ -11,7 +11,7 @@ impl Pickable for String { } impl Pickable for Vec<String> { - type Output = Vec<String>; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { Some(args.pick_arguments(flag)) @@ -53,7 +53,7 @@ macro_rules! impl_pickable_for_number { impl_pickable_for_number!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64); impl Pickable for bool { - type Output = bool; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { Some(args.pick_flag(flag)) @@ -62,25 +62,22 @@ impl Pickable for bool { /// Special: parses a size string (e.g. "10MB") into a `usize` representing the number of bytes. impl Pickable for usize { - type Output = usize; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let picked = args.pick_argument(flag)?; let size_parse = Size::from_str(picked.as_str()); - match size_parse { - Ok(size) => usize::try_from(size.bytes()).ok(), - Err(_) => None, - } + size_parse.map_or(None, |size| Self::try_from(size.bytes()).ok()) } } /// Special: parses a comma-separated list of size strings (e.g. "10MB,20KB") into a `Vec<usize>`. impl Pickable for Vec<usize> { - type Output = Vec<usize>; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let picked_vec = args.pick_arguments(flag); - let mut result = Vec::new(); + let mut result = Self::new(); for picked in picked_vec { let size_parse = Size::from_str(picked.as_str()); match size_parse { @@ -94,7 +91,7 @@ impl Pickable for Vec<usize> { /// Special: dumps the remaining arguments into an `Argument` struct. impl Pickable for Argument { - type Output = Argument; + type Output = Self; fn pick( args: &mut crate::parser::Argument, @@ -106,7 +103,7 @@ impl Pickable for Argument { /// Special: parses a single value of type `T` using the `Pickable` implementation for `T`, and wraps it in an `Option`. impl<T: Pickable<Output = T> + Default> Pickable for Option<T> { - type Output = Option<T>; + type Output = Self; fn pick(args: &mut Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let r = T::pick(args, flag); diff --git a/mingling/src/parser/picker/path.rs b/mingling/src/parser/picker/path.rs index 961542e..4722088 100644 --- a/mingling/src/parser/picker/path.rs +++ b/mingling/src/parser/picker/path.rs @@ -6,22 +6,21 @@ mod rule; pub use rule::*; impl Pickable for Vec<PathBuf> { - type Output = Vec<PathBuf>; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let raw: Vec<String> = args.pick_arguments(flag); - let paths: Vec<PathBuf> = raw.into_iter().map(PathBuf::from).collect(); + let paths = raw.into_iter().map(PathBuf::from).collect(); Some(paths) } } impl Pickable for PathBuf { - type Output = PathBuf; + type Output = Self; fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { let raw: String = args.pick_argument(flag)?; - let path: PathBuf = PathBuf::from(raw); - Some(path) + Some(Self::from(raw)) } } @@ -86,8 +85,8 @@ pub trait PathChecker { } } -impl<T: Into<Vec<PathBuf>>> PathsChecker for T where T: Into<Vec<PathBuf>> {} -impl<T: Into<PathBuf>> PathChecker for T where T: Into<PathBuf> {} +impl<T: Into<Vec<PathBuf>>> PathsChecker for T {} +impl<T: Into<PathBuf>> PathChecker for T {} fn check_paths(path: impl Into<Vec<PathBuf>>, rule: &PathCheckRule) -> Result<(), ()> { let paths = path.into(); @@ -140,6 +139,6 @@ fn check_type(path: &Path, rule: &PathCheckRule) -> Result<(), ()> { Err(()) } -fn bool_to_result(b: bool) -> Result<(), ()> { +const fn bool_to_result(b: bool) -> Result<(), ()> { if b { Ok(()) } else { Err(()) } } diff --git a/mingling/src/parser/picker/path/rule.rs b/mingling/src/parser/picker/path/rule.rs index bf5cab3..52bf65a 100644 --- a/mingling/src/parser/picker/path/rule.rs +++ b/mingling/src/parser/picker/path/rule.rs @@ -26,7 +26,7 @@ pub struct PathTypeCheck { impl PathCheckRule { /// Creates a new `PathCheckRule` with default values #[must_use] - pub fn new() -> Self { + pub const fn new() -> Self { Self { exist_check: None, type_check: None, @@ -35,7 +35,7 @@ impl PathCheckRule { /// Allows the path to be a file #[must_use] - pub fn allow_file(self) -> Self { + pub const fn allow_file(self) -> Self { match self.type_check { Some(type_check) => Self { type_check: Some(PathTypeCheck { @@ -58,7 +58,7 @@ impl PathCheckRule { /// Allows the path to be a directory #[must_use] - pub fn allow_dir(self) -> Self { + pub const fn allow_dir(self) -> Self { match self.type_check { Some(type_check) => Self { type_check: Some(PathTypeCheck { @@ -81,7 +81,7 @@ impl PathCheckRule { /// Allows the path to be a symlink #[must_use] - pub fn allow_symlink(self) -> Self { + pub const fn allow_symlink(self) -> Self { match self.type_check { Some(type_check) => Self { type_check: Some(PathTypeCheck { @@ -104,7 +104,7 @@ impl PathCheckRule { /// Denies the path from being a file #[must_use] - pub fn deny_file(self) -> Self { + pub const fn deny_file(self) -> Self { match self.type_check { Some(type_check) => Self { type_check: Some(PathTypeCheck { @@ -127,7 +127,7 @@ impl PathCheckRule { /// Denies the path from being a directory #[must_use] - pub fn deny_dir(self) -> Self { + pub const fn deny_dir(self) -> Self { match self.type_check { Some(type_check) => Self { type_check: Some(PathTypeCheck { @@ -150,7 +150,7 @@ impl PathCheckRule { /// Denies the path from being a symlink #[must_use] - pub fn deny_symlink(self) -> Self { + pub const fn deny_symlink(self) -> Self { match self.type_check { Some(type_check) => Self { type_check: Some(PathTypeCheck { @@ -173,7 +173,7 @@ impl PathCheckRule { /// Requires the path to be a file (overrides type checks) #[must_use] - pub fn must_file(self) -> Self { + pub const fn must_file(self) -> Self { Self { type_check: Some(PathTypeCheck { allow_file: true, @@ -186,7 +186,7 @@ impl PathCheckRule { /// Requires the path to be a directory (overrides type checks) #[must_use] - pub fn must_dir(self) -> Self { + pub const fn must_dir(self) -> Self { Self { type_check: Some(PathTypeCheck { allow_file: false, @@ -199,7 +199,7 @@ impl PathCheckRule { /// Requires the path to be a symlink (overrides type checks) #[must_use] - pub fn must_symlink(self) -> Self { + pub const fn must_symlink(self) -> Self { Self { type_check: Some(PathTypeCheck { allow_file: false, @@ -212,7 +212,7 @@ impl PathCheckRule { /// Requires the path to exist #[must_use] - pub fn must_exist(self) -> Self { + pub const fn must_exist(self) -> Self { Self { exist_check: Some(PathExistCheck::Exists), ..self @@ -221,7 +221,7 @@ impl PathCheckRule { /// Requires the path to not exist #[must_use] - pub fn must_not_exist(self) -> Self { + pub const fn must_not_exist(self) -> Self { Self { exist_check: Some(PathExistCheck::NotExists), ..self |
