fix: SymbolicArgs::from_str with group ids

This commit is contained in:
Rowan 2025-03-01 07:00:21 -06:00
parent 28550c78d9
commit 002a981071

View file

@ -216,10 +216,12 @@ impl FromStr for SymbolicArgs {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match Modes::<SymbolicMode>::from_str(s) { match Modes::<SymbolicMode>::from_str(s) {
Ok(perms) => Ok(Self::Mode(perms)), Ok(perms) if perms.0.bits() > 0 => Ok(Self::Mode(perms)),
Err(_) => match Modes::<GroupId>::from_str(s) { Ok(_) | Err(_) => match Modes::<GroupId>::from_str(s) {
Ok(perms) => Ok(Self::Group(perms)), Ok(perms) if perms.0.bits() > 0 => Ok(Self::Group(perms)),
Err(_) => Err(FromStrError::new(format!("{} is not a valid argument", s)).into()), Ok(_) | Err(_) => {
Err(FromStrError::new(format!("{} is not a valid argument", s)).into())
}
}, },
} }
} }
@ -413,7 +415,7 @@ impl Display for Permissions {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::str::FromStr; use std::{fmt::Debug, str::FromStr};
use enumflags2::BitFlags; use enumflags2::BitFlags;
@ -429,6 +431,41 @@ mod tests {
assert_eq!(flags.to_string(), "7777"); assert_eq!(flags.to_string(), "7777");
} }
fn test_chars<T>(values: &[&str])
where
T: FromStr + ToString + Debug,
<T as FromStr>::Err: Debug,
{
let modes: Vec<(usize, T)> = values
.into_iter()
.map(|s| T::from_str(*s))
.map(Result::unwrap)
.enumerate()
.collect();
for (i, mode) in modes {
assert_eq!(values[i], mode.to_string())
}
}
#[test]
fn symbolic_modes() {
test_chars::<SymbolicMode>(&["r", "w", "x", "X", "s", "t"]);
}
#[test]
fn symbolic_groups() {
test_chars::<GroupId>(&["u", "g", "o"]);
}
#[test]
fn symbolic_args() {
assert_eq!(
SymbolicArgs::from_str("u").unwrap(),
SymbolicArgs::Group(Modes(BitFlags::from(GroupId::User)))
);
}
#[test] #[test]
fn symbolic_permissions() { fn symbolic_permissions() {
let sym = Permissions::from_str("ug+w").unwrap(); let sym = Permissions::from_str("ug+w").unwrap();