Serialize the RenderBlogPost to JSON.

This struct still does not contain anything, but I'm just setting up the skeleton for this code.
This commit is contained in:
Tom Alexander 2023-10-22 16:01:42 -04:00
parent b72aec9d20
commit 0b6900eeca
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 40 additions and 12 deletions

8
src/blog_post/convert.rs Normal file
View File

@ -0,0 +1,8 @@
use super::render_context::RenderBlogPost;
use super::BlogPost;
pub(crate) fn convert_blog_post_to_render_context(post: &BlogPost) -> RenderBlogPost {
RenderBlogPost {
id: post.id.clone(),
}
}

View File

@ -8,7 +8,7 @@ use crate::error::CustomError;
#[derive(Debug)]
pub(crate) struct BlogPost {
id: String,
pub(crate) id: String,
}
impl BlogPost {

View File

@ -1,3 +1,5 @@
mod convert;
mod definition;
mod render_context;
pub(crate) use convert::convert_blog_post_to_render_context;
pub(crate) use definition::BlogPost;

View File

@ -4,5 +4,5 @@ use serde::Serialize;
#[serde(tag = "type")]
#[serde(rename = "blog_post")]
pub(crate) struct RenderBlogPost {
id: String,
pub(crate) id: String,
}

View File

@ -1,5 +1,6 @@
use std::path::PathBuf;
use crate::blog_post::convert_blog_post_to_render_context;
use crate::blog_post::BlogPost;
use crate::cli::parameters::BuildArgs;
use crate::config::Config;
@ -7,17 +8,14 @@ use crate::error::CustomError;
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> {
let config = Config::load_from_file(args.config).await?;
let root_directory = config.get_root_directory().to_owned();
let output_directory = get_output_directory(&config).await?;
let post_directories = get_post_directories(&config).await?;
let load_jobs = post_directories
.into_iter()
.map(|path| tokio::spawn(BlogPost::load_blog_post(root_directory.clone(), path)));
let mut blog_posts = Vec::new();
for job in load_jobs {
blog_posts.push(job.await??);
}
let blog_posts = load_blog_posts(&config).await?;
println!("{:?}", blog_posts);
for blog_post in &blog_posts {
let render_context = convert_blog_post_to_render_context(blog_post);
println!("{}", serde_json::to_string(&render_context)?);
}
Ok(())
}
@ -51,3 +49,16 @@ async fn get_post_directories(config: &Config) -> Result<Vec<PathBuf>, CustomErr
}
Ok(ret)
}
async fn load_blog_posts(config: &Config) -> Result<Vec<BlogPost>, CustomError> {
let root_directory = config.get_root_directory().to_owned();
let post_directories = get_post_directories(&config).await?;
let load_jobs = post_directories
.into_iter()
.map(|path| tokio::spawn(BlogPost::load_blog_post(root_directory.clone(), path)));
let mut blog_posts = Vec::new();
for job in load_jobs {
blog_posts.push(job.await??);
}
Ok(blog_posts)
}

View File

@ -6,6 +6,7 @@ pub(crate) enum CustomError {
TomlDeserialize(toml::de::Error),
WalkDir(walkdir::Error),
Tokio(tokio::task::JoinError),
Serde(serde_json::Error),
}
impl From<std::io::Error> for CustomError {
@ -43,3 +44,9 @@ impl From<tokio::task::JoinError> for CustomError {
CustomError::Tokio(value)
}
}
impl From<serde_json::Error> for CustomError {
fn from(value: serde_json::Error) -> Self {
CustomError::Serde(value)
}
}