Skip to content
Myr

[ownership] initial struct, parsing, and test framework

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alexandru Macovei · committed by David Peter · parent daf3046 · Browse files · 2 files+67 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+67 added−0 removed

  • other:+67 added−0 removed

2 files+67 added−0 removed

modified:src/filter/mod.rs+6 added
1pub use self::size::SizeFilter;2pub use self::time::TimeFilter;34#[cfg(unix)]5pub use self::owner::OwnerFilter;67mod size;8mod time;910#[cfg(unix)]11mod owner;
added:src/filter/owner.rs+61 added
1use anyhow::{anyhow, Result};23#[derive(Clone, Copy, Debug, PartialEq)]4pub struct OwnerFilter {5 uid: Option<u32>,6 gid: Option<u32>,7}89impl OwnerFilter {10 pub fn from_string(input: &str) -> Result<Self> {11 let mut it = input.split(':');12 let (fst, snd) = (it.next(), it.next());1314 let uid = match fst {15 Some("") | None => None,16 Some(s) => Some(s.parse()?),17 };18 let gid = match snd {19 Some("") | None => None,20 Some(s) => Some(s.parse()?),21 };2223 if uid.is_none() && gid.is_none() {24 Err(anyhow!(25 "'{}' is not a valid user/group specifier. See 'fd --help'.",26 input27 ))28 } else {29 Ok(OwnerFilter { uid, gid })30 }31 }32}3334#[cfg(test)]35mod owner_parsing {36 use super::OwnerFilter;3738 macro_rules! owner_tests {39 ($($name:ident: $value:expr => $result:pat,)*) => {40 $(41 #[test]42 fn $name() {43 let o = OwnerFilter::from_string($value);44 match o {45 $result => {},46 _ => panic!("{:?} does not match {}", o, stringify!($result)),47 }48 }49 )*50 };51 }5253 owner_tests! {54 empty: "" => Err(_),55 uid_only: "5" => Ok(OwnerFilter { uid: Some(5), gid: None }),56 uid_gid: "9:3" => Ok(OwnerFilter { uid: Some(9), gid: Some(3)}),57 gid_only: ":8" => Ok(OwnerFilter { uid: None, gid: Some(8)}),58 colon_only: ":" => Err(_),59 trailing: "5:" => Ok(OwnerFilter { uid: Some(5), gid: None }),60 }61}