website-risg/src/part.rs

33 lines
1001 B
Rust
Raw Normal View History

2024-02-20 13:03:06 +01:00
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 = format!("{}", part.file_name().into_string().unwrap());
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
}
}