diff --git a/src/project.rs b/src/project.rs index 9654cb4..31ef166 100644 --- a/src/project.rs +++ b/src/project.rs @@ -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()); @@ -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!( @@ -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 { diff --git a/src/stave.rs b/src/stave.rs index f32113f..282929a 100644 --- a/src/stave.rs +++ b/src/stave.rs @@ -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) { @@ -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)); + } +}