diff --git a/Modules/File_system/src/Generics/Fundamentals/Flags.rs b/Modules/File_system/src/Generics/Fundamentals/Flags.rs new file mode 100644 index 0000000..1019b9e --- /dev/null +++ b/Modules/File_system/src/Generics/Fundamentals/Flags.rs @@ -0,0 +1,113 @@ +use super::Permission_type; + +#[inline] +fn Set_bit(Data: &mut u8, Position: u8, Value: bool) { + if Value { + *Data |= 1 << Position; + } else { + *Data &= !(1 << Position); + } +} + +#[inline] +fn Get_bit(Data: &u8, Bit: u8) -> bool { + (Data & (1 << Bit)) != 0 +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[repr(transparent)] +pub struct Mode_type(u8); + +impl Mode_type { + const Read_bit: u8 = 0; + const Write_bit: u8 = 1; + + pub fn Read_only() -> Self { + Self(1 << Self::Read_bit) + } + + pub fn Write_only() -> Self { + Self(1 << Self::Write_bit) + } + + pub fn Read_write() -> Self { + Self((1 << Self::Read_bit) | (1 << Self::Write_bit)) + } + + pub fn Get_read(&self) -> bool { + Get_bit(&self.0, Self::Read_bit) + } + + pub fn Get_write(&self) -> bool { + Get_bit(&self.0, Self::Write_bit) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[repr(transparent)] +pub struct Status_type(u8); + +impl Default for Status_type { + fn default() -> Self { + Self(0).Set_append(false).Set_non_blocking(false) + } +} + +impl Status_type { + const Append_bit: u8 = 0; + const Non_blocking_bit: u8 = 1; + + pub fn Set_non_blocking(mut self, Value: bool) -> Self { + Set_bit(&mut self.0, Self::Non_blocking_bit, Value); + self + } + + pub fn Get_non_blocking(&self) -> bool { + Get_bit(&self.0, Self::Non_blocking_bit) + } + + pub fn Set_append(mut self, Value: bool) -> Self { + Set_bit(&mut self.0, Self::Append_bit, Value); + self + } + + pub fn Get_append(&self) -> bool { + Get_bit(&self.0, Self::Append_bit) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[repr(transparent)] +pub struct Flags_type(u8); + +impl Flags_type { + pub fn New(Mode: Mode_type, Status: Option) -> Self { + Self(Mode.0 << 4 | Status.unwrap_or_default().0) + } + + pub fn Get_mode(&self) -> Mode_type { + Mode_type(self.0 >> 4) + } + + pub fn Get_status(&self) -> Status_type { + Status_type(self.0 & 0b00001111) + } + + pub fn Set_status(&mut self, Status: Status_type) { + self.0 = (self.0 & 0b11110000) | Status.0; + } + + pub fn Is_permission_granted(&self, Permission: &Permission_type) -> bool { + let Mode = self.Get_mode(); + + (Permission.Get_read() && Mode.Get_read()) // Read permission + || (Permission.Get_write() && (Mode.Get_write() || self.Get_status().Get_append())) + // Write permission + } +} + +impl From for Flags_type { + fn from(Mode: Mode_type) -> Self { + Self::New(Mode, None) + } +} diff --git a/Modules/File_system/src/Generics/Fundamentals/mod.rs b/Modules/File_system/src/Generics/Fundamentals/mod.rs index 10d829a..62bf1ed 100644 --- a/Modules/File_system/src/Generics/Fundamentals/mod.rs +++ b/Modules/File_system/src/Generics/Fundamentals/mod.rs @@ -1,6 +1,5 @@ -use std::{ops, u64}; - -pub mod Path; +mod Flags; +pub use Flags::*; pub use Path::*; use Shared::Discriminant_trait;