refactor errors into map_err

This commit is contained in:
2024-01-16 20:35:13 +01:00
parent 834f998788
commit 7f4e3e27e1
4 changed files with 22 additions and 40 deletions

View File

@ -21,7 +21,7 @@ pub struct PostMetadata {
#[template(path = "post.html")]
pub struct PostTemplate {
pub title: String,
pub content: String,
pub body: String,
}
pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, StatusCode> {
@ -29,6 +29,6 @@ pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, St
let parsed = parse_post::<PostMetadata>(&path).await?;
Ok(PostTemplate {
title: parsed.metadata.title,
content: parsed.content,
body: parsed.body,
})
}

View File

@ -16,18 +16,12 @@ pub struct PostListTemplate {
pub async fn render_post_list() -> Result<PostListTemplate, StatusCode> {
let path = "../_posts/blog/";
let dir = read_dir(path).await;
let mut dir = read_dir(path)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let mut posts: Vec<ParseResult<PostMetadata>> = Vec::new();
let mut files = match dir {
Err(_reason) => {
// TODO find the real reason
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
Ok(files) => files,
};
while let Some(file) = files
while let Some(file) = dir
.next_entry()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?