refactor errors into map_err
This commit is contained in:
@ -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,
|
||||
})
|
||||
}
|
||||
|
@ -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)?
|
||||
|
Reference in New Issue
Block a user