Moving into a load_blog_post function to create a BlogPost struct.

This commit is contained in:
Tom Alexander 2023-10-22 12:04:09 -04:00
parent 816780589f
commit d8fc49797e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,61 @@
#[derive(Debug)]
struct BlogPost {
id: String,
}
impl BlogPost {
pub(crate) async fn load_blog_post<P: AsRef<Path>, R: AsRef<Path>>(
root_dir: R,
post_dir: P,
) -> Result<BlogPost, Box<dyn std::error::Error>> {
async fn inner(
root_dir: &Path,
post_dir: &Path,
) -> Result<BlogPost, Box<dyn std::error::Error>> {
let org_files = {
let mut ret = Vec::new();
let org_files_iter = get_org_files(post_dir)?;
for entry in org_files_iter {
ret.push(entry.await??);
}
ret
};
let parsed_org_files = {
let mut ret = Vec::new();
for (path, contents) in org_files.iter() {
let parsed = organic::parser::parse_file(contents.as_str(), Some(path))?;
ret.push((path, contents, parsed));
}
ret
};
Ok(BlogPost {
id: "foo".to_owned(),
})
}
inner(root_dir.as_ref(), post_dir.as_ref()).await
}
}
fn get_org_files<P: AsRef<Path>>(
root_dir: P,
) -> Result<impl Iterator<Item = JoinHandle<std::io::Result<(PathBuf, String)>>>, walkdir::Error> {
let org_files = WalkDir::new(root_dir)
.into_iter()
.filter(|e| match e {
Ok(dir_entry) => {
dir_entry.file_type().is_file()
&& Path::new(dir_entry.file_name())
.extension()
.map(|ext| ext.to_ascii_lowercase() == "org")
.unwrap_or(false)
}
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()?;
let org_files = org_files
.into_iter()
.map(walkdir::DirEntry::into_path)
.map(|path| tokio::spawn(read_file(path)));
Ok(org_files)
}

0
src/blog_post/mod.rs Normal file
View File

View File

@ -6,6 +6,7 @@ use self::cli::parameters::Cli;
use self::cli::parameters::Commands;
use self::command::build::build_site;
use self::command::init::init_writer_folder;
mod blog_post;
mod cli;
mod command;
mod config;