Adds a basic tagging system
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2025-11-27 18:00:24 +01:00
parent 4cd3d4de98
commit 3e309291e8
3 changed files with 56 additions and 3 deletions

View File

@ -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),
)))
}
}

View File

@ -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<EpochIdentifier>,
}
/// `efo epoch tag "tag"`
#[derive(Args, Debug)]
pub struct Tag {
#[arg(required = true)]
/// The tag to tag the current epoch with
pub tag: String,
}

View File

@ -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<String>,
) -> 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(())
}
}