2025-02-22 15:23:46 +00:00
|
|
|
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
|
2025-02-21 15:28:41 +00:00
|
|
|
|
|
|
|
use crate::Message;
|
|
|
|
|
|
|
|
pub struct Bundle {
|
2025-02-22 15:23:46 +00:00
|
|
|
path: PathBuf,
|
|
|
|
messages: HashMap<String, Message>,
|
2025-02-21 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bundle {
|
2025-02-22 15:23:46 +00:00
|
|
|
pub fn load_from_disk(path: PathBuf) -> Self {
|
2025-02-21 15:28:41 +00:00
|
|
|
Self {
|
2025-02-22 15:23:46 +00:00
|
|
|
path,
|
|
|
|
messages: HashMap::new(),
|
2025-02-21 15:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2025-02-22 15:23:46 +00:00
|
|
|
|
|
|
|
pub fn message(&mut self, name: String) -> &mut Message {
|
|
|
|
self.messages
|
|
|
|
.entry(name.to_owned())
|
|
|
|
.or_insert(Message::new(name.to_owned()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self) {
|
|
|
|
self.messages.iter().for_each(|(key, value)| {
|
|
|
|
let mut path = self.path.clone();
|
|
|
|
path.push(key);
|
|
|
|
path.set_extension("toml");
|
|
|
|
save_file(&path, toml::to_string(value).unwrap().as_bytes());
|
|
|
|
});
|
|
|
|
}
|
2025-02-21 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
2025-02-22 15:23:46 +00:00
|
|
|
fn save_file(path: &PathBuf, s: &[u8]) {
|
|
|
|
let mut f = File::create(path).unwrap();
|
|
|
|
f.write(s).unwrap();
|
|
|
|
}
|
2025-02-21 15:28:41 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2025-02-22 15:23:46 +00:00
|
|
|
mod test {}
|