Skip to content

Commit

Permalink
Replace user and group identifier type alias by structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Oct 8, 2024
1 parent d19bd61 commit bf17f76
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 31 deletions.
16 changes: 9 additions & 7 deletions Modules/Task/src/Manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
sync::{OnceLock, RwLock},
time::Duration,
};
use Users::{Root_user_identifier, User_identifier_type};
use Users::User_identifier_type;

/// Internal representation of a task.
struct Task_internal_type {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Manager_type {
let Task_internal = Task_internal_type {
Main_thread: Thread_wrapper_type::Get_current(),
Parent: Task_identifier,
Owner: Root_user_identifier,
Owner: User_identifier_type::Root,
Environment_variables: vec![],
};

Expand Down Expand Up @@ -102,7 +102,7 @@ impl Manager_type {
let Task_internal = Task_internal_type {
Main_thread: Thread_wrapper_type::Get_current(),
Parent: Self::Root_task_identifier,
Owner: Root_user_identifier,
Owner: User_identifier_type::Root,
Environment_variables: vec![],
};

Expand Down Expand Up @@ -558,7 +558,7 @@ mod Tests {
}

fn Test_get_owner(Manager: &Manager_type) {
let User_identifier = 123; // Assuming User_identifier_type is i32 for example
let User_identifier = User_identifier_type::New(123); // Assuming User_identifier_type is i32 for example

let Task = Manager.Get_current_task_identifier().unwrap();

Expand Down Expand Up @@ -590,7 +590,7 @@ mod Tests {
}

fn Test_task_owner_inheritance(Manager: &Manager_type) {
let User_identifier = 123; // Assuming User_identifier_type is i32 for example
let User_identifier = User_identifier_type::New(123); // Assuming User_identifier_type is i32 for example
let Task = Manager.Get_current_task_identifier().unwrap();

let _ = Manager
Expand All @@ -611,12 +611,14 @@ mod Tests {
})
.unwrap();

let User_identifier = User_identifier_type::New(6969); // Assuming User_identifier_type is i32 for example

// - Overwrite owner
let _ = Get_instance()
.New_task(Task, Some(6969), "Task 3", None, move || {
.New_task(Task, Some(User_identifier), "Task 3", None, move || {
let Task = Get_instance().Get_current_task_identifier().unwrap();

assert_eq!(Get_instance().Get_owner(Task).unwrap(), 6969);
assert_eq!(Get_instance().Get_owner(Task).unwrap(), User_identifier);
assert_eq!(Get_instance().Get_task_name(Task).unwrap(), "Task 3");
})
.unwrap();
Expand Down
89 changes: 89 additions & 0 deletions Modules/Users/src/Identifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::ops::{Add, AddAssign};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct User_identifier_type(u16);

impl User_identifier_type {
pub const Root: Self = Self::New(0);

pub const Minimum: Self = Self::New(1);
pub const Maximum: Self = Self::New(u16::MAX);

pub const fn New(Identifier: u16) -> Self {
Self(Identifier)
}

pub const fn Into_inner(self) -> u16 {
self.0
}
}

impl AddAssign<u16> for User_identifier_type {
fn add_assign(&mut self, Other: u16) {
self.0 += Other;
}
}

impl Add<u16> for User_identifier_type {
type Output = Self;

fn add(self, Other: u16) -> Self {
Self::New(self.0 + Other)
}
}

impl From<u16> for User_identifier_type {
fn from(Value: u16) -> Self {
User_identifier_type::New(Value)
}
}
impl From<User_identifier_type> for u16 {
fn from(Value: User_identifier_type) -> Self {
Value.Into_inner()
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct Group_identifier_type(u16);

impl Group_identifier_type {
pub const Root: Self = Self::New(0);

pub const Minimum: Self = Self::New(1);
pub const Maximum: Self = Self::New(u16::MAX);

pub const fn New(Identifier: u16) -> Self {
Self(Identifier)
}

pub const fn Into_inner(self) -> u16 {
self.0
}
}

impl From<u16> for Group_identifier_type {
fn from(Value: u16) -> Self {
Group_identifier_type::New(Value)
}
}
impl From<Group_identifier_type> for u16 {
fn from(Value: Group_identifier_type) -> Self {
Value.Into_inner()
}
}

impl AddAssign<u16> for Group_identifier_type {
fn add_assign(&mut self, Other: u16) {
self.0 += Other;
}
}

impl Add<u16> for Group_identifier_type {
type Output = Self;

fn add(self, Other: u16) -> Self {
Self::New(self.0 + Other)
}
}
50 changes: 33 additions & 17 deletions Modules/Users/src/Manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Manager_type {
fn New() -> Self {
let mut Groups = BTreeMap::new();
Groups.insert(
Root_group_identifier,
Group_identifier_type::Root,
Internal_group_type {
Name: "root".to_string(),
Users: BTreeSet::new(),
Expand All @@ -61,10 +61,10 @@ impl Manager_type {

let mut Users = BTreeMap::new();
Users.insert(
crate::Root_user_identifier,
User_identifier_type::Root,
Internal_user_type {
Name: "root".to_string(),
Primary_group: Root_group_identifier,
Primary_group: Group_identifier_type::Root,
},
);

Expand All @@ -74,17 +74,33 @@ impl Manager_type {
fn Get_new_group_identifier<T>(
Groups: &BTreeMap<Group_identifier_type, T>,
) -> Result_type<Group_identifier_type> {
(0..Group_identifier_type::MAX)
.find(|Identifier| !Groups.contains_key(Identifier))
.ok_or(Error_type::Too_many_groups)
let mut Identifier = Group_identifier_type::Minimum;

while Groups.contains_key(&Identifier) {
Identifier += 1;

if Identifier == Group_identifier_type::Maximum {
return Err(Error_type::Too_many_groups);
}
}

Ok(Identifier)
}

fn Get_new_user_identifier(
Users: &BTreeMap<User_identifier_type, Internal_user_type>,
) -> Result_type<User_identifier_type> {
(0..User_identifier_type::MAX)
.find(|Identifier| !Users.contains_key(Identifier))
.ok_or(Error_type::Too_many_users)
let mut Identifier = User_identifier_type::Minimum;

while Users.contains_key(&Identifier) {
Identifier += 1;

if Identifier == User_identifier_type::Maximum {
return Err(Error_type::Too_many_users);
}
}

Ok(Identifier)
}

pub fn Create_user(
Expand Down Expand Up @@ -148,7 +164,7 @@ impl Manager_type {
}

pub fn Is_root(Identifier: User_identifier_type) -> bool {
crate::Root_user_identifier == Identifier
User_identifier_type::Root == Identifier
}

pub fn Is_in_group(
Expand Down Expand Up @@ -270,7 +286,7 @@ mod Tests {
fn Create_user() {
let Manager = Manager_type::New();
let User_name = "Alice";
let Result = Manager.Create_user(User_name, Root_group_identifier);
let Result = Manager.Create_user(User_name, Group_identifier_type::Root);
assert!(Result.is_ok());
let User_id = Result.unwrap();
assert!(Manager.Exists_user(User_id).unwrap());
Expand All @@ -288,7 +304,7 @@ mod Tests {

#[test]
fn Is_root() {
let Root_id = crate::Root_user_identifier;
let Root_id = User_identifier_type::Root;
assert!(Manager_type::Is_root(Root_id));
}

Expand All @@ -297,7 +313,7 @@ mod Tests {
let Manager = Manager_type::New();
let User_name = "Bob";
let User_id = Manager
.Create_user(User_name, Root_group_identifier)
.Create_user(User_name, Group_identifier_type::Root)
.unwrap();
let Group_name = "Admins";
let Group_id = Manager.Create_group(Group_name, None).unwrap();
Expand All @@ -311,7 +327,7 @@ mod Tests {

let User_name = "Charlie";
let User_id = Manager
.Create_user(User_name, Root_group_identifier)
.Create_user(User_name, Group_identifier_type::Root)
.unwrap();
let Group_name1 = "TeamA";
let Group_id1 = Manager.Create_group(Group_name1, None).unwrap();
Expand All @@ -325,7 +341,7 @@ mod Tests {
assert!(
Groups.contains(&Group_id1)
&& Groups.contains(&Group_id2)
&& Groups.contains(&Root_group_identifier)
&& Groups.contains(&Group_identifier_type::Root)
);
}

Expand All @@ -343,7 +359,7 @@ mod Tests {
let Manager = Manager_type::New();
let User_name = "Dave";
let User_id = Manager
.Create_user(User_name, Root_group_identifier)
.Create_user(User_name, Group_identifier_type::Root)
.unwrap();
let Group_name = "Engineers";
let Group_id = Manager.Create_group(Group_name, None).unwrap();
Expand All @@ -358,7 +374,7 @@ mod Tests {
let Manager = Manager_type::New();
let User_name = "Eve";
let User_id = Manager
.Create_user(User_name, Root_group_identifier)
.Create_user(User_name, Group_identifier_type::Root)
.unwrap();
let Retrieved_name = Manager.Get_user_name(User_id).unwrap();
assert_eq!(User_name, Retrieved_name);
Expand Down
11 changes: 4 additions & 7 deletions Modules/Users/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

pub type User_identifier_type = u16;
pub type Group_identifier_type = u16;
pub const Root_user_identifier: User_identifier_type = 0;
pub const Root_group_identifier: Group_identifier_type = 0;

mod Error;
mod Identifiers;
mod Manager;
pub use Manager::*;

mod Error;
pub use Error::*;
pub use Identifiers::*;
pub use Manager::*;

0 comments on commit bf17f76

Please sign in to comment.