Shortened a function a bit, would love to continue but I've got so much

homework
This commit is contained in:
2025-11-30 12:14:31 +00:00
parent ee8e97ddb0
commit 54ec8c22d5
2 changed files with 63 additions and 58 deletions

View File

@@ -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::<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
}
/// 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) ->
(
Vec<u8>,
@@ -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<u8> = 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 {