Final round of refactoring for now
+ Moved away from blocking FS ops + Clippy + fmt
This commit is contained in:
parent
813c16a00c
commit
521db9fef9
19
src/main.rs
19
src/main.rs
@ -1,6 +1,5 @@
|
|||||||
mod config;
|
mod config;
|
||||||
mod error;
|
mod error;
|
||||||
mod tests;
|
|
||||||
|
|
||||||
use axum::error_handling::HandleErrorLayer;
|
use axum::error_handling::HandleErrorLayer;
|
||||||
use axum::extract::{Path, State};
|
use axum::extract::{Path, State};
|
||||||
@ -46,7 +45,7 @@ async fn index(State(state): State<Arc<Tera>>) -> PageResult<impl IntoResponse>
|
|||||||
let mut links: Vec<SiteFile> = Vec::new();
|
let mut links: Vec<SiteFile> = Vec::new();
|
||||||
|
|
||||||
// Get the links to display on the main page
|
// 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);
|
ctx.insert("links", &links);
|
||||||
Ok(Html(state.render("index.html.tera", &ctx)?))
|
Ok(Html(state.render("index.html.tera", &ctx)?))
|
||||||
@ -60,12 +59,12 @@ async fn index(State(state): State<Arc<Tera>>) -> PageResult<impl IntoResponse>
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `path` - the path to look for pages in
|
/// * `path` - the path to look for pages in
|
||||||
/// * `pages` - A vector where found pages will be inserted
|
/// * `pages` - A vector where found pages will be inserted
|
||||||
fn get_pages(path: &str, pages: &mut Vec<SiteFile>) -> PageResult<()> {
|
async fn get_pages(path: &str, pages: &mut Vec<SiteFile>) -> PageResult<()> {
|
||||||
let re = Regex::new(r"(?P<rank>^\d*)(?P<link_name>.+)").unwrap();
|
let re = Regex::new(r"(?P<rank>^\d*)(?P<link_name>.+)").unwrap();
|
||||||
|
|
||||||
// Find all files in the directory
|
// Find all files in the directory
|
||||||
for entry in std::fs::read_dir(path)? {
|
let mut dir = tokio::fs::read_dir(path).await?;
|
||||||
let entry = entry?;
|
while let Some(entry) = dir.next_entry().await? {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
let file_name = match path.file_stem() {
|
let file_name = match path.file_stem() {
|
||||||
Some(name) => name,
|
Some(name) => name,
|
||||||
@ -81,13 +80,9 @@ fn get_pages(path: &str, pages: &mut Vec<SiteFile>) -> PageResult<()> {
|
|||||||
let link_name = &caps["link_name"];
|
let link_name = &caps["link_name"];
|
||||||
let rank = &caps["rank"];
|
let rank = &caps["rank"];
|
||||||
|
|
||||||
let rank: u32 = if rank.is_empty() {
|
let rank = match rank.parse() {
|
||||||
std::u32::MAX
|
|
||||||
} else {
|
|
||||||
match rank.parse() {
|
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(_) => u32::MAX,
|
Err(_) => u32::MAX,
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let site_file = SiteFile {
|
let site_file = SiteFile {
|
||||||
@ -169,7 +164,7 @@ async fn md_page(tera: State<Arc<Tera>>, Path(page): Path<PathBuf>) -> PageResul
|
|||||||
// If the file is a directory, list its contents instead
|
// If the file is a directory, list its contents instead
|
||||||
let mut map = Context::new();
|
let mut map = Context::new();
|
||||||
let mut sub_files: Vec<SiteFile> = Vec::new();
|
let mut sub_files: Vec<SiteFile> = 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(_) => (),
|
Ok(_) => (),
|
||||||
Err(_) => return error_page(&tera, &site_page.link_name).await,
|
Err(_) => return error_page(&tera, &site_page.link_name).await,
|
||||||
}
|
}
|
||||||
@ -184,7 +179,7 @@ async fn md_page(tera: State<Arc<Tera>>, Path(page): Path<PathBuf>) -> PageResul
|
|||||||
} else {
|
} else {
|
||||||
// Else, render the MD page
|
// Else, render the MD page
|
||||||
let mut map = Context::new();
|
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,
|
Ok(contents) => contents,
|
||||||
Err(_) => return error_page(&tera, site_page.path.to_str().unwrap()).await,
|
Err(_) => return error_page(&tera, site_page.path.to_str().unwrap()).await,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user