diff --git a/bin_efo/src/commands/epoch.rs b/bin_efo/src/commands/epoch.rs index 8a9aaf8..587f283 100644 --- a/bin_efo/src/commands/epoch.rs +++ b/bin_efo/src/commands/epoch.rs @@ -3,7 +3,10 @@ //! Handles all epoch‑related logic – logging, switching, computing the //! next epoch, and resetting future events. -use lib_efo::epoch_struct::{ConfigWritable, EpochConfig}; +use lib_efo::{ + epoch_struct::{ConfigWritable, EpochConfig}, + error::EpochError, +}; use crate::error::AppError; @@ -20,6 +23,7 @@ pub fn handle_cmd(cmd: cli_epoch::EpochCmd) -> Result<(), AppError> { cli_epoch::EpochCmd::Next(args) => handle_next(args), cli_epoch::EpochCmd::Reset(args) => handle_reset(args, &mut main_epoch), cli_epoch::EpochCmd::Diff(args) => handle_diff(args), + cli_epoch::EpochCmd::Tag(args) => handle_taging(args, &mut main_epoch), } } @@ -68,3 +72,42 @@ fn handle_reset(args: cli_epoch::Reset, main_epoch: &mut EpochConfig) -> Result< println!("All future events deleted – back to epoch {current_id}",); Ok(()) } + +/// Adds a tag to the current epoch +fn handle_taging(args: cli_epoch::Tag, main_epoch: &mut EpochConfig) -> Result<(), AppError> { + // Load into memory the current epoch + main_epoch.try_load(&main_epoch.epoch_id.clone())?; + + // Check if the tag doens't exists + if main_epoch.tags.contains_key(&args.tag) { + Err(AppError::Epoch(EpochError::TagAlreadyExists( + main_epoch.epoch_id, + args.tag.clone(), + )))?; + } + + // Modify the epoch to add the tag inside + if let Some(current_epoch_info) = main_epoch + .list_existing_epochs + .get_mut(&main_epoch.epoch_id) + .unwrap() + { + current_epoch_info.tags.push(args.tag.clone()); + current_epoch_info.save( + "Saving modified epoch".to_owned(), + lib_efo::epoch_struct::SaveMode::Erase, + )?; + main_epoch + .tags + .insert(args.tag.clone(), main_epoch.epoch_id); + main_epoch.save( + "Saving modified main config".into(), + lib_efo::epoch_struct::SaveMode::Erase, + )?; + Ok(()) + } else { + Err(AppError::Epoch(lib_efo::error::EpochError::Unknown( + format!("Couldn't add the tag {}", args.tag), + ))) + } +} diff --git a/bin_efo/src/commands/epoch/cli_epoch.rs b/bin_efo/src/commands/epoch/cli_epoch.rs index c5773d3..25b53a4 100644 --- a/bin_efo/src/commands/epoch/cli_epoch.rs +++ b/bin_efo/src/commands/epoch/cli_epoch.rs @@ -20,6 +20,7 @@ pub enum EpochCmd { Diff(Diff), // Tag and next could be implemented as subcommands of epoch // even if they barely belong here + Tag(Tag), } /// `efo epoch log` @@ -60,3 +61,11 @@ pub struct Diff { /// The end point to compute the differences, the current epoch by default pub end: Option, } + +/// `efo epoch tag "tag"` +#[derive(Args, Debug)] +pub struct Tag { + #[arg(required = true)] + /// The tag to tag the current epoch with + pub tag: String, +} diff --git a/lib_efo/src/epoch_struct.rs b/lib_efo/src/epoch_struct.rs index 4c6b70f..131dd3a 100644 --- a/lib_efo/src/epoch_struct.rs +++ b/lib_efo/src/epoch_struct.rs @@ -203,7 +203,8 @@ pub trait ConfigWritable: Serialize { /// Save the config into it's nominal path fn save( &self, - action: String, + // Very unsure about this versus a macro + action_description: String, do_not_erase: SaveMode, // advice: Option, ) -> Result<(), EpochError> { @@ -212,7 +213,7 @@ pub trait ConfigWritable: Serialize { SaveMode::DoNotErase => File::create_new(self.save_path()), } .map_err(|e| EpochError::ConfigCreation(self.save_path(), e))?; - write!(f, "{}", self.as_json(action)?).map_err(EpochError::ConfigWrite)?; + write!(f, "{}", self.as_json(action_description)?).map_err(EpochError::ConfigWrite)?; Ok(()) } }