start the refactor

This commit is contained in:
2025-05-10 21:49:23 +02:00
parent ae79359990
commit baf7ba2ffb
10 changed files with 473 additions and 321 deletions

View File

@@ -1,36 +1,45 @@
use std::fs;
#[cfg(test)]
use mockall::{automock, mock, predicate::*};
use once_cell::sync::Lazy;
use std::collections::LinkedList;
use std::fs;
use std::sync::Mutex;
use std::sync::RwLock;
use once_cell::sync::Lazy;
#[cfg(not(debug_assertions))]
const ALLOW_PATH : &str ="/opt/mini_admin_bot/allow_users";
const ALLOW_PATH: &str = "/opt/mini_admin_bot/allow_users";
#[cfg(not(debug_assertions))]
const POLE_PATH : &str ="/opt/mini_admin_bot/allow_pole";
const POLE_PATH: &str = "/opt/mini_admin_bot/allow_pole";
#[cfg(debug_assertions)]
const ALLOW_PATH : &str ="allow_users";
const ALLOW_PATH: &str = "allow_users";
#[cfg(debug_assertions)]
const POLE_PATH : &str ="allow_pole";
const POLE_PATH: &str = "allow_pole";
static mut LIST_IDS: Lazy<Mutex<LinkedList<String>>> = Lazy::new(|| Mutex::new(read_ids(ALLOW_PATH)));
static mut LIST_IDS: Lazy<Mutex<LinkedList<String>>> =
Lazy::new(|| Mutex::new(read_ids(ALLOW_PATH)));
static mut LIST_POLE_IDS: Lazy<Mutex<LinkedList<String>>> = Lazy::new(|| Mutex::new(read_ids(POLE_PATH)));
static mut LIST_POLE_IDS: Lazy<Mutex<LinkedList<String>>> =
Lazy::new(|| Mutex::new(read_ids(POLE_PATH)));
fn read_ids<'a>(path: &str)-> LinkedList<String> {
fn read_ids<'a>(path: &str) -> LinkedList<String> {
let content = fs::read_to_string(path).expect("Something went wrong reading the file");
content.split('\n').into_iter().map(|item| format!("{}", item)).collect::<LinkedList<String>>()
content
.split('\n')
.into_iter()
.map(|item| format!("{}", item))
.collect::<LinkedList<String>>()
}
pub struct group_permissions {
pub struct GroupPermissions {
aproved_groups: RwLock<LinkedList<String>>,
party_groups: RwLock<LinkedList<String>>,
}
impl group_permissions {
#[cfg_attr(test, automock)]
impl GroupPermissions {
pub fn new() -> Self {
Self{
Self {
aproved_groups: RwLock::new(read_ids(ALLOW_PATH)),
party_groups: RwLock::new(read_ids(POLE_PATH)),
}
@@ -45,22 +54,20 @@ impl group_permissions {
}
}
pub fn compare(id: &String) -> bool{
pub fn compare(id: &String) -> bool {
let ret: bool;
unsafe {
ret=LIST_IDS.lock().unwrap().contains(id);
log::info!("{}",id);
ret = LIST_IDS.lock().unwrap().contains(id);
log::info!("{}", id);
}
ret
}
pub fn compare_pole(id: &String) -> bool{
pub fn compare_pole(id: &String) -> bool {
let ret: bool;
unsafe {
ret=LIST_POLE_IDS.lock().unwrap().contains(id);
log::info!("{}",id);
ret = LIST_POLE_IDS.lock().unwrap().contains(id);
log::info!("{}", id);
}
ret
}