Skip to main content

statum_core/
lib.rs

1//! Core error and result types shared by Statum crates.
2
3/// A generated state marker type.
4pub trait StateMarker {
5    /// The payload type stored in machines for this state.
6    type Data;
7}
8
9/// A generated state marker with no payload.
10pub trait UnitState: StateMarker<Data = ()> {}
11
12/// A generated state marker that carries payload data.
13pub trait DataState: StateMarker {}
14
15/// A machine that can transition directly to `Next`.
16pub trait CanTransitionTo<Next> {
17    /// The transition result type.
18    type Output;
19
20    /// Perform the transition.
21    fn transition_to(self) -> Self::Output;
22}
23
24/// A machine that can transition using `Data`.
25pub trait CanTransitionWith<Data> {
26    /// The next state selected by this transition.
27    type NextState;
28    /// The transition result type.
29    type Output;
30
31    /// Perform the transition with payload data.
32    fn transition_with_data(self, data: Data) -> Self::Output;
33}
34
35/// Errors returned by Statum runtime helpers.
36#[derive(Debug)]
37pub enum Error {
38    /// Returned when a runtime check determines the current state is invalid.
39    InvalidState,
40}
41
42/// Convenience result alias used by Statum APIs.
43///
44/// # Example
45///
46/// ```
47/// fn ensure_ready(ready: bool) -> statum_core::Result<()> {
48///     if ready {
49///         Ok(())
50///     } else {
51///         Err(statum_core::Error::InvalidState)
52///     }
53/// }
54///
55/// assert!(ensure_ready(true).is_ok());
56/// assert!(ensure_ready(false).is_err());
57/// ```
58pub type Result<T> = core::result::Result<T, Error>;
59
60impl<T> From<Error> for core::result::Result<T, Error> {
61    fn from(val: Error) -> Self {
62        Err(val)
63    }
64}
65
66impl core::fmt::Display for Error {
67    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
68        write!(fmt, "{self:?}")
69    }
70}
71
72impl std::error::Error for Error {}