refactor errors into map_err
This commit is contained in:
parent
834f998788
commit
7f4e3e27e1
@ -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)?
|
||||
|
@ -20,22 +20,17 @@ where
|
||||
}
|
||||
|
||||
pub struct ParseResult<Metadata> {
|
||||
pub content: String,
|
||||
pub body: String,
|
||||
pub metadata: Metadata,
|
||||
}
|
||||
|
||||
pub async fn parse_post<'de, Metadata: DeserializeOwned>(
|
||||
path: &str,
|
||||
) -> Result<ParseResult<Metadata>, StatusCode> {
|
||||
let contents = fs::read_to_string(path).await;
|
||||
|
||||
let raw_content = match contents {
|
||||
Err(_reason) => {
|
||||
// TODO find the real reason
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
Ok(content) => content,
|
||||
};
|
||||
let file_contents = fs::read_to_string(path)
|
||||
.await
|
||||
// TODO Proper reasoning for an error
|
||||
.map_err(|_| StatusCode::NOT_FOUND)?;
|
||||
|
||||
let markdown_options = Options {
|
||||
parse: ParseOptions {
|
||||
@ -52,28 +47,21 @@ pub async fn parse_post<'de, Metadata: DeserializeOwned>(
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let parsed = to_html_with_options(&raw_content, &markdown_options);
|
||||
let content = match parsed {
|
||||
Err(reason) => {
|
||||
let body = to_html_with_options(&file_contents, &markdown_options).map_err(|reason| {
|
||||
tracing::error!(reason);
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
Ok(content) => content,
|
||||
};
|
||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||
})?;
|
||||
|
||||
let matter = Matter::<YAML>::new();
|
||||
let parsed_metadata = matter.parse_with_struct::<Metadata>(&raw_content);
|
||||
|
||||
let metadata = match parsed_metadata {
|
||||
None => {
|
||||
let metadata = matter
|
||||
.parse_with_struct::<Metadata>(&file_contents)
|
||||
.ok_or_else(|| {
|
||||
tracing::error!("Failed to parse metadata");
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
Some(metadata) => metadata,
|
||||
};
|
||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||
})?;
|
||||
|
||||
return Ok(ParseResult {
|
||||
content,
|
||||
body,
|
||||
metadata: metadata.data,
|
||||
});
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
{% block content %}
|
||||
<h1>{{title}}</h1>
|
||||
|
||||
<article>{{content|escape("none")}}</article>
|
||||
<article>{{body|escape("none")}}</article>
|
||||
|
||||
{# footer #}
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user