[ownership] parse leading exclamation mark as negation of ownership component
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 22a5536 · Browse files · 1 file·+36 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+36 added−37 removed
- other:+36 added−37 removed
Provenance
- Commit
09cdf98- Tree
29785ef- Author
Alexandru Macovei <alexnmaco@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
22a5536
1 file changed+36 added−37 removed
modified:src/filter/owner.rs+36 added-37 removed
17 unmodified lines18 pub fn from_string(input: &str) -> Result<Self> {19 let mut it = input.split(':');20 let (fst, snd) = (it.next(), it.next());21 22- let uid = match fst {23- Some("") | None => None,24- Some(s) => {25- let maybe_uid = s26- .parse()27- .ok()28- .or_else(|| users::get_user_by_name(s).map(|user| user.uid()));29- match maybe_uid {30- Some(uid) => Some(uid),31- _ => return Err(anyhow!("'{}' is not a recognized user name", s)),32- }33- }34- };35- let gid = match snd {36- Some("") | None => None,37- Some(s) => {38- let maybe_gid = s39- .parse()40- .ok()41- .or_else(|| users::get_group_by_name(s).map(|group| group.gid()));42- match maybe_gid {43- Some(gid) => Some(gid),44- _ => return Err(anyhow!("'{}' is not a recognized group name", s)),45- }46- }47- };22+ let uid = Check::parse(fst, |s| {23+ s.parse()24+ .ok()25+ .or_else(|| users::get_user_by_name(s).map(|user| user.uid()))26+ .ok_or_else(|| anyhow!("'{}' is not a recognized user name", s))27+ })?;28+ let gid = Check::parse(snd, |s| {29+ s.parse()30+ .ok()31+ .or_else(|| users::get_group_by_name(s).map(|group| group.gid()))32+ .ok_or_else(|| anyhow!("'{}' is not a recognized group name", s))33+ })?;34 49- use self::Check::*;50- let uid = match uid {51- Some(u) => Equal(u),52- _ => Ignore,53- };54- let gid = match gid {55- Some(g) => Equal(g),56- _ => Ignore,57- };58-59- if let (Ignore, Ignore) = (uid, gid) {35+ if let (Check::Ignore, Check::Ignore) = (uid, gid) {36 Err(anyhow!(37 "'{}' is not a valid user/group specifier. See 'fd --help'.",38 input39 ))16 unmodified lines56 Check::NotEq(x) => v != *x,57 Check::Ignore => true,58 }59 }60+61+ fn parse<F>(s: Option<&str>, f: F) -> Result<Self>62+ where63+ F: Fn(&str) -> Result<T>,64+ {65+ let (s, equality) = match s {66+ Some("") | None => return Ok(Check::Ignore),67+ Some(s) if s.starts_with('!') => (&s[1..], false),68+ Some(s) => (s, true),69+ };70+71+ f(s).map(|x| {72+ if equality {73+ Check::Equal(x)74+ } else {75+ Check::NotEq(x)76+ }77+ })78+ }79 }80 81 #[cfg(test)]82 mod owner_parsing {21 unmodified lines104 uid_gid: "9:3" => Ok(OwnerFilter { uid: Equal(9), gid: Equal(3) }),105 gid_only: ":8" => Ok(OwnerFilter { uid: Ignore, gid: Equal(8) }),106 colon_only: ":" => Err(_),107 trailing: "5:" => Ok(OwnerFilter { uid: Equal(5), gid: Ignore }),108+109+ uid_negate: "!5" => Ok(OwnerFilter { uid: NotEq(5), gid: Ignore }),110+ both_negate:"!4:!3" => Ok(OwnerFilter { uid: NotEq(4), gid: NotEq(3) }),111+ uid_not_gid:"6:!8" => Ok(OwnerFilter { uid: Equal(6), gid: NotEq(8) }),112 }113 }