Changed the file_contents to be stored in an &str

This commit is contained in:
2025-11-29 10:15:01 +00:00
parent ce643f2066
commit 0ee35b2795
2 changed files with 23 additions and 17 deletions

View File

@@ -49,7 +49,7 @@ pub fn watch_files(file_list: &Vec<PathBuf>) -> u8
} }
} }
println!("Watching files..."); println!("Watching files...");
return 1 1
} }
pub fn read_file(file_name: &str, depth: u8) -> pub fn read_file(file_name: &str, depth: u8) ->
@@ -60,13 +60,13 @@ pub fn read_file(file_name: &str, depth: u8) ->
{ {
if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name)) if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name))
{ {
let (bytes, file_list) = encode_npon(&file_contents, depth); let (bytes, file_list) = encode_npon(file_contents.as_str(), depth);
return (bytes, file_list) return (bytes, file_list)
} }
return (vec![], vec![]) (vec![], vec![]) // return
} }
fn encode_npon(file_contents: &String, depth: u8) -> fn encode_npon(file_contents: &str, depth: u8) ->
( (
Vec<u8>, Vec<u8>,
Vec<PathBuf>, Vec<PathBuf>,
@@ -79,7 +79,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
return (vec![], file_list); return (vec![], file_list);
} }
if depth == 0 { if depth == 0 {
bytes.push(01); bytes.push(1);
} }
// flag // flag
@@ -119,11 +119,11 @@ fn encode_npon(file_contents: &String, depth: u8) ->
{ {
40 => 40 =>
{ {
bytes.push(03); // ( for name start bytes.push(3); // ( for name start
flag = 2; flag = 2;
} }
123 => bytes.push(05), 123 => bytes.push(5),
125 => bytes.push(06), 125 => bytes.push(6),
58 => 58 =>
{ {
index_flag = index + 1; index_flag = index + 1;
@@ -131,7 +131,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
}, },
39 | 34 => 39 | 34 =>
{ {
bytes.push(07); bytes.push(7);
flag = 1; flag = 1;
}, },
_ => (), _ => (),
@@ -144,7 +144,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
{ {
39 | 34 => 39 | 34 =>
{ {
bytes.push(08); bytes.push(8);
flag = 0; flag = 0;
}, },
92 => 92 =>
@@ -161,7 +161,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
{ {
41 => 41 =>
{ {
bytes.push(04); // ) for name end bytes.push(4); // ) for name end
flag = 0; flag = 0;
}, },
92 => 92 =>
@@ -178,8 +178,12 @@ fn encode_npon(file_contents: &String, depth: u8) ->
{ {
58 => 58 =>
{ {
file_list.push(ROOT_DIR.join(&file_contents[index_flag..index.clone()])); 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); let (new_bytes, mut new_file_list) = read_file
(
&file_contents[index_flag..index],
depth+1
);
file_list.append(&mut new_file_list); file_list.append(&mut new_file_list);
for byte in new_bytes.iter() for byte in new_bytes.iter()
{ {
@@ -195,9 +199,9 @@ fn encode_npon(file_contents: &String, depth: u8) ->
} }
} }
if depth == 0 { if depth == 0 {
bytes.push(02); bytes.push(2);
} }
return (bytes, file_list) (bytes, file_list) // return
} }

View File

@@ -23,13 +23,14 @@ lazy_static! {
// TODO, Avoid copying bytes // TODO, Avoid copying bytes
fn main() -> io::Result<()> fn main() -> io::Result<()>
{ {
#![warn(clippy::pedantic, clippy::perf)]
// Default values // Default values
const BIND_ADDRESS: &str = "127.0.0.1:2008"; const BIND_ADDRESS: &str = "127.0.0.1:2008";
const INDEX_FILE: &str = "index.npon"; const INDEX_FILE: &str = "index.npon";
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let listener = TcpListener::bind(BIND_ADDRESS).unwrap(); let listener = TcpListener::bind(BIND_ADDRESS).unwrap();
println!("Listening at {}", BIND_ADDRESS); println!("Listening at {BIND_ADDRESS}");
/* /*
let mut file_list: Vec<std::path::PathBuf> = vec![ROOT_DIR.join(INDEX_FILE)]; let mut file_list: Vec<std::path::PathBuf> = vec![ROOT_DIR.join(INDEX_FILE)];
@@ -43,9 +44,10 @@ fn main() -> io::Result<()>
let mut file_list: Vec<PathBuf>; let mut file_list: Vec<PathBuf>;
(bytes, file_list) = files::read_file(INDEX_FILE, 0); (bytes, file_list) = files::read_file(INDEX_FILE, 0);
file_list.push(ROOT_DIR.join(INDEX_FILE)); file_list.push(ROOT_DIR.join(INDEX_FILE));
println!("Tracking: {:?}", file_list);
tx.send(bytes.clone()).unwrap(); tx.send(bytes.clone()).unwrap();
'watch_loop: loop loop
{ {
let code = files::watch_files(&file_list); let code = files::watch_files(&file_list);
if code == 0 if code == 0