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