diff --git a/noxis-rs/src/utils/files.rs b/noxis-rs/src/utils/files.rs index 435813c..d286c8f 100644 --- a/noxis-rs/src/utils/files.rs +++ b/noxis-rs/src/utils/files.rs @@ -12,46 +12,43 @@ pub mod v2 { use log::{error, info, warn}; - - // use std::collections::HashMap; use crate::options::structs::{FileTriggerType, FileTriggersForController as Triggers, ProcessUnit}; use super::*; use std::{collections::HashMap, path::Path}; - type MpscSender<'a> = Arc>>; - // type EventHandlers<'a> = HashMap - type EventHandlers<'a> = HashMap<&'a str, (Triggers<'a>, MpscSender<'a>)>; + type MpscSender = Arc>; + type EventHandlers = HashMap, (Triggers, MpscSender)>; #[derive(Debug)] - pub struct FilesController<'a> { - name : &'a str, + pub struct FilesController { + name : Arc, path : String, + code_name : Arc, watcher : Option, - // obj: Arc, - triggers : EventHandlers<'a>, - code_name : String, + triggers : EventHandlers, } - impl<'a> PartialEq for FilesController<'a> { + impl PartialEq for FilesController { fn eq(&self, other: &Self) -> bool { - self.path == other.path && self.name == other.name + self.code_name == other.code_name } } - impl<'a> FilesController<'a> { - pub fn new(name: &'a str, triggers: EventHandlers<'a>) -> FilesController<'a> { + impl FilesController { + pub fn new(name: &str, triggers: EventHandlers) -> FilesController { + let name: Arc = Arc::from(name); Self { - name, + name: name.clone(), path : String::new(), watcher: None, triggers, - code_name : name.to_string(), + code_name : name.clone(), } } - pub fn with_path(mut self, path: impl AsRef) -> anyhow::Result> { + pub fn with_path(mut self, path: impl AsRef) -> anyhow::Result { self.path = path.as_ref().to_string_lossy().into_owned(); self.watcher = { - match create_watcher(self.name, &self.path) { + match create_watcher(&self.name, &self.path) { Ok(val) => Some(val), Err(er) => { error!("Cannot create watcher for {} ({}) due to {}", self.name, &self.path, er); @@ -59,25 +56,25 @@ } } }; - self.code_name = format!("{}{}", &self.path, &self.code_name); + self.code_name = Arc::from(format!("{}{}", &self.path, &self.code_name)); Ok(self) } - pub fn add_event(&mut self, file_controller : FilesController<'a>) { + pub fn add_event(&mut self, file_controller : FilesController) { for (k, v) in file_controller.triggers { self.triggers.entry(k).or_insert(v); } } - async fn trigger_on(&'a mut self, trigger_type: Option) { + async fn trigger_on(&mut self, trigger_type: Option) { let _ = self.triggers.iter() .map(|(prc_name, (triggers, channel))| async { let _ = channel.send({ match &trigger_type { None => { - Events::Positive(&self.code_name) + Events::Positive(self.code_name.clone()) }, Some(event) => { info!("Event on file {} ({}) : {}. Notifying `{}` ...", self.name, &self.path, event, *prc_name); - event.event_from_file_trigger_controller(self.name, triggers) + event.event_from_file_trigger_controller(self.code_name.clone(), triggers) }, } }).await; @@ -85,11 +82,11 @@ } } #[async_trait] - impl<'a> ProcessUnit<'a> for FilesController<'a> { - async fn process(&'a mut self) { + impl ProcessUnit for FilesController { + async fn process(&mut self) { // polling file check // 1) existing check - if let Ok(_) = check_file(self.name, &self.path).await { + if let Ok(_) = check_file(&self.name, &self.path).await { match &mut self.watcher { Some(notify) => { let mut buffer = [0; 1024]; @@ -100,7 +97,7 @@ ) { warn!("File {} ({}) was changed", self.name, &self.path); if recreate_watcher { - self.watcher = match create_watcher(self.name, &self.path) { + self.watcher = match create_watcher(&self.name, &self.path) { Ok(notifier) => Some(notifier), Err(er) => { error!("Failed to recreate watcher for {} ({}) due to {}",