Structs for handling epoch

This commit is contained in:
Dandaedre 2025-11-15 22:24:39 +01:00
parent a7f3ace4b6
commit 5a537cd5c1
3 changed files with 52 additions and 8 deletions

View File

@ -4,7 +4,11 @@
//! Handles all epoch‑related logic – logging, switching, computing the
//! next epoch, and resetting future events.
use clap::{Args, Subcommand};
use std::str::FromStr;
use clap::{Args, Subcommand, error::ErrorKind};
use lib_efo::epoch_struct::EpochId;
use serde::{Deserialize, Serialize};
// use crate::storage::JsonStore;
// use crate::storage::StoreTrait; // abstraction for persistence
@ -39,10 +43,10 @@ pub struct Log {
pub struct Switch {
/// The epoch to switch to – can be an integer, a name, or a tag.
#[arg(required = true)]
pub target: String,
pub target: EpochIdentifier,
/// Optional confirmation flag (if you ever want to force‑switch)
#[arg(long, short, help = "Force‑switch even if the epoch has no tag")]
#[arg(long, short, help = "Force‑switch dropping all the pending work")]
pub force: bool,
}
@ -54,6 +58,45 @@ pub struct Next;
#[derive(Args, Debug)]
pub struct Reset;
/// A flexible identifier for *any* epoch – the world may be
/// addressed by a numeric ID or by a friendly tag (e.g. `"winter‑b"`).
///
/// The enum is `untagged` so that serialising/deserialising does
/// **not** add a wrapper field, e.g. `{"Tag":"foo"}`.
/// Instead the JSON representation is exactly the inner value:
/// * `"42"` → `EpochIdentifier::Id(EpochId::new(42))`
/// * `"foo"` → `EpochIdentifier::Tag("foo".into())`
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
#[serde(untagged)] // <‑‑ no “type” wrapper is written to JSON
pub enum EpochIdentifier {
/// A textual tag that maps to a numeric epoch ID in `EpochConfig::tags`.
Tag(String),
/// The raw numeric identifier of the epoch.
Id(EpochId),
}
impl FromStr for EpochIdentifier {
type Err = clap::Error; // any `std::error::Error` works
/// Try to parse either a decimal number (-> `Id`) or any non‑empty
/// string (-> `Tag`).
///
/// Errors are converted to a Clap error so that the CLI shows a
/// nice message.
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(num) = s.parse::<u64>() {
Ok(EpochIdentifier::Id(EpochId::new(num)))
} else if !s.is_empty() {
Ok(EpochIdentifier::Tag(s.to_owned()))
} else {
Err(clap::Error::raw(
ErrorKind::InvalidValue,
format!("{} is not a valid epoch id or tag", s),
))
}
}
}
// /// **Public entry point used by `efo::main`** – dispatches to the real
// /// handler that lives in `crate::world::EpochManager`.
// pub fn handle_cmd(cmd: EpochCmd, world: &mut World) -> anyhow::Result<()> {
@ -126,8 +169,6 @@ pub struct Reset;
// }
//
//
// use anyhow::Result;
// use crate::world::World;
// pub fn handle_cmd(cmd: EpochCmd, world: &mut World) -> Result<()> {
// match cmd {

View File

@ -74,8 +74,10 @@ pub struct EpochInfo {
}
/// Thin type to handle EpochIds
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EpochId {
/// The underlying numeric value. A value of `0` is never used –
/// epoch IDs start at 1.
pub id: u64,
}
@ -86,6 +88,7 @@ impl std::fmt::Display for EpochId {
}
impl EpochId {
/// Creates a new `EpochId` from a numeric value.
pub fn new(id: u64) -> Self {
Self { id }
}

View File

@ -19,7 +19,9 @@
//! `World::load()` / `World::save()` API that uses the default JSON file
//! (`world.json` in the current directory).
pub mod epoch_struct;
pub mod error;
// use async_trait::async_trait;
use std::io::Result as IoResult;
@ -69,5 +71,3 @@ where
// }
//
//
mod epoch_struct;