From 413cc09758925d53f6960f39181035d5d38cce34 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 28 Nov 2025 17:16:41 +0100 Subject: [PATCH] Adds most of next and delete logic --- bin_efo/src/commands/epoch.rs | 13 ++++++++++--- bin_efo/src/main.rs | 2 -- lib_efo/src/epoch_struct.rs | 30 +++++++++++++++++------------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/bin_efo/src/commands/epoch.rs b/bin_efo/src/commands/epoch.rs index 96e191f..1717cd8 100644 --- a/bin_efo/src/commands/epoch.rs +++ b/bin_efo/src/commands/epoch.rs @@ -26,7 +26,11 @@ pub fn handle_cmd(cmd: cli_epoch::EpochCmd) -> Result<(), AppError> { cli_epoch::EpochCmd::Diff(args) => handle_diff(args), cli_epoch::EpochCmd::Tag(args) => handle_taging(args, &mut main_epoch), cli_epoch::EpochCmd::Fsck(args) => handle_fsck(args, &mut main_epoch), - } + }?; + + // TODO: Remove this debug + println!("The main epoch is {:#?}", main_epoch); + Ok(()) } /// Show the diff between two epochs @@ -115,14 +119,17 @@ fn handle_next(_args: cli_epoch::Next, main_epoch: &mut EpochConfig) -> Result<( fn handle_reset(args: cli_epoch::Reset, main_epoch: &mut EpochConfig) -> Result<(), AppError> { let _ = args; let current_id = main_epoch.epoch_id; + // for id in main_epoch for id in main_epoch .list_existing_epochs .keys() .filter(|&k| k > ¤t_id) + .copied() + // Middle step necessary for borrowing purposes + .collect::>() { - main_epoch.delete_id(*id)? + main_epoch.delete_id(id)? } - println!("All future events deleted – back to epoch {current_id}",); Ok(()) } diff --git a/bin_efo/src/main.rs b/bin_efo/src/main.rs index 4654c76..a7afd47 100644 --- a/bin_efo/src/main.rs +++ b/bin_efo/src/main.rs @@ -75,8 +75,6 @@ fn main() -> miette::Result<()> { // AppError::Storage(storage_error) => todo!(), // AppError::Other(_) => todo!(), // } - println!("The program didn't exit properly and returned the following error:"); - // println!("{}", e); Err(e)?; } diff --git a/lib_efo/src/epoch_struct.rs b/lib_efo/src/epoch_struct.rs index 8432369..fb9d488 100644 --- a/lib_efo/src/epoch_struct.rs +++ b/lib_efo/src/epoch_struct.rs @@ -121,7 +121,10 @@ impl EpochConfig { /// Try to load into memory the given epoch id pub fn try_load(&mut self, epoch_id: &EpochId) -> Result<(), EpochError> { // Preload the epoch - let value = self.list_existing_epochs.get_mut(epoch_id).unwrap(); + let value = self + .list_existing_epochs + .get_mut(epoch_id) + .ok_or(EpochError::EpochNotFound(format!("{epoch_id}")))?; // Create a mock epoch let path = EpochInfo::with_id(epoch_id).save_path(); @@ -166,7 +169,8 @@ impl EpochConfig { } /// Tries to delete an entire epoch folder by Id - pub fn delete_id(&self, id: EpochId) -> Result<(), EpochError> { + // TODO: Create a delete_ids taking an iterator for added performance on huge deletions + pub fn delete_id(&mut self, id: EpochId) -> Result<(), EpochError> { if !self.list_existing_epochs.contains_key(&id) { println!( "The given epoch {} doesn't exists and it's deletion was skipped", @@ -178,6 +182,9 @@ impl EpochConfig { "Removing the epoch {}", id )?; + self.list_existing_epochs.remove(&id); + self.tags.retain(|_k, v| *v != id); + self.save_with_context("Saving after delete_id", &SaveMode::Erase)?; Ok(()) } @@ -201,18 +208,15 @@ impl EpochConfig { return Err(EpochError::EpochAlreadyExists(new_epoch.epoch_id)); } - // TODO: Finish - new_epoch - .save_with_context("Saving new epoch without subfolders", save_mode) - .unwrap(); - new_epoch.create_subfolders().unwrap(); - new_epoch.create_subfolders().unwrap(); - new_epoch.create_subfolders().unwrap(); - new_epoch - .save_with_context("Saving new epoch", save_mode) - .unwrap(); + new_epoch.create_subfolders()?; + new_epoch.save_with_context("Saving new epoch", save_mode)?; - // Add the epoch to the list of epochs + // Add the epoch to the list of epochs, not doing so will still have it added + // at the next iteration since the command always parse the files to determine it's state rater that the list + // not doing so would likely create errors if insert is used before another command + self.list_existing_epochs + .insert(new_epoch.epoch_id, Some(new_epoch)); + self.save_with_context("Saving after insert_and_save", &SaveMode::Erase)?; Ok(()) } }