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")]
|
#[template(path = "post.html")]
|
||||||
pub struct PostTemplate {
|
pub struct PostTemplate {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub content: String,
|
pub body: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, StatusCode> {
|
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?;
|
let parsed = parse_post::<PostMetadata>(&path).await?;
|
||||||
Ok(PostTemplate {
|
Ok(PostTemplate {
|
||||||
title: parsed.metadata.title,
|
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> {
|
pub async fn render_post_list() -> Result<PostListTemplate, StatusCode> {
|
||||||
let path = "../_posts/blog/";
|
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 posts: Vec<ParseResult<PostMetadata>> = Vec::new();
|
||||||
|
|
||||||
let mut files = match dir {
|
while let Some(file) = dir
|
||||||
Err(_reason) => {
|
|
||||||
// TODO find the real reason
|
|
||||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
Ok(files) => files,
|
|
||||||
};
|
|
||||||
|
|
||||||
while let Some(file) = files
|
|
||||||
.next_entry()
|
.next_entry()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||||
|
@ -20,22 +20,17 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ParseResult<Metadata> {
|
pub struct ParseResult<Metadata> {
|
||||||
pub content: String,
|
pub body: String,
|
||||||
pub metadata: Metadata,
|
pub metadata: Metadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn parse_post<'de, Metadata: DeserializeOwned>(
|
pub async fn parse_post<'de, Metadata: DeserializeOwned>(
|
||||||
path: &str,
|
path: &str,
|
||||||
) -> Result<ParseResult<Metadata>, StatusCode> {
|
) -> Result<ParseResult<Metadata>, StatusCode> {
|
||||||
let contents = fs::read_to_string(path).await;
|
let file_contents = fs::read_to_string(path)
|
||||||
|
.await
|
||||||
let raw_content = match contents {
|
// TODO Proper reasoning for an error
|
||||||
Err(_reason) => {
|
.map_err(|_| StatusCode::NOT_FOUND)?;
|
||||||
// TODO find the real reason
|
|
||||||
return Err(StatusCode::NOT_FOUND);
|
|
||||||
}
|
|
||||||
Ok(content) => content,
|
|
||||||
};
|
|
||||||
|
|
||||||
let markdown_options = Options {
|
let markdown_options = Options {
|
||||||
parse: ParseOptions {
|
parse: ParseOptions {
|
||||||
@ -52,28 +47,21 @@ pub async fn parse_post<'de, Metadata: DeserializeOwned>(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let parsed = to_html_with_options(&raw_content, &markdown_options);
|
let body = to_html_with_options(&file_contents, &markdown_options).map_err(|reason| {
|
||||||
let content = match parsed {
|
tracing::error!(reason);
|
||||||
Err(reason) => {
|
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
tracing::error!(reason);
|
})?;
|
||||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
Ok(content) => content,
|
|
||||||
};
|
|
||||||
|
|
||||||
let matter = Matter::<YAML>::new();
|
let matter = Matter::<YAML>::new();
|
||||||
let parsed_metadata = matter.parse_with_struct::<Metadata>(&raw_content);
|
let metadata = matter
|
||||||
|
.parse_with_struct::<Metadata>(&file_contents)
|
||||||
let metadata = match parsed_metadata {
|
.ok_or_else(|| {
|
||||||
None => {
|
|
||||||
tracing::error!("Failed to parse metadata");
|
tracing::error!("Failed to parse metadata");
|
||||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
}
|
})?;
|
||||||
Some(metadata) => metadata,
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(ParseResult {
|
return Ok(ParseResult {
|
||||||
content,
|
body,
|
||||||
metadata: metadata.data,
|
metadata: metadata.data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>{{title}}</h1>
|
<h1>{{title}}</h1>
|
||||||
|
|
||||||
<article>{{content|escape("none")}}</article>
|
<article>{{body|escape("none")}}</article>
|
||||||
|
|
||||||
{# footer #}
|
{# footer #}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user