From 6c0b7cb787082ada71385563ea3a3ffc190efcd5 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 23 Jan 2023 09:10:08 -0500 Subject: [PATCH] Use `Bitflags` trait in `mode::Parser` --- src/mode/parser.rs | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/mode/parser.rs b/src/mode/parser.rs index 6177563..31766d0 100644 --- a/src/mode/parser.rs +++ b/src/mode/parser.rs @@ -1,3 +1,5 @@ +use crate::bitflags::BitFlags; + use super::{get_umask, Bit}; use std::{ error, @@ -65,6 +67,14 @@ impl BitAnd for u32 { } } +impl BitAnd for Who { + type Output = u32; + + fn bitand(self, rhs: u32) -> Self::Output { + self as u32 & rhs + } +} + impl BitAndAssign for u32 { fn bitand_assign(&mut self, rhs: Who) { *self = *self & rhs; @@ -157,13 +167,13 @@ impl Parser { if self.op.is_none() { Err(ParseError::NoOpSet) } else { - if self.who & 0o100 != 0 { + if self.who.contains(Who::User) { self.bits |= Bit::URead; } - if self.who & 0o10 != 0 { + if self.who.contains(Who::Group) { self.bits |= Bit::GRead; } - if self.who & 0o1 != 0 { + if self.who.contains(Who::Other) { self.bits |= Bit::ORead; } Ok(()) @@ -174,13 +184,13 @@ impl Parser { if self.op.is_none() { Err(ParseError::NoOpSet) } else { - if self.who & 0o100 != 0 { + if self.who.contains(Who::User) { self.bits |= Bit::UWrite; } - if self.who & 0o10 != 0 { + if self.who.contains(Who::Group) { self.bits |= Bit::GWrite; } - if self.who & 0o1 != 0 { + if self.who.contains(Who::Other) { self.bits |= Bit::OWrite; } Ok(()) @@ -191,13 +201,13 @@ impl Parser { if self.op.is_none() { Err(ParseError::NoOpSet) } else { - if self.who & 0o100 != 0 { + if self.who.contains(Who::User) { self.bits |= Bit::UExec; } - if self.who & 0o10 != 0 { + if self.who.contains(Who::Group) { self.bits |= Bit::GExec; } - if self.who & 0o1 != 0 { + if self.who.contains(Who::Other) { self.bits |= Bit::OExec; } Ok(()) @@ -205,27 +215,27 @@ impl Parser { } fn push_suid_sgid(&mut self) -> Result<(), ParseError> { - if self.who == 0 || self.who & 0o1 != 0 { + if self.who == 0 || self.who.contains(Who::Other) { return Err(ParseError::InvalidBit); } else if self.op.is_none() { return Err(ParseError::NoOpSet); } - if self.who & 0o100 != 0 { + if self.who.contains(Who::User) { self.bits |= Bit::Suid; } - if self.who & 0o10 != 0 { + if self.who.contains(Who::Group) { self.bits |= Bit::Sgid; } Ok(()) } fn push_sticky(&mut self) -> Result<(), ParseError> { - if self.who == 0 || self.who & 0o100 != 0 || self.who & 0o10 != 0 { + if self.who == 0 || self.who.contains(Who::User) || self.who.contains(Who::Group) { return Err(ParseError::InvalidBit); } else if self.op.is_none() { return Err(ParseError::NoOpSet); } - if self.who & 0o1 != 0 { + if self.who.contains(Who::Other) { self.bits |= Bit::Sticky; } Ok(()) @@ -244,13 +254,13 @@ impl Parser { Some(Op::Add) => self.add_bits(), Some(Op::Remove) => self.remove_bits(), Some(Op::Equals) => { - if self.who & 0o100 != 0 { + if self.who.contains(Who::User) { self.mode &= !(0o4700); } - if self.who & 0o10 != 0 { + if self.who.contains(Who::Group) { self.mode &= !(0o2070); } - if self.who & 0o1 != 0 { + if self.who.contains(Who::Other) { self.mode &= !(0o1007); } self.add_bits();