website-risg/src/part.rs
philipp 98aed71ef5
Some checks are pending
CI/CD Pipeline / deploy-main (push) Waiting to run
clean with pedantic clippy
2024-02-21 14:28:16 +01:00

33 lines
996 B
Rust

use std::{collections::HashMap, fs, path::Path};
pub(crate) struct Parts {
parts: HashMap<String, String>,
}
impl Parts {
pub(crate) fn new() -> Self {
let mut parts = HashMap::new();
let parts_path = Path::new("templates/parts");
for part in fs::read_dir(parts_path).expect("No templates/parts folder") {
let part = part.unwrap();
let filename = part.file_name().into_string().unwrap().to_string();
let filename = filename.replace(".html", "");
let path = format!("{}", part.path().display());
let template = fs::read_to_string(path).unwrap();
parts.insert(filename, template);
}
Self { parts }
}
pub(crate) fn perform(&self, input: String) -> String {
let mut input = input;
for (key, value) in &self.parts {
let replace = format!("{{{{{key}}}}}");
input = input.replace(&replace, value);
}
input
}
}