1
Fork 0

loadstore

This commit is contained in:
Andy Killorin 2025-03-27 18:54:39 -04:00
parent c0daf3f64c
commit a2b629beab
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 7 additions and 4 deletions

View file

@ -1,4 +1,4 @@
use std::{fmt::format, ops::ControlFlow, result, sync::{atomic::Ordering, Arc}, thread::{self, sleep}, time::Duration}; use std::{fmt::format, ops::ControlFlow, path::Path, result, sync::{atomic::Ordering, Arc}, thread::{self, sleep}, time::Duration};
use anyhow::{Context, Ok, Result}; use anyhow::{Context, Ok, Result};
use atomic_float::AtomicF32; use atomic_float::AtomicF32;
@ -17,7 +17,7 @@ fn main() -> Result<()> {
let directories = ProjectDirs::from("dev", "ank", "Cruise Control").context("no home")?; let directories = ProjectDirs::from("dev", "ank", "Cruise Control").context("no home")?;
let auto = Auto::new("../auto/target/release/libauto.so")?; let auto = Auto::new("../auto/target/release/libauto.so")?;
let storage = StorageManager::new(directories.config_dir().with_file_name("conf.dat"))?; let storage = StorageManager::new(dbg!(directories.config_dir().to_path_buf().join(Path::new("conf.dat"))))?;
println!("name: {}", auto.name()); println!("name: {}", auto.name());
@ -116,10 +116,12 @@ fn main() -> Result<()> {
println!("launching gui"); println!("launching gui");
let (_conf_sender, conf) = watch::channel(CONFIGS); let (_conf_sender, conf) = watch::channel(CONFIGS);
gui(gui_data, toasts, executor, conf, storage).unwrap(); gui(gui_data, toasts, executor, conf, storage.clone()).unwrap();
drop(stream); drop(stream);
storage.save();
Ok(()) Ok(())
} }

View file

@ -16,7 +16,7 @@ impl StorageManager {
let map = if let Ok(mut file) = File::open(path.clone()) { let map = if let Ok(mut file) = File::open(path.clone()) {
let mut vec = Vec::new(); let mut vec = Vec::new();
file.read_to_end(&mut vec)?; file.read_to_end(&mut vec)?;
postcard::from_bytes(&vec)? postcard::from_bytes_cobs(&mut vec)?
} else { } else {
DashMap::new() DashMap::new()
}; };
@ -29,6 +29,7 @@ impl StorageManager {
pub fn save(&self) { pub fn save(&self) {
let map = postcard::to_stdvec_cobs(&self.map).unwrap(); let map = postcard::to_stdvec_cobs(&self.map).unwrap();
// TODO: mkdir
let mut backing_file = File::options().write(true).truncate(true).create(true) let mut backing_file = File::options().write(true).truncate(true).create(true)
.open(self.storage.as_path()).unwrap(); .open(self.storage.as_path()).unwrap();
backing_file.write_all(&map).unwrap(); backing_file.write_all(&map).unwrap();