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:
39
src/files.rs
39
src/files.rs
@@ -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,30 +169,24 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
||||
},
|
||||
_ => temp_string.push(character),
|
||||
}
|
||||
continue
|
||||
},
|
||||
3 => // in import
|
||||
{
|
||||
match character
|
||||
if character == 58
|
||||
{
|
||||
58 =>
|
||||
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
|
||||
);
|
||||
file_list.append(&mut new_file_list);
|
||||
for byte in &new_bytes
|
||||
{
|
||||
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
|
||||
);
|
||||
file_list.append(&mut new_file_list);
|
||||
for byte in new_bytes.iter()
|
||||
{
|
||||
bytes.push(*byte);
|
||||
}
|
||||
flag = 0;
|
||||
bytes.push(*byte);
|
||||
}
|
||||
_ => (),
|
||||
flag = 0;
|
||||
}
|
||||
continue
|
||||
},
|
||||
_ => println!("ERROR: Invalid flag code or 'null'"),
|
||||
}
|
||||
|
||||
55
src/main.rs
55
src/main.rs
@@ -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.
|
||||
@@ -24,15 +28,8 @@ lazy_static! {
|
||||
// TODO, Have timers to see how inefficient my code is
|
||||
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";
|
||||
|
||||
#![warn(clippy::pedantic, clippy::perf)]
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let listener = TcpListener::bind(BIND_ADDRESS).unwrap();
|
||||
println!("Listening at {BIND_ADDRESS}");
|
||||
|
||||
// File watch thread
|
||||
thread::spawn(
|
||||
move || {
|
||||
@@ -58,22 +55,37 @@ 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 mut bytes: Vec<u8> = vec![];
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let bytes_result = rx.try_recv();
|
||||
bytes = match bytes_result
|
||||
let listener_result = TcpListener::bind(BIND_ADDRESS);
|
||||
match listener_result
|
||||
{
|
||||
Ok(listener) =>
|
||||
{
|
||||
Ok(bytes_data) => bytes_data,
|
||||
Err(_) => bytes,
|
||||
};
|
||||
let stream = stream.unwrap();
|
||||
handle_connection(stream, &bytes);
|
||||
println!("Listening at {BIND_ADDRESS}");
|
||||
|
||||
let mut bytes: Vec<u8> = vec![];
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let bytes_result = rx.try_recv();
|
||||
bytes = match bytes_result
|
||||
{
|
||||
Ok(bytes_data) => bytes_data,
|
||||
Err(_) => bytes,
|
||||
};
|
||||
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, ¬p_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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
34
src/pages.rs
34
src/pages.rs
@@ -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,26 +31,22 @@ 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(opening_index) = find_pattern(bytes, &search_pages_bytes)
|
||||
&& let Some(closing_index) = find_closing_brace(bytes, opening_index+search_pages_bytes.len())
|
||||
{
|
||||
if 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], &search_bytes)
|
||||
{
|
||||
//println!("found pages: {} to {}", opening_index, closing_index);
|
||||
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..closing_index+1], &search_bytes)
|
||||
//println!("{:?}", &bytes[opening_index+opening_page_index..]);
|
||||
if let Some(closing_page_index) = find_closing_brace
|
||||
(
|
||||
&bytes[opening_index+opening_page_index..],
|
||||
search_bytes.len()
|
||||
)
|
||||
{
|
||||
//println!("{:?}", &bytes[opening_index+opening_page_index..]);
|
||||
if let Some(closing_page_index) = find_closing_brace
|
||||
(
|
||||
&bytes[opening_index+opening_page_index..],
|
||||
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.push(2);
|
||||
return page_bytes
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user