use actix::Message; use rand::Rng; use std::{ collections::HashMap, ops::{Deref, DerefMut}, }; use crate::definition::Command; #[derive(Clone, Message, Debug)] #[rtype(result = "()")] pub struct Jobs(Vec); impl Jobs { pub fn new(tasks: Vec) -> Self { Self(tasks) } pub fn pop_front(&mut self) -> Option { if self.0.is_empty() { None } else { Some(self.0.remove(0)) } } } impl Deref for Jobs { type Target = Vec; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Jobs { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl std::ops::Add for Jobs { type Output = Jobs; fn add(mut self, mut rhs: Self) -> Self::Output { self.0.append(&mut rhs.0); self } } impl From> for Jobs { fn from(fr: Vec) -> Self { Jobs::new(fr) } } #[derive(Clone, Debug, Message)] #[rtype(result = "()")] pub struct Job { pub name: String, pub command: Command, pub path: String, pub env: HashMap, pub retry: bool, pub keep_alive: bool, pub retry_delay: u64, pub color: (u8, u8, u8), } impl Default for Job { fn default() -> Self { let mut rng = rand::thread_rng(); Job { name: "Unnamed".to_string(), command: "".into(), path: ".".to_string(), env: HashMap::new(), retry: true, keep_alive: true, retry_delay: 2, color: ( rng.gen_range(100..255), rng.gen_range(100..255), rng.gen_range(100..255), ), } } }