diff --git a/src/main.rs b/src/main.rs index ac4d123..3d5f3c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,13 +106,20 @@ fn build_ui(app: &Application) { let file = dialog.file().expect("Couldn't get file"); eprintln!("Open"); let filename = file.path().expect("Couldn't get file path"); - let contents = fs::read_to_string(filename).expect("Something went wrong reading the file"); + let contents = fs::read_to_string(filename.clone()).expect("Something went wrong reading the file"); eprintln!("{}", contents); let new_dataset : Vec = serde_json::from_str(&contents).unwrap(); let mut state = state.borrow_mut(); state.replace_foucs_stacks(new_dataset); + state.save_path = Some( + filename.clone().as_path() + .to_str() + .expect("failed to convert filname to str") + .to_string() + ); + eprintln!("{}", state.save_path.clone().unwrap()); image_ui.update(&state); } dialog.close(); @@ -147,6 +154,7 @@ fn build_ui(app: &Application) { let mut state = state.borrow_mut(); state.mark_focus(); + state.save(); state.skip(); image_ui.update(&state); })); diff --git a/src/state/mod.rs b/src/state/mod.rs index 64477b9..63ff497 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -1,4 +1,8 @@ +use std::io::Write; +use std::{collections::HashMap, fs::File}; + use serde::{Deserialize, Serialize}; +use serde_json::Value; use crate::constants::NONE_STRING_OPTION; #[derive(Debug, Clone)] @@ -6,6 +10,7 @@ pub struct State { stacks: Vec, stack_index: Option, pub image_index: Option, + pub save_path: Option, } impl State { @@ -14,6 +19,7 @@ impl State { stacks: Vec::new(), stack_index: None, image_index: None, + save_path: None, } } pub fn set_image_index(&mut self, image_index: Option) { @@ -86,6 +92,29 @@ impl State { } } + pub fn save(&self) { + if let Some(path) = self.save_path.clone() { + match File::create(path) { + Ok(mut file) => { + let contents = + serde_json::to_string(&self.stacks).expect("Could not serialize data."); + match file.write(contents.as_bytes()) { + Ok(_) => {} + Err(e) => { + eprintln!("an error occured while saving: {}", e.to_string()); + } + } + } + Err(e) => { + eprintln!("{}", e.to_string()); + } + }; + } else { + // TODO: error dialogue + eprintln!("No save path specified"); + } + } + pub fn previous(&mut self) { let len = self.stacks.len(); if len == 0 { @@ -100,12 +129,18 @@ impl State { pub struct AnnotationZStack { pub images: Vec, pub best_index: Option, + + #[serde(flatten)] + extra: HashMap, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AnnotationImage { pub image_path: String, pub neighbours: [Option; 8], + + #[serde(flatten)] + extra: HashMap, } impl AnnotationImage { @@ -118,6 +153,7 @@ impl AnnotationImage { AnnotationImage { image_path, neighbours: _neighbours, + extra: HashMap::new(), } } }