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
Provenance
Commit
30cb4ad
Tree
53ae8bd
Author
Alexandru Macovei <alexnmaco@gmail.com>
Committer
David Peter <sharkdp@users.noreply.github.com>
Parent
daf3046
2 files changed+67 added−0 removed
modified:src/filter/mod.rs+6 added
1pub use self::size::SizeFilter;2pub use self::time::TimeFilter;34+#[cfg(unix)]5+pub use self::owner::OwnerFilter;6+7mod size;8mod time;9+10+#[cfg(unix)]11+mod owner;
added:src/filter/owner.rs+61 added
1+use anyhow::{anyhow, Result};2+3+#[derive(Clone, Copy, Debug, PartialEq)]4+pub struct OwnerFilter {5+ uid: Option<u32>,6+ gid: Option<u32>,7+}8+9+impl OwnerFilter {10+ pub fn from_string(input: &str) -> Result<Self> {11+ let mut it = input.split(':');12+ let (fst, snd) = (it.next(), it.next());13+14+ 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+ };22+23+ 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+}33+34+#[cfg(test)]35+mod owner_parsing {36+ use super::OwnerFilter;37+38+ 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+ }52+53+ 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+}