212 lines
7.2 KiB
Rust
212 lines
7.2 KiB
Rust
//! # `epoch` sub‑command
|
||
//!
|
||
//! Handles all epoch‑related logic – logging, switching, computing the
|
||
//! next epoch, and resetting future events.
|
||
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
|
||
|
||
use crate::error::AppError;
|
||
use lib_efo::{epoch_struct::EpochConfig, epoch_traits::ConfigWritable, error::EpochError};
|
||
use log::{debug, info};
|
||
|
||
pub mod cli_epoch;
|
||
|
||
/// **Public entry point used by `efo::main`** – dispatches to the real
|
||
/// handler that lives in `crate::world::EpochManager`.
|
||
pub fn handle_cmd(cmd: cli_epoch::EpochCmd) -> Result<(), AppError> {
|
||
let mut main_epoch = EpochConfig::new_from_path(None)?;
|
||
// // TODO[2]: Remove this debug
|
||
debug!("The main epoch befor any action is {:#?}", main_epoch);
|
||
match cmd {
|
||
cli_epoch::EpochCmd::Log(args) => handle_log(args, &mut main_epoch),
|
||
cli_epoch::EpochCmd::Switch(args) => handle_switch(args, &mut main_epoch),
|
||
cli_epoch::EpochCmd::Next(args) => handle_next(args, &mut main_epoch),
|
||
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),
|
||
cli_epoch::EpochCmd::Fsck(args) => handle_fsck(args, &mut main_epoch),
|
||
}?;
|
||
|
||
info!("The main epoch after all actions is {:#?}", main_epoch);
|
||
Ok(())
|
||
}
|
||
|
||
/// Show the diff between two epochs
|
||
fn handle_diff(args: cli_epoch::Diff) -> Result<(), AppError> {
|
||
let _ = args;
|
||
unimplemented!("Blocker: There is actually no way to diff the world yet")
|
||
}
|
||
|
||
/// Show a log of all different epochs
|
||
fn handle_log(args: cli_epoch::Log, main_epoch: &mut EpochConfig) -> Result<(), AppError> {
|
||
// TODO: Have a special centralized message dispacher
|
||
println!("Epoch log (most recent first):");
|
||
let limit = args.limit;
|
||
let start_id = &main_epoch.epoch_id;
|
||
|
||
let list_id = main_epoch
|
||
.list_existing_epochs
|
||
.iter()
|
||
.filter(|(id, _)| id <= &start_id)
|
||
.take(limit)
|
||
.map(|(id, _)| id)
|
||
.copied()
|
||
.collect::<Vec<_>>();
|
||
|
||
main_epoch.log_elements(&list_id)?;
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// Switches the current epoch to the one specified by the user.
|
||
///
|
||
/// This updates the in‑memory configuration, reloads the target epoch,
|
||
/// updates the epoch identifier, and persists the changes.
|
||
///
|
||
/// # Arguments
|
||
/// * `args` – `cli_epoch::Switch` containing the target epoch identifier.
|
||
/// * `main_epoch` – mutable reference to the current `EpochConfig`.
|
||
fn handle_switch(args: cli_epoch::Switch, main_epoch: &mut EpochConfig) -> Result<(), AppError> {
|
||
let target = main_epoch.get(&args.target)?;
|
||
main_epoch.try_load(&target)?;
|
||
main_epoch.epoch_id = target;
|
||
|
||
// TODO: Have a special centralized message dispacher
|
||
println!("Switched to epoch {:?}", target);
|
||
main_epoch.save_with_context(
|
||
"Saving modified epoch config",
|
||
&lib_efo::epoch_struct::SaveMode::Erase,
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Handle the `next` sub‑command.
|
||
///
|
||
/// Advances the epoch to the next scheduled event. If the current
|
||
/// configuration contains any epochs with an identifier larger than
|
||
/// the current one, an error is returned – otherwise the next epoch
|
||
/// is computed.
|
||
///
|
||
/// # Arguments
|
||
/// * `main_epoch` – mutable reference to the current `EpochConfig`
|
||
/// (the `cli_epoch::Next` argument is unused).
|
||
///
|
||
/// # Returns
|
||
/// * `Ok(())` if the next epoch was successfully computed.
|
||
/// * `Err(AppError::Epoch(EpochError::FutureEpochExist))` if there are
|
||
/// no future epochs to advance to.
|
||
fn handle_next(_args: cli_epoch::Next, main_epoch: &mut EpochConfig) -> Result<(), AppError> {
|
||
let current_id = main_epoch.epoch_id;
|
||
|
||
// Check that there are no future epochs
|
||
if main_epoch
|
||
.list_existing_epochs
|
||
.keys()
|
||
.any(|id| id > ¤t_id)
|
||
{
|
||
Err(AppError::Epoch(EpochError::FutureEpochExist))
|
||
} else {
|
||
let new_epoch = main_epoch.compute_next()?;
|
||
|
||
// Insert new epoch
|
||
main_epoch
|
||
.insert_and_save(new_epoch, &lib_efo::epoch_struct::SaveMode::DoNotErase)
|
||
.unwrap();
|
||
Ok(())
|
||
}
|
||
}
|
||
|
||
/// Handle the `reset` sub‑command.
|
||
///
|
||
/// Deletes all epochs whose identifier is greater than the current one,
|
||
/// effectively rolling back to the present epoch.
|
||
///
|
||
/// # Arguments
|
||
/// * `main_epoch` – mutable reference to the current `EpochConfig`
|
||
/// (the `cli_epoch::Reset` argument is unused).
|
||
///
|
||
/// # Returns
|
||
/// * `Ok(())` if all future epochs were successfully removed.
|
||
/// * `Err(AppError)` if any deletion fails.
|
||
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::<Vec<_>>()
|
||
{
|
||
main_epoch.delete_id(id)?
|
||
}
|
||
// TODO: Have a special centralized message dispacher
|
||
println!("All future events deleted – back to epoch {current_id}",);
|
||
Ok(())
|
||
}
|
||
|
||
/// Handle the `tag` sub‑command.
|
||
///
|
||
/// Adds a tag to the current epoch. The tag must not already exist
|
||
/// elsewhere in the configuration; otherwise an error is returned.
|
||
///
|
||
/// # Arguments
|
||
/// * `args` – `cli_epoch::Tag` containing the tag to add.
|
||
/// * `main_epoch` – mutable reference to the current `EpochConfig`.
|
||
///
|
||
/// # Returns
|
||
/// * `Ok(())` if the tag was successfully added.
|
||
/// * `Err(AppError::Epoch(EpochError::TagAlreadyExists))` if the tag
|
||
/// already exists in another epoch.
|
||
/// * `Err(AppError::Epoch(EpochError::Unknown))` if the epoch cannot
|
||
/// be found or saved.
|
||
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
|
||
#[allow(clippy::unwrap_used)]
|
||
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_with_context(
|
||
"Saving modified epoch",
|
||
&lib_efo::epoch_struct::SaveMode::Erase,
|
||
)?;
|
||
main_epoch
|
||
.tags
|
||
.insert(args.tag.clone(), main_epoch.epoch_id);
|
||
main_epoch.save_with_context(
|
||
"Saving modified main config",
|
||
&lib_efo::epoch_struct::SaveMode::Erase,
|
||
)?;
|
||
Ok(())
|
||
} else {
|
||
Err(AppError::Epoch(lib_efo::error::EpochError::Unknown(
|
||
format!("Couldn't add the tag {}", args.tag),
|
||
)))
|
||
}
|
||
}
|
||
|
||
/// Performs a basic sanity check.
|
||
///
|
||
/// Currently unimplemented – the function will panic at runtime.
|
||
///
|
||
fn handle_fsck(args: cli_epoch::Fsck, main_epoch: &mut EpochConfig) -> Result<(), AppError> {
|
||
let _ = main_epoch;
|
||
let _ = args;
|
||
unimplemented!()
|
||
}
|