Skip to content

Commit

Permalink
improvement: uses BSON for saving projects so versino can be checked
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomaz-Vieira committed Jul 8, 2024
1 parent fb551fb commit 5d5b379
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 100 deletions.
169 changes: 85 additions & 84 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bioimg_gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ serde_json = "1.0.116"
zip = { workspace = true }
uuid = { version = "1.8.0", features = ["v4"] }
bioimg_codegen = { path = "../bioimg_codegen" }
postcard = { version = "1.0.8", features = ["alloc", "use-std"] }
bson = "2.11.0"

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
28 changes: 15 additions & 13 deletions bioimg_gui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bioimg_spec::rdf::{self, ResourceName};
use bioimg_spec::rdf::bounded_string::BoundedString;
use bioimg_spec::rdf::non_empty_list::NonEmptyList;

use crate::project_data::AppStateRawData;
use crate::project_data::{AppStateRawData, ProjectLoadError};
use crate::result::{GuiError, Result, VecResultExt};
use crate::widgets::attachments_widget::AttachmentsWidget;

Expand Down Expand Up @@ -162,10 +162,9 @@ impl eframe::App for AppState1 {
.create(true)
.truncate(true)
.open(&path).map_err(|err| format!("Could not open project file for writing: {err}"))?;
postcard::to_io(
&AppStateRawData::Version1(self.dump()), &writer
).map_err(|err| format!("Could not serialize project to bytes: {err}"))?;
Ok(format!("Saved project to {}", path.to_string_lossy()))
AppStateRawData::Version1(self.dump()).save(writer)
.map_err(|err| format!("Could not serialize project to bytes: {err}"))
.map(|_| format!("Saved project to {}", path.to_string_lossy()))
}();
self.notifications_widget.push_message(result);
}}
Expand All @@ -175,14 +174,17 @@ impl eframe::App for AppState1 {
break 'load_project;
};
let result = || -> Result<(), String>{
println!("Started OPENING file at {:?}", std::time::Instant::now());
let mut project_file = std::fs::File::open(&path).map_err(|err| format!("Could not open project file: {err}"))?;
println!("Started READING file at {:?}", std::time::Instant::now());
let mut project_bytes = Vec::<u8>::new();
project_file.read_to_end(&mut project_bytes).map_err(|err| format!("Could not read file: {err}"))?;
let loaded_project: AppStateRawData = postcard::from_bytes(&project_bytes)
.map_err(|err| format!("Could not deserialize project: {err}"))?;
match loaded_project{
let reader = std::fs::File::open(&path).map_err(|err| format!("Could not open project file: {err}"))?;
let proj_data = match AppStateRawData::load(reader){
Err(ProjectLoadError::FutureVersion{found_version}) => return Err(format!(
"Found project data version {found_version}, but this program only supports project data up to {}\n\
You can try downloading the newest version at https://github.com/kreshuklab/bioimg_rs/releases",
AppStateRawData::highest_supported_version(),
)),
Err(err) => return Err(format!("Could not load project file at {}: {err}", path.to_string_lossy())),
Ok(proj_data) => proj_data,
};
match proj_data{
AppStateRawData::Version1(ver1) => self.restore(ver1),
}
Ok(())
Expand Down
Loading

0 comments on commit 5d5b379

Please sign in to comment.