Shortened a function a bit, would love to continue but I've got so much
homework
This commit is contained in:
119
src/files.rs
119
src/files.rs
@@ -17,6 +17,14 @@ use crate::
|
|||||||
ROOT_DIR,
|
ROOT_DIR,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Copy,Clone)]
|
||||||
|
enum Mode {
|
||||||
|
Default,
|
||||||
|
String,
|
||||||
|
Name,
|
||||||
|
Import,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn watch_files(file_list: &[PathBuf]) -> u8
|
pub fn watch_files(file_list: &[PathBuf]) -> u8
|
||||||
{
|
{
|
||||||
let (tx, rx) = mpsc::channel::<Result<Event>>(); // Use recommended_watcher() to automatically select the best implementation
|
let (tx, rx) = mpsc::channel::<Result<Event>>(); // 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
|
(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<u8>, 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<u8>, temp_string: &mut Vec<u8>, 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) ->
|
fn encode_npon(file_contents: &str, depth: u8) ->
|
||||||
(
|
(
|
||||||
Vec<u8>,
|
Vec<u8>,
|
||||||
@@ -82,60 +131,23 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
|||||||
bytes.push(1);
|
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 index_flag: usize = 0;
|
||||||
let mut flag: u8 = 0;
|
let mut flag: Mode = Mode::Default;
|
||||||
let mut backslash_flag: bool = false;
|
let mut backslash_flag: bool = false;
|
||||||
let mut temp_string: Vec<u8> = vec![];
|
let mut temp_string: Vec<u8> = vec![];
|
||||||
for (index, character) in file_contents.bytes().enumerate()
|
for (index, character) in file_contents.bytes().enumerate()
|
||||||
{
|
{
|
||||||
if backslash_flag
|
if backslash_flag
|
||||||
{
|
{
|
||||||
match character
|
// Push a newline or tab character when there's a \n or \t
|
||||||
{
|
// Otherwise, just push the raw sequence
|
||||||
110 => // newline
|
backslash(&mut bytes, character);
|
||||||
{
|
|
||||||
bytes.push(14);
|
|
||||||
},
|
|
||||||
116 => // tab
|
|
||||||
{
|
|
||||||
bytes.push(15);
|
|
||||||
},
|
|
||||||
_ => bytes.push(character), // Otherwise just push the character
|
|
||||||
}
|
|
||||||
backslash_flag = false;
|
backslash_flag = false;
|
||||||
}
|
}
|
||||||
match flag
|
match flag
|
||||||
{
|
{
|
||||||
0 =>
|
Mode::Default => default_flag(&mut bytes, &mut temp_string, &mut flag, &mut index_flag, character, index),
|
||||||
{
|
Mode::String => // in a string
|
||||||
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
|
|
||||||
{
|
{
|
||||||
match character
|
match character
|
||||||
{
|
{
|
||||||
@@ -143,16 +155,13 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
|||||||
{
|
{
|
||||||
temp_string.push(8);
|
temp_string.push(8);
|
||||||
bytes.append(&mut temp_string);
|
bytes.append(&mut temp_string);
|
||||||
flag = 0;
|
flag = Mode::Default;
|
||||||
},
|
|
||||||
92 =>
|
|
||||||
{
|
|
||||||
backslash_flag = true;
|
|
||||||
},
|
},
|
||||||
|
92 => backslash_flag = true,
|
||||||
_ => temp_string.push(character),
|
_ => temp_string.push(character),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
2 => // in a name
|
Mode::Name => // in a name
|
||||||
{
|
{
|
||||||
match character
|
match character
|
||||||
{
|
{
|
||||||
@@ -161,16 +170,13 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
|||||||
temp_string.push(4);
|
temp_string.push(4);
|
||||||
bytes.append(&mut temp_string); // ) for name end
|
bytes.append(&mut temp_string); // ) for name end
|
||||||
temp_string = vec![];
|
temp_string = vec![];
|
||||||
flag = 0;
|
flag = Mode::Default;
|
||||||
},
|
|
||||||
92 =>
|
|
||||||
{
|
|
||||||
backslash_flag = true;
|
|
||||||
},
|
},
|
||||||
|
92 => backslash_flag = true,
|
||||||
_ => temp_string.push(character),
|
_ => temp_string.push(character),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
3 => // in import
|
Mode::Import => // in import
|
||||||
{
|
{
|
||||||
if character == 58
|
if character == 58
|
||||||
{
|
{
|
||||||
@@ -185,10 +191,9 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
|||||||
{
|
{
|
||||||
bytes.push(*byte);
|
bytes.push(*byte);
|
||||||
}
|
}
|
||||||
flag = 0;
|
flag = Mode::Default;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => println!("ERROR: Invalid flag code or 'null'"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ pub fn get_page(bytes: &[u8], page: &str) -> Vec<u8>
|
|||||||
|
|
||||||
if let Some(opening_index) = find_pattern(bytes, &search_pages_bytes)
|
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())
|
&& 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);
|
//println!("found pages: {} to {}", opening_index, closing_index);
|
||||||
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..=closing_index], &search_bytes)
|
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..=closing_index], &search_bytes)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user