From 14859feb4caaea250c9d78eb2539bd2622d24018 Mon Sep 17 00:00:00 2001 From: berrysweet Date: Wed, 5 Jul 2023 22:43:26 +0200 Subject: [PATCH] feat: add listing files in a template directory --- Cargo.toml | 3 ++- src/main.rs | 7 +++++++ src/util/templating.rs | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/util/templating.rs diff --git a/Cargo.toml b/Cargo.toml index 3ff4829..948bc3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] confy = "0.5.1" -serde = { version = "1.0.164", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0.164", features = ["derive"] } +walkdir = "2.3.3" diff --git a/src/main.rs b/src/main.rs index 8cdfcec..ddfc8bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ mod data { pub mod formats; } +mod util { + pub mod templating; +} const APP_NAME: &str = "livejrnl-rs"; @@ -9,6 +12,10 @@ fn main() { match config_data { Ok(config_data) => { println!("{:#?}", config_data); + + let template_files = util::templating::get_templates(config_data.template); + println!("{:?}", template_files); + }, Err(error) => println!("{:#?}", error) }; diff --git a/src/util/templating.rs b/src/util/templating.rs new file mode 100644 index 0000000..28242cb --- /dev/null +++ b/src/util/templating.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; +use std::path::PathBuf; +use std::str::FromStr; +use walkdir::WalkDir; + +pub fn get_templates(template: String) -> HashMap { + let template_path = PathBuf::from_str(&template); + let mut templates: HashMap = HashMap::new(); + for path in WalkDir::new(template_path.unwrap()).into_iter().filter_map(|e| e.ok()) { + if path.metadata().unwrap().is_file() { + let relative_path = PathBuf::from(path.path()); + let relative_path = relative_path.strip_prefix(&template).unwrap(); + let full_path = path.path().display().to_string(); + if let Ok(file) = std::fs::read_to_string(&full_path) { + templates.insert(relative_path.to_str().unwrap_or_default().to_string(), file); + } + } + } + templates +} \ No newline at end of file -- libgit2 1.7.2