natter/src/command/build/runner.rs

29 lines
1015 B
Rust
Raw Normal View History

use crate::blog_post::BlogPost;
use crate::cli::parameters::BuildArgs;
use crate::config::Config;
use crate::error::CustomError;
use walkdir::WalkDir;
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 post_directories = WalkDir::new(config.get_posts_directory())
.into_iter()
.filter(|e| match e {
Ok(entry) if entry.depth() == 1 && entry.file_type().is_dir() => true,
Ok(_) => false,
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()?;
let load_jobs = post_directories
.into_iter()
.map(walkdir::DirEntry::into_path)
.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??);
}
println!("{:?}", blog_posts);
Ok(())
}