Fixed some pedant issues.

I need to fix the last two by splitting up encode into multiple
functions (probably a good thing)
and apparently changing the lazy static macro
This commit is contained in:
2025-11-30 02:12:09 +00:00
parent 173cf9085f
commit ee8e97ddb0
3 changed files with 62 additions and 66 deletions

View File

@@ -41,9 +41,9 @@ pub fn watch_files(file_list: &[PathBuf]) -> u8
return 0
}
},
Err(e) =>
Err(error) =>
{
println!("watch error: {:?}", e);
println!("watch error: {error:?}");
return 1
}
}
@@ -58,7 +58,7 @@ pub fn read_file(file_name: &str, depth: u8) ->
Vec<PathBuf>,
)
{
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.as_str(), depth);
return (bytes, file_list)
@@ -108,10 +108,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
_ => bytes.push(character), // Otherwise just push the character
}
backslash_flag = false;
continue
}
backslash_flag = false;
//print!("{} ", character);
match flag
{
0 =>
@@ -137,7 +134,6 @@ fn encode_npon(file_contents: &str, depth: u8) ->
},
_ => (),
}
continue
},
1 => // in a string
{
@@ -155,7 +151,6 @@ fn encode_npon(file_contents: &str, depth: u8) ->
},
_ => temp_string.push(character),
}
continue
},
2 => // in a name
{
@@ -174,13 +169,10 @@ fn encode_npon(file_contents: &str, depth: u8) ->
},
_ => temp_string.push(character),
}
continue
},
3 => // in import
{
match character
{
58 =>
if character == 58
{
file_list.push(ROOT_DIR.join(&file_contents[index_flag..index]));
let (new_bytes, mut new_file_list) = read_file
@@ -189,15 +181,12 @@ fn encode_npon(file_contents: &str, depth: u8) ->
depth+1
);
file_list.append(&mut new_file_list);
for byte in new_bytes.iter()
for byte in &new_bytes
{
bytes.push(*byte);
}
flag = 0;
}
_ => (),
}
continue
},
_ => println!("ERROR: Invalid flag code or 'null'"),
}

View File

@@ -14,9 +14,13 @@ use lazy_static::lazy_static;
mod files;
mod pages;
// Default values
lazy_static! {
static ref ROOT_DIR: PathBuf = fs::canonicalize(PathBuf::from("./root")).unwrap();
}
const BIND_ADDRESS: &str = "127.0.0.1:2008";
const INDEX_FILE: &str = "index.npon";
// TODO, remove .unwrap()
// TODO, make 'code' an enum
// TODO, safely modify bytes in thread.
@@ -25,14 +29,7 @@ lazy_static! {
fn main() -> io::Result<()>
{
#![warn(clippy::pedantic, clippy::perf)]
// Default values
const BIND_ADDRESS: &str = "127.0.0.1:2008";
const INDEX_FILE: &str = "index.npon";
let (tx, rx) = mpsc::channel();
let listener = TcpListener::bind(BIND_ADDRESS).unwrap();
println!("Listening at {BIND_ADDRESS}");
// File watch thread
thread::spawn(
move || {
@@ -58,11 +55,19 @@ fn main() -> io::Result<()>
file_list.push(ROOT_DIR.join(INDEX_FILE));
tx.send(bytes).unwrap();
},
error => println!("Watch error: {}", error),
error => println!("Watch error: {error}"),
}
}
}
);
let listener_result = TcpListener::bind(BIND_ADDRESS);
match listener_result
{
Ok(listener) =>
{
println!("Listening at {BIND_ADDRESS}");
let mut bytes: Vec<u8> = vec![];
for stream in listener.incoming() {
@@ -75,6 +80,13 @@ fn main() -> io::Result<()>
let stream = stream.unwrap();
handle_connection(stream, &bytes);
}
},
Err(error) =>
{
println!("Error starting listener: {error}");
return Err(error)
},
}
Ok(())
}
@@ -87,15 +99,14 @@ fn handle_connection(mut stream: TcpStream, bytes: &[u8])
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!("REQUEST: {:?}", notp_request); // TODO format requests in npon
println!("REQUEST: {notp_request:?}"); // TODO format requests in npon
let page_bytes = pages::get_page(bytes, &notp_request[0]); // first request line is the page atm
if let Err(e) = stream.write_all(&page_bytes)
if let Err(error) = stream.write_all(&page_bytes)
{
eprintln!("Failed to send response: {}", e);
eprintln!("Failed to send response: {error}");
}
else
{
println!("Sent response");
}
}

View File

@@ -1,5 +1,5 @@
// TODO these functions are so shit
/// Finds the index of the first occurrence of search_bytes in bytes.
/// Finds the index of the first occurrence of `search_bytes` in `bytes`.
fn find_pattern(bytes: &[u8], search_bytes: &[u8]) -> Option<usize>
{
bytes.windows(search_bytes.len())
@@ -9,7 +9,7 @@ fn find_pattern(bytes: &[u8], search_bytes: &[u8]) -> Option<usize>
fn find_closing_brace(bytes: &[u8], opening_brace_index: usize) -> Option<usize>
{
let mut depth: i8 = 0;
for i in opening_brace_index..bytes.len()-1
for (i, _) in bytes.iter().enumerate().take(bytes.len()-1).skip(opening_brace_index)
{
if bytes[i] == 5 {
depth += 1; }
@@ -31,12 +31,11 @@ pub fn get_page(bytes: &[u8], page: &str) -> Vec<u8>
search_bytes.append(&mut page.as_bytes().to_vec());
search_bytes.push(4);
if let Some(opening_index) = find_pattern(&bytes, &search_pages_bytes)
{
if let Some(closing_index) = find_closing_brace(&bytes, opening_index+search_pages_bytes.len())
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())
{
//println!("found pages: {} to {}", opening_index, closing_index);
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..closing_index+1], &search_bytes)
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..=closing_index], &search_bytes)
{
//println!("{:?}", &bytes[opening_index+opening_page_index..]);
if let Some(closing_page_index) = find_closing_brace
@@ -45,15 +44,12 @@ pub fn get_page(bytes: &[u8], page: &str) -> Vec<u8>
search_bytes.len()
)
{
//println!("found page: {} to {}", opening_index+opening_page_index, opening_index+closing_page_index);
//println!("found page: {:?}", &bytes[opening_index+opening_page_index..opening_index+opening_page_index+closing_page_index+1]);
page_bytes.append(&mut bytes[opening_page_index+opening_index..opening_page_index+closing_page_index+opening_index+1].to_vec());
page_bytes.append(&mut bytes[opening_page_index+opening_index..=opening_page_index+closing_page_index+opening_index].to_vec());
page_bytes.push(2);
return page_bytes
}
}
}
}
page_bytes.push(10); // Page not found error message
page_bytes.push(2);
page_bytes