dimas_core/enums/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright © 2024 Stephan Kunz

//! Core enums of `DiMAS`
//!

#[doc(hidden)]
extern crate alloc;

// region:		--- modules
use crate::error::Error;
use alloc::vec::Vec;
use alloc::{
	boxed::Box,
	string::{String, ToString},
};
use bitcode::{Decode, Encode};
use core::fmt::{Debug, Display};
// endregion:	--- modules

// region:		--- OperationState
/// The possible states a `DiMAS` entity can take
#[derive(Debug, Decode, Encode, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
pub enum OperationState {
	/// Entity is in an erronous state
	Error,
	/// Entity is in initial state
	#[default]
	Created,
	/// Entity is setup properly
	Configured,
	/// Entity is listening to important messages only
	Inactive,
	/// Entity has full situational awareness but does
	Standby,
	/// Entity is fully operational
	Active,
}

impl TryFrom<&str> for OperationState {
	type Error = Box<dyn core::error::Error + Send + Sync + 'static>;

	fn try_from(
		value: &str,
	) -> core::result::Result<Self, Box<dyn core::error::Error + Send + Sync + 'static>> {
		let v = value.to_lowercase();
		match v.as_str() {
			"created" => Ok(Self::Created),
			"configured" => Ok(Self::Configured),
			"inactive" => Ok(Self::Inactive),
			"standby" => Ok(Self::Standby),
			"active" => Ok(Self::Active),
			_ => Err(Error::UnknownOperationState {
				state: value.to_string(),
			}
			.into()),
		}
	}
}

impl Display for OperationState {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			Self::Error => write!(f, "Error"),
			Self::Created => write!(f, "Created"),
			Self::Configured => write!(f, "Configured"),
			Self::Inactive => write!(f, "Inactive"),
			Self::Standby => write!(f, "Standby"),
			Self::Active => write!(f, "Active"),
		}
	}
}
// endregion:	--- OperationState

// region:		--- Signal
/// All defined commands of `DiMAS`
#[derive(Debug, Decode, Encode)]
pub enum Signal {
	/// About
	About,
	/// respond to Ping
	Ping {
		/// the utc time coordinate when the request was sent
		sent: i64,
	},
	/// Shutdown application
	Shutdown,
	/// State
	State {
		/// Optional `OperationState` to set
		state: Option<OperationState>,
	},
}
// endregion:	--- Signal

// region:		--- TaskSignal
/// Internal signals, used by panic hooks to inform that someting has happened.
#[derive(Debug, Clone)]
pub enum TaskSignal {
	/// Restart a certain liveliness subscriber, identified by its key expression
	#[cfg(feature = "unstable")]
	RestartLiveliness(String),
	/// Restart a certain observable, identified by its key expression
	RestartObservable(String),
	/// Restart a certain queryable, identified by its key expression
	RestartQueryable(String),
	/// Restart a certain lsubscriber, identified by its key expression
	RestartSubscriber(String),
	/// Restart a certain timer, identified by its key expression
	RestartTimer(String),
	/// Shutdown whole process
	Shutdown,
}
// endregion:	--- TaskSignal