feat: add listing files in a template directory
Diff
Cargo.toml | 3 ++-
src/main.rs | 7 +++++++
src/util/templating.rs | 20 ++++++++++++++++++++
3 files changed, 29 insertions(+), 1 deletion(-)
@@ -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"
@@ -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)
};
@@ -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<String, String> {
let template_path = PathBuf::from_str(&template);
let mut templates: HashMap<String, String> = 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