Skip to main content

fun_folder_common/
process.rs

1use std::path::PathBuf;
2use std::process::Command;
3use std::{fmt, str::FromStr};
4
5pub struct HandlerProcess{
6    handled_path: Option<PathBuf>, // Path listening to.
7    worker_program_path: Option<PathBuf>, // Path to executable worker.
8    pid: Option<u32> // Process id of live handler.
9}
10
11impl HandlerProcess {
12    pub fn new(handled_str_path: &str, program_str_path: &str) -> Self {
13        
14        let mut command = Command::new(program_str_path);
15        if let Ok(child) = command.spawn() {
16            let handled_path = PathBuf::from_str(handled_str_path).unwrap();
17            let program_path = PathBuf::from_str(program_str_path).unwrap();
18            Self{pid: Some(child.id()), handled_path: Some(handled_path), worker_program_path: Some(program_path)}
19        } else {
20            Self{pid: None, handled_path: None, worker_program_path: None}
21        }
22    }
23
24    pub fn new_handle_current_folder(program_str_path: &str) -> Self {
25        Self::new(".", program_str_path)
26    }
27}
28
29impl fmt::Display for HandlerProcess{
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        writeln!(f, "PID: {:?}. Handle path: {:?}. Worker path: {:?}", self.pid, self.handled_path, self.worker_program_path)
32    }
33}
34
35pub fn build_worker(package_folder_str_path: &str){
36    assert!(try_clean_worker_build(package_folder_str_path));
37    let status = Command::new("cargo").arg("build")
38    .current_dir(package_folder_str_path).status().expect("Worker build process failed to execute");
39    assert!(status.success());
40}
41
42fn try_clean_worker_build(package_folder_str_path: &str) -> bool{
43    let status = Command::new("cargo").arg("clean")
44    .current_dir(package_folder_str_path).status().expect("Worker cleanup process failed to execute");
45    return status.success();
46}