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
use scsys::prelude::{fnl_remove, StatePack};
use serde::{Deserialize, Serialize};
use std::convert::From;
use strum::{EnumString, EnumVariantNames};
#[derive(
Clone, Copy, Debug, Deserialize, EnumString, EnumVariantNames, Eq, Hash, PartialEq, Serialize,
)]
#[strum(serialize_all = "snake_case")]
pub enum CompilerStates {
Idle = 0,
Init = 1,
Read = 2,
Compile = 3,
Write = 4,
Complete = 5,
Invalid = 6,
}
impl CompilerStates {
pub fn idle() -> Self {
Self::Idle
}
pub fn init() -> Self {
Self::Init
}
pub fn invalid() -> Self {
Self::Invalid
}
pub fn read() -> Self {
Self::Read
}
pub fn write() -> Self {
Self::Write
}
pub fn compile() -> Self {
Self::Compile
}
pub fn complete() -> Self {
Self::Complete
}
}
impl StatePack for CompilerStates {}
impl std::fmt::Display for CompilerStates {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
fnl_remove(serde_json::to_string(self).unwrap()).to_ascii_lowercase()
)
}
}
impl Default for CompilerStates {
fn default() -> Self {
Self::Idle
}
}
impl From<i64> for CompilerStates {
fn from(data: i64) -> Self {
match data {
0 => Self::idle(),
1 => Self::init(),
2 => Self::read(),
3 => Self::compile(),
4 => Self::write(),
5 => Self::complete(),
_ => Self::invalid(),
}
}
}
impl From<CompilerStates> for i64 {
fn from(data: CompilerStates) -> Self {
match data {
CompilerStates::Idle => 0,
CompilerStates::Init => 1,
CompilerStates::Read => 2,
CompilerStates::Compile => 3,
CompilerStates::Write => 4,
CompilerStates::Complete => 5,
CompilerStates::Invalid => 6,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compiler_state() {
let a: i64 = CompilerStates::default().into();
assert_eq!(a, 0i64);
assert_eq!(
CompilerStates::try_from("idle").ok().unwrap(),
CompilerStates::from(a)
)
}
}