Skip to content

Commit

Permalink
Bookmarks serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrGlad committed Nov 6, 2023
1 parent 539b30f commit fce81ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Project {

impl Project {
const DIRECTORY_NAME_SUFFIX: &'static str = "emmate";
const SNAPSHOTS_DIR_NAME: &'static str = "snapshots";

pub fn open_file(source_file: &PathBuf) -> Project {
dbg!("source file", source_file.to_string_lossy());
Expand All @@ -21,8 +22,15 @@ impl Project {
}
directory.set_extension("");
directory.set_extension(Project::DIRECTORY_NAME_SUFFIX);

let mut snapshots_dir = directory.clone();
snapshots_dir.push(Self::SNAPSHOTS_DIR_NAME);
if !snapshots_dir.is_dir() {
fs::create_dir_all(&snapshots_dir).expect("create snapshots directory");
}

let mut history = if directory.is_dir() {
TrackHistory::with_directory(&directory)
TrackHistory::with_directory(&snapshots_dir)
} else {
fs::create_dir_all(&directory).expect(
format!(
Expand All @@ -31,7 +39,7 @@ impl Project {
)
.as_str(),
);
TrackHistory::with_directory(&directory).init(&source_file)
TrackHistory::with_directory(&snapshots_dir).init(&source_file)
};
history.open();
Project {
Expand Down
26 changes: 24 additions & 2 deletions src/stave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ pub struct Bookmarks {
}

impl Bookmarks {
pub fn load_from(&mut self, file_path: &PathBuf) {
pub fn load_from(file_path: &PathBuf) -> Bookmarks {
let binary = std::fs::read(file_path)
.expect(&*format!("load bookmarks from {}", &file_path.display()));
self.list = rmp_serde::from_slice(&binary).expect("deserialize bookmarks");
rmp_serde::from_slice(&binary).expect("deserialize bookmarks")
}

pub fn store_to(&self, file_path: &PathBuf) {
Expand Down Expand Up @@ -905,3 +905,25 @@ fn note_color(velocity: &Level, selected: bool) -> Color32 {
};
egui::lerp(c..=Rgba::from_rgb(0.0, 0.0, 0.0), *velocity as f32 / 128.0).into()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn bookmarks_serialization() {
let file_path = PathBuf::from("./target/test_bookmarks_serialization");
let bm1 = Bookmark { at: 12 };
let bm2 = Bookmark { at: 23 };

let mut bookmarks = Bookmarks::default();
bookmarks.list.insert(bm1);
bookmarks.list.insert(bm2);
bookmarks.store_to(&file_path);

let loaded = Bookmarks::load_from(&file_path);
assert_eq!(loaded.list.len(), 2);
assert!(loaded.list.contains(&bm1));
assert!(loaded.list.contains(&bm2));
}
}

0 comments on commit fce81ef

Please sign in to comment.