diff --git a/src/files.rs b/src/files.rs index 448a129..42f334b 100644 --- a/src/files.rs +++ b/src/files.rs @@ -17,6 +17,14 @@ use crate:: ROOT_DIR, }; +#[derive(Copy,Clone)] +enum Mode { + Default, + String, + Name, + Import, +} + pub fn watch_files(file_list: &[PathBuf]) -> u8 { let (tx, rx) = mpsc::channel::>(); // Use recommended_watcher() to automatically select the best implementation @@ -66,6 +74,47 @@ pub fn read_file(file_name: &str, depth: u8) -> (vec![], vec![]) // return } +/// Push a newline or tab character when there's a \n or \t +/// Otherwise, just push the raw sequence +fn backslash(bytes: &mut Vec, character: u8) +{ + match character + { + 110 => // newline + { + bytes.push(14); + }, + 116 => // tab + { + bytes.push(15); + }, + _ => bytes.push(character), // Otherwise just push the character + } +} +fn default_flag(bytes: &mut Vec, temp_string: &mut Vec, flag: &mut Mode, index_flag: &mut usize, character: u8, index: usize) +{ + match character + { + 40 => + { + temp_string.push(3); // ( for name start + *flag = Mode::Name; + } + 123 => bytes.push(5), + 125 => bytes.push(6), + 58 => + { + *index_flag = index + 1; + *flag = Mode::Import; + }, + 39 | 34 => + { + temp_string.push(7); + *flag = Mode::String; + }, + _ => (), + } +} fn encode_npon(file_contents: &str, depth: u8) -> ( Vec, @@ -82,60 +131,23 @@ fn encode_npon(file_contents: &str, depth: u8) -> 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 flag: Mode = Mode::Default; 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 - } + // Push a newline or tab character when there's a \n or \t + // Otherwise, just push the raw sequence + backslash(&mut bytes, character); backslash_flag = false; } 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; - }, - _ => (), - } - }, - 1 => // in a string + Mode::Default => default_flag(&mut bytes, &mut temp_string, &mut flag, &mut index_flag, character, index), + Mode::String => // in a string { match character { @@ -143,16 +155,13 @@ fn encode_npon(file_contents: &str, depth: u8) -> { temp_string.push(8); bytes.append(&mut temp_string); - flag = 0; - }, - 92 => - { - backslash_flag = true; + flag = Mode::Default; }, + 92 => backslash_flag = true, _ => temp_string.push(character), } }, - 2 => // in a name + Mode::Name => // in a name { match character { @@ -161,16 +170,13 @@ fn encode_npon(file_contents: &str, depth: u8) -> temp_string.push(4); bytes.append(&mut temp_string); // ) for name end temp_string = vec![]; - flag = 0; - }, - 92 => - { - backslash_flag = true; + flag = Mode::Default; }, + 92 => backslash_flag = true, _ => temp_string.push(character), } }, - 3 => // in import + Mode::Import => // in import { if character == 58 { @@ -185,10 +191,9 @@ fn encode_npon(file_contents: &str, depth: u8) -> { bytes.push(*byte); } - flag = 0; + flag = Mode::Default; } }, - _ => println!("ERROR: Invalid flag code or 'null'"), } } if depth == 0 { diff --git a/src/pages.rs b/src/pages.rs index 814316d..b15108f 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -33,7 +33,7 @@ pub fn get_page(bytes: &[u8], page: &str) -> Vec if let Some(opening_index) = find_pattern(bytes, &search_pages_bytes) && let Some(closing_index) = find_closing_brace(bytes, opening_index+search_pages_bytes.len()) - { + { // TODO ignore when in quotes or name //println!("found pages: {} to {}", opening_index, closing_index); if let Some(opening_page_index) = find_pattern(&bytes[opening_index..=closing_index], &search_bytes) {