Epoch first draft finished
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 17:31:34 +01:00
parent fcd55b6e34
commit 4cd3d4de98
2 changed files with 60 additions and 47 deletions

View File

@ -3,10 +3,7 @@
//! 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 lib_efo::epoch_struct::{ConfigWritable, EpochConfig};
use crate::error::AppError;
@ -20,54 +17,54 @@ pub fn handle_cmd(cmd: cli_epoch::EpochCmd) -> Result<(), AppError> {
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(_) => handle_next(),
cli_epoch::EpochCmd::Reset(_) => handle_reset(),
cli_epoch::EpochCmd::Diff(_) => handle_diff(),
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),
}
}
fn handle_diff() -> Result<(), AppError> {
// Blocker: There is actually no way to diff the world yet
todo!()
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> {
// Just needs for the epoch to implement lvels of displays trait: DisplayLevels
println!("Epoch log (most recent first):");
// println!(
// "#{} {} [{}] {}",
// epoch.id, epoch.date, tags, epoch.summary
// );
Ok(())
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)?;
// TODO: Check if the epoch is valid
// let b = main_epoch
// .list_existing_epochs
// .get(&target.to_string())
// .unwrap();
main_epoch
.try_load(&target)
.map_err(|_e| EpochError::EpochFolderMissing(target, main_epoch.save_folder().into()))?;
main_epoch.try_load(&target)?;
main_epoch.epoch_id = target;
println!("Switched to epoch {:?}", target);
main_epoch.save(
"Saving modified config".into(),
"Saving modified epoch config".into(),
lib_efo::epoch_struct::SaveMode::Erase,
)?;
Ok(())
}
fn handle_next() -> Result<(), AppError> {
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() -> Result<(), AppError> {
println!("✅ All future events deleted – back to epoch",);
unimplemented!("Blocked by non parsing of folders");
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 > &current_id)
{
main_epoch.delete_id(*id)?
}
println!("All future events deleted – back to epoch {current_id}",);
Ok(())
}

View File

@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt,
fs::{File, create_dir_all, read_to_string},
fs::{File, create_dir_all, read_to_string, remove_dir_all},
io::Write,
path::Path,
str::FromStr,
@ -121,31 +121,24 @@ impl EpochConfig {
}
/// Try to load into memory the given epoch id
// TODO: CLEANSE THIS
// TODO: CLEANSE THIS
// TODO: CLEANSE THIS
pub fn try_load(&mut self, epoch_id: &EpochId) -> Result<(), EpochError> {
// TODO: Cleanse this abomination
let _ep = self
.list_existing_epochs
.contains_key(epoch_id)
.then_some(epoch_id)
.ok_or(EpochError::EpochFolderMissing(
*epoch_id,
EpochConfig::default().save_path().into(),
))
.copied()?;
let path = Path::new(&epoch_id.to_string()).join(".epochinfo");
// Preload the epoch
let value = self.list_existing_epochs.get_mut(epoch_id).unwrap();
// Create a mock epoch
let path = EpochInfo::with_id(epoch_id).save_path();
let read_to_string =
io_err_with_help!(read_to_string(&path), format!("Reading {:?}", path));
// Try to load
let maybe_epoch: EpochInfo =
serde_json::from_str(&read_to_string?).map_err(|e| EpochError::Serde {
source: e,
file_name: path.to_string_lossy().to_string(),
file_name: path.to_string(),
help: None,
})?;
let value = self.list_existing_epochs.get_mut(epoch_id).unwrap();
// Overwrite the value
*value = Some(maybe_epoch);
Ok(())
}
@ -173,6 +166,21 @@ impl EpochConfig {
fn save_subfolder(&self) -> String {
format!("{}/", SUBFOLDER_NAMING)
}
/// Tries to delete an entire epoch folder by Id
pub fn delete_id(&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",
id
)
}
io_err_with_help!(
remove_dir_all(EpochInfo::with_id(&id).save_folder()),
format!("Removing the epoch {}", id)
)?;
Ok(())
}
}
/// Trait representing a serialisable / writable config
@ -262,6 +270,14 @@ impl EpochInfo {
pub fn create_subfolders(&self) -> Result<(), EpochError> {
io_err_with_help!(create_dir_all(self.save_folder()), "Creating subfolders")
}
/// Create an epoch with only an Id, please note that this function doesn't load the epoch at all
fn with_id(epoch_id: &EpochId) -> Self {
Self {
epoch_id: *epoch_id,
..Default::default()
}
}
}
/// Thin type to handle EpochIds