122 lines
4.0 KiB
Rust
122 lines
4.0 KiB
Rust
//! # `epoch` sub‑command
|
||
//!
|
||
//! Handles all epoch‑related logic – logging, switching, computing the
|
||
//! next epoch, and resetting future events.
|
||
|
||
use lib_efo::{
|
||
epoch_struct::{ConfigWritable, EpochConfig},
|
||
error::EpochError,
|
||
};
|
||
|
||
use crate::error::AppError;
|
||
|
||
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: Remove this debug
|
||
println!("The main epoch is {:#?}", main_epoch);
|
||
match cmd {
|
||
cli_epoch::EpochCmd::Log(args) => handle_log(args),
|
||
cli_epoch::EpochCmd::Switch(args) => handle_switch(args, &mut main_epoch),
|
||
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),
|
||
cli_epoch::EpochCmd::Fsck(args) => handle_fsck(args, &mut main_epoch),
|
||
}
|
||
}
|
||
|
||
fn handle_diff(args: cli_epoch::Diff) -> Result<(), AppError> {
|
||
let _ = args;
|
||
unimplemented!("Blocker: There is actually no way to diff the world yet")
|
||
}
|
||
|
||
fn handle_log(_args: cli_epoch::Log) -> Result<(), AppError> {
|
||
println!("Epoch log (most recent first):");
|
||
unimplemented!("Just needs for the epoch to implement lvels of displays trait: DisplayLevels");
|
||
}
|
||
|
||
/// Switch the current epoch for another
|
||
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;
|
||
|
||
println!("Switched to epoch {:?}", target);
|
||
main_epoch.save(
|
||
"Saving modified epoch config".into(),
|
||
lib_efo::epoch_struct::SaveMode::Erase,
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
fn handle_next(args: cli_epoch::Next) -> Result<(), AppError> {
|
||
let _ = args;
|
||
println!("⚡ Next",);
|
||
unimplemented!("Missing logic to process a next event");
|
||
}
|
||
|
||
/// Reset all future events by deleting them
|
||
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
|
||
.list_existing_epochs
|
||
.keys()
|
||
.filter(|&k| k > ¤t_id)
|
||
{
|
||
main_epoch.delete_id(*id)?
|
||
}
|
||
|
||
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),
|
||
)))
|
||
}
|
||
}
|
||
|
||
/// Basic sanity check
|
||
fn handle_fsck(args: cli_epoch::Fsck, main_epoch: &mut EpochConfig) -> Result<(), AppError> {
|
||
let _ = main_epoch;
|
||
let _ = args;
|
||
unimplemented!()
|
||
}
|