From 521db9fef9f7b3a7f2466f28919e140ccc39337a Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 26 Dec 2022 21:30:03 -0600 Subject: [PATCH] Final round of refactoring for now + Moved away from blocking FS ops + Clippy + fmt --- src/main.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2605ad5..0fb4987 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ mod config; mod error; -mod tests; use axum::error_handling::HandleErrorLayer; use axum::extract::{Path, State}; @@ -46,7 +45,7 @@ async fn index(State(state): State>) -> PageResult let mut links: Vec = Vec::new(); // Get the links to display on the main page - get_pages("static/raw_md", &mut links)?; + get_pages("static/raw_md", &mut links).await?; ctx.insert("links", &links); Ok(Html(state.render("index.html.tera", &ctx)?)) @@ -60,12 +59,12 @@ async fn index(State(state): State>) -> PageResult /// # Arguments /// * `path` - the path to look for pages in /// * `pages` - A vector where found pages will be inserted -fn get_pages(path: &str, pages: &mut Vec) -> PageResult<()> { +async fn get_pages(path: &str, pages: &mut Vec) -> PageResult<()> { let re = Regex::new(r"(?P^\d*)(?P.+)").unwrap(); // Find all files in the directory - for entry in std::fs::read_dir(path)? { - let entry = entry?; + let mut dir = tokio::fs::read_dir(path).await?; + while let Some(entry) = dir.next_entry().await? { let path = entry.path(); let file_name = match path.file_stem() { Some(name) => name, @@ -81,13 +80,9 @@ fn get_pages(path: &str, pages: &mut Vec) -> PageResult<()> { let link_name = &caps["link_name"]; let rank = &caps["rank"]; - let rank: u32 = if rank.is_empty() { - std::u32::MAX - } else { - match rank.parse() { - Ok(r) => r, - Err(_) => u32::MAX, - } + let rank = match rank.parse() { + Ok(r) => r, + Err(_) => u32::MAX, }; let site_file = SiteFile { @@ -169,7 +164,7 @@ async fn md_page(tera: State>, Path(page): Path) -> PageResul // If the file is a directory, list its contents instead let mut map = Context::new(); let mut sub_files: Vec = Vec::new(); - match get_pages(site_page.path.to_str().unwrap(), &mut sub_files) { + match get_pages(site_page.path.to_str().unwrap(), &mut sub_files).await { Ok(_) => (), Err(_) => return error_page(&tera, &site_page.link_name).await, } @@ -184,7 +179,7 @@ async fn md_page(tera: State>, Path(page): Path) -> PageResul } else { // Else, render the MD page let mut map = Context::new(); - let contents = match std::fs::read_to_string(site_page.path.clone()) { + let contents = match tokio::fs::read_to_string(site_page.path.clone()).await { Ok(contents) => contents, Err(_) => return error_page(&tera, site_page.path.to_str().unwrap()).await, };