monorepo/l10n-db/src/bundle.rs

41 lines
935 B
Rust
Raw Normal View History

2025-02-22 15:23:46 +00:00
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
use crate::Message;
pub struct Bundle {
2025-02-22 15:23:46 +00:00
path: PathBuf,
messages: HashMap<String, Message>,
}
impl Bundle {
2025-02-22 15:23:46 +00:00
pub fn load_from_disk(path: PathBuf) -> Self {
Self {
2025-02-22 15:23:46 +00:00
path,
messages: HashMap::new(),
}
}
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-22 15:23:46 +00:00
fn save_file(path: &PathBuf, s: &[u8]) {
let mut f = File::create(path).unwrap();
f.write(s).unwrap();
}
#[cfg(test)]
2025-02-22 15:23:46 +00:00
mod test {}