index : livejrnl-rs.git

ascending towards madness

author berrysweet <berrysweet@noreply.codeberg.org> 2023-07-05 20:43:26.0 +00:00:00
committer berrysweet <berrysweet@noreply.codeberg.org> 2023-07-05 20:43:26.0 +00:00:00
commit
14859feb4caaea250c9d78eb2539bd2622d24018 [patch]
tree
6dc6754ca909cd8d7c8f67c74fd69a23cb9188cd
parent
104fa1d268d705d08a7835301c0dfd7a1d41aa71
download
14859feb4caaea250c9d78eb2539bd2622d24018.tar.gz

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(-)

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<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