dimas_core/enums/
mod.rs

1// Copyright © 2024 Stephan Kunz
2
3//! Core enums of `DiMAS`
4//!
5
6#[doc(hidden)]
7extern crate alloc;
8
9// region:		--- modules
10use crate::error::Error;
11use alloc::{
12	boxed::Box,
13	string::{String, ToString},
14};
15use bitcode::{Decode, Encode};
16use core::fmt::{Debug, Display};
17// endregion:	--- modules
18
19// region:		--- OperationState
20/// The possible states a `DiMAS` entity can take
21#[derive(Debug, Decode, Encode, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
22pub enum OperationState {
23	/// Entity is in an erronous state
24	Error,
25	/// Entity is in initial state
26	#[default]
27	Created,
28	/// Entity is setup properly
29	Configured,
30	/// Entity is listening to important messages only
31	Inactive,
32	/// Entity has full situational awareness but does not act
33	Standby,
34	/// Entity is fully operational
35	Active,
36}
37
38impl TryFrom<&str> for OperationState {
39	type Error = Box<dyn core::error::Error + Send + Sync + 'static>;
40
41	fn try_from(
42		value: &str,
43	) -> core::result::Result<Self, Box<dyn core::error::Error + Send + Sync + 'static>> {
44		let v = value.to_lowercase();
45		match v.as_str() {
46			"created" => Ok(Self::Created),
47			"configured" => Ok(Self::Configured),
48			"inactive" => Ok(Self::Inactive),
49			"standby" => Ok(Self::Standby),
50			"active" => Ok(Self::Active),
51			_ => Err(Error::UnknownOperationState {
52				state: value.to_string(),
53			}
54			.into()),
55		}
56	}
57}
58
59impl Display for OperationState {
60	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
61		match self {
62			Self::Error => write!(f, "Error"),
63			Self::Created => write!(f, "Created"),
64			Self::Configured => write!(f, "Configured"),
65			Self::Inactive => write!(f, "Inactive"),
66			Self::Standby => write!(f, "Standby"),
67			Self::Active => write!(f, "Active"),
68		}
69	}
70}
71// endregion:	--- OperationState
72
73// region:		--- Signal
74/// All defined commands of `DiMAS`
75#[derive(Debug, Decode, Encode)]
76pub enum Signal {
77	/// About
78	About,
79	/// respond to Ping
80	Ping {
81		/// the utc time coordinate when the request was sent
82		sent: i64,
83	},
84	/// Shutdown application
85	Shutdown,
86	/// State
87	State {
88		/// Optional `OperationState` to set
89		state: Option<OperationState>,
90	},
91}
92// endregion:	--- Signal
93
94// region:		--- TaskSignal
95/// Internal signals, used by panic hooks to inform that someting has happened.
96#[derive(Debug, Clone)]
97pub enum TaskSignal {
98	/// Restart a certain liveliness subscriber, identified by its key expression
99	RestartLiveliness(String),
100	/// Restart a certain observable, identified by its key expression
101	RestartObservable(String),
102	/// Restart a certain queryable, identified by its key expression
103	RestartQueryable(String),
104	/// Restart a certain lsubscriber, identified by its key expression
105	RestartSubscriber(String),
106	/// Restart a certain timer, identified by its key expression
107	RestartTimer(String),
108	/// Shutdown whole process
109	Shutdown,
110}
111// endregion:	--- TaskSignal