From 466ad9f98c65e087c34c6ff2c740fd00130cf54b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 15 Nov 2025 22:24:39 +0100 Subject: [PATCH] Structs for handling epoch --- bin_efo/src/commands/epoch.rs | 51 +++++++++++++++++++++++++++++++---- lib_efo/src/epoch_struct.rs | 5 +++- lib_efo/src/lib.rs | 4 +-- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/bin_efo/src/commands/epoch.rs b/bin_efo/src/commands/epoch.rs index a88d511..b3aa6da 100644 --- a/bin_efo/src/commands/epoch.rs +++ b/bin_efo/src/commands/epoch.rs @@ -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 { + if let Ok(num) = s.parse::() { + 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 { diff --git a/lib_efo/src/epoch_struct.rs b/lib_efo/src/epoch_struct.rs index fa12405..0f99137 100644 --- a/lib_efo/src/epoch_struct.rs +++ b/lib_efo/src/epoch_struct.rs @@ -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 } } diff --git a/lib_efo/src/lib.rs b/lib_efo/src/lib.rs index 802ce5f..e689d5a 100644 --- a/lib_efo/src/lib.rs +++ b/lib_efo/src/lib.rs @@ -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;