use std::path::Path; use std::path::PathBuf; use crate::cli::parameters::BuildArgs; use crate::config::Config; use tokio::task::JoinHandle; use walkdir::WalkDir; pub(crate) async fn build_site(args: BuildArgs) -> Result<(), Box> { let config = Config::load_from_file(args.config).await?; let org_files = { let mut ret = Vec::new(); let org_files_iter = get_org_files(config.get_root_directory())?; 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(()) } async fn read_file(path: PathBuf) -> std::io::Result<(PathBuf, String)> { let contents = tokio::fs::read_to_string(&path).await?; Ok((path, contents)) } fn get_org_files>( root_dir: P, ) -> Result>>, 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::, _>>()?; let org_files = org_files .into_iter() .map(walkdir::DirEntry::into_path) .map(|path| tokio::spawn(read_file(path))); Ok(org_files) }