extern crate notify; use notify::{ RecursiveMode, Watcher, Result, Event, }; use std:: { fs, path::Path, path::PathBuf, sync::mpsc, }; use crate:: { ROOT_DIR, }; pub fn watch_files(file_list: &[PathBuf]) -> u8 { let (tx, rx) = mpsc::channel::>(); // Use recommended_watcher() to automatically select the best implementation // for your platform. The `EventHandler` passed to this constructor can be a // closure, a `std::sync::mpsc::Sender`, a `crossbeam_channel::Sender`, or // another type the trait is implemented for. let mut watcher = notify::recommended_watcher(tx).expect("Watcher start failed"); // Add a path to be watched. All files and directories at that path and // below will be monitored for changes. watcher.watch(Path::new("root"), RecursiveMode::Recursive).expect("Path watching failed"); // Block forever, printing out events as they come in for res in rx { match res { Ok(event) => { // Ignore access event kinds as they're irrelevant if ! notify::EventKind::is_access(&event.kind) && file_list.contains(&event.paths[0]) { return 0 } }, Err(e) => { println!("watch error: {:?}", e); return 1 } } } println!("Watching files..."); 2 } pub fn read_file(file_name: &str, depth: u8) -> ( Vec, Vec, ) { if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name)) { let (bytes, file_list) = encode_npon(file_contents.as_str(), depth); return (bytes, file_list) } (vec![], vec![]) // return } fn encode_npon(file_contents: &str, depth: u8) -> ( Vec, Vec, ) { let mut bytes: Vec = vec![]; let mut file_list: Vec = vec![]; if depth > 63 { println!("Depth level safety reached (63), recursion error suspected"); return (vec![], file_list); } if depth == 0 { bytes.push(1); } // flag // 0 = nothing // 1 = in a string // 2 = in a name // 3 = in import let mut index_flag: usize = 0; let mut flag: u8 = 0; let mut backslash_flag: bool = false; let mut temp_string: Vec = vec![]; for (index, character) in file_contents.bytes().enumerate() { if backslash_flag { match character { 110 => // newline { bytes.push(14); }, 116 => // tab { bytes.push(15); }, _ => bytes.push(character), // Otherwise just push the character } backslash_flag = false; continue } backslash_flag = false; //print!("{} ", character); match flag { 0 => { match character { 40 => { temp_string.push(3); // ( for name start flag = 2; } 123 => bytes.push(5), 125 => bytes.push(6), 58 => { index_flag = index + 1; flag = 3; }, 39 | 34 => { temp_string.push(7); flag = 1; }, _ => (), } continue }, 1 => // in a string { match character { 39 | 34 => { temp_string.push(8); bytes.append(&mut temp_string); flag = 0; }, 92 => { backslash_flag = true; }, _ => temp_string.push(character), } continue }, 2 => // in a name { match character { 41 => { temp_string.push(4); bytes.append(&mut temp_string); // ) for name end temp_string = vec![]; flag = 0; }, 92 => { backslash_flag = true; }, _ => temp_string.push(character), } continue }, 3 => // in import { match character { 58 => { file_list.push(ROOT_DIR.join(&file_contents[index_flag..index])); let (new_bytes, mut new_file_list) = read_file ( &file_contents[index_flag..index], depth+1 ); file_list.append(&mut new_file_list); for byte in new_bytes.iter() { bytes.push(*byte); } flag = 0; } _ => (), } continue }, _ => println!("ERROR: Invalid flag code or 'null'"), } } if depth == 0 { bytes.push(2); } (bytes, file_list) // return }