[TSM.ID].[11031972] PXE : Platform X Ecosystem I [118 Module -LIVE-]

This commit is contained in:
TSM.ID
2026-05-25 03:51:34 +07:00
parent e820143b3c
commit 8f1a37129a
354 changed files with 0 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
[package]
name = "xcu-state-machine"
version = "0.1.0"
edition = "2021"
authors = ["TSM.ID <tsm@tsm.id>"]
description = "[TSM.ID].[11031972] xcu-state-machine"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+104
View File
@@ -0,0 +1,104 @@
//! [TSM.ID].[11031972] — Platform X Ecosystem
//! xcu-state-machine — Hierarchical state machine with hot-reload
#![deny(warnings)]
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub enum XcuError {
InitFailed(String),
InvalidConfig(String),
OperationFailed(String),
ResourceExhausted,
NotFound(String),
Timeout,
}
impl std::fmt::Display for XcuError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InitFailed(e) => write!(f, "Init failed: {e}"),
Self::InvalidConfig(e) => write!(f, "Invalid config: {e}"),
Self::OperationFailed(e) => write!(f, "Operation failed: {e}"),
Self::ResourceExhausted => write!(f, "Resource exhausted"),
Self::NotFound(e) => write!(f, "Not found: {e}"),
Self::Timeout => write!(f, "Operation timed out"),
}
}
}
impl std::error::Error for XcuError {}
pub type Result<T> = std::result::Result<T, XcuError>;
pub struct Config {
pub params: HashMap<String, String>,
}
impl Config {
pub fn new() -> Self { Self { params: HashMap::new() } }
pub fn set(&mut self, key: &str, val: &str) -> &mut Self {
self.params.insert(key.to_string(), val.to_string()); self
}
pub fn get(&self, key: &str) -> Result<&str> {
self.params.get(key).map(|s| s.as_str()).ok_or_else(|| XcuError::NotFound(key.to_string()))
}
}
impl Default for Config {
fn default() -> Self { Self::new() }
}
pub struct Engine {
config: Config,
state: Arc<Mutex<EngineState>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum EngineState {
Idle,
Running,
Paused,
ShuttingDown,
Stopped,
}
impl Engine {
pub fn new(config: Config) -> Result<Self> {
Ok(Self { config, state: Arc::new(Mutex::new(EngineState::Idle)) })
}
pub fn start(&self) -> Result<()> {
let mut s = self.state.lock().map_err(|e| XcuError::OperationFailed(e.to_string()))?;
*s = EngineState::Running;
Ok(())
}
pub fn stop(&self) -> Result<()> {
let mut s = self.state.lock().map_err(|e| XcuError::OperationFailed(e.to_string()))?;
*s = EngineState::ShuttingDown;
// graceful shutdown logic
*s = EngineState::Stopped;
Ok(())
}
pub fn state(&self) -> Result<EngineState> {
let s = self.state.lock().map_err(|e| XcuError::OperationFailed(e.to_string()))?;
Ok(s.clone())
}
pub fn config(&self) -> &Config { &self.config }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_engine_lifecycle() {
let engine = Engine::new(Config::new()).unwrap();
assert_eq!(engine.state().unwrap(), EngineState::Idle);
engine.start().unwrap();
assert_eq!(engine.state().unwrap(), EngineState::Running);
engine.stop().unwrap();
assert_eq!(engine.state().unwrap(), EngineState::Stopped);
}
}