This commit is contained in:
2026-01-20 21:13:42 -05:00
parent 6821e546db
commit ace356381a
8 changed files with 89 additions and 47 deletions

View File

@@ -20,13 +20,17 @@ impl Default for DataDir {
}
}
pub trait DataRsc: bincode::Encode + bincode::Decode<()> + Default {
fn path() -> &'static str;
}
impl DataDir {
pub fn get(&self) -> &Path {
self.dirs.data_local_dir()
}
pub fn load<T: bincode::Decode<()> + Default>(&self, path: &str) -> T {
let path = self.get().join(path);
pub fn load<T: DataRsc>(&self) -> T {
let path = self.get().join(T::path());
match fs::read(path) {
Ok(bytes) => match bincode::decode_from_slice(&bytes, BINCODE_CONFIG) {
Ok((data, _)) => data,
@@ -36,10 +40,10 @@ impl DataDir {
}
}
pub fn save<T: bincode::Encode>(&self, path: &str, data: &T) {
pub fn save<T: DataRsc>(&self, data: &T) {
let dir = self.get();
fs::create_dir_all(dir).unwrap();
let mut file = File::create(dir.join(path)).unwrap();
let mut file = File::create(dir.join(T::path())).unwrap();
bincode::encode_into_std_write(data, &mut file, BINCODE_CONFIG).unwrap();
}
}