1use crate::{
4 prelude::{fmt, Box, Cow, FromStr, Named, String, Vec},
5 BlockInstantiation, System,
6};
7use enum_iterator::Sequence;
8use protoflow_core::{types::Any, Block};
9
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Sequence)]
12pub enum BlockTag {
13 Buffer,
15 Const,
16 Count,
17 Delay,
18 Drop,
19 Random,
20 #[cfg(feature = "hash")]
23 Hash,
24 Decode,
26 Encode,
27 EncodeHex,
28 EncodeJson,
29 #[cfg(feature = "std")]
32 ReadDir,
33 #[cfg(feature = "std")]
34 ReadEnv,
35 #[cfg(feature = "std")]
36 ReadFile,
37 #[cfg(feature = "std")]
38 ReadStdin,
39 #[cfg(feature = "std")]
40 WriteFile,
41 #[cfg(feature = "std")]
42 WriteStderr,
43 #[cfg(feature = "std")]
44 WriteStdout,
45 }
47
48impl BlockTag {
49 pub fn count() -> usize {
50 enum_iterator::cardinality::<Self>()
51 }
52
53 pub fn all() -> Vec<Self> {
54 enum_iterator::all::<Self>().collect()
55 }
56
57 pub fn as_str(&self) -> &'static str {
58 use BlockTag::*;
59 match self {
60 Buffer => "Buffer",
61 Const => "Const",
62 Count => "Count",
63 Delay => "Delay",
64 Drop => "Drop",
65 Random => "Random",
66 #[cfg(feature = "hash")]
67 Hash => "Hash",
68 Decode => "Decode",
69 Encode => "Encode",
70 EncodeHex => "EncodeHex",
71 EncodeJson => "EncodeJSON",
72 #[cfg(feature = "std")]
73 ReadDir => "ReadDir",
74 #[cfg(feature = "std")]
75 ReadEnv => "ReadEnv",
76 #[cfg(feature = "std")]
77 ReadFile => "ReadFile",
78 #[cfg(feature = "std")]
79 ReadStdin => "ReadStdin",
80 #[cfg(feature = "std")]
81 WriteFile => "WriteFile",
82 #[cfg(feature = "std")]
83 WriteStderr => "WriteStderr",
84 #[cfg(feature = "std")]
85 WriteStdout => "WriteStdout",
86 }
87 }
88}
89
90impl FromStr for BlockTag {
91 type Err = ();
92
93 fn from_str(input: &str) -> Result<Self, Self::Err> {
94 use BlockTag::*;
95 Ok(match input {
96 "Buffer" => Buffer,
97 "Const" => Const,
98 "Count" => Count,
99 "Delay" => Delay,
100 "Drop" => Drop,
101 "Random" => Random,
102 #[cfg(feature = "hash")]
103 "Hash" => Hash,
104 "Decode" => Decode,
105 "Encode" => Encode,
106 "EncodeHex" => EncodeHex,
107 "EncodeJSON" => EncodeJson,
108 #[cfg(feature = "std")]
109 "ReadDir" => ReadDir,
110 #[cfg(feature = "std")]
111 "ReadEnv" => ReadEnv,
112 #[cfg(feature = "std")]
113 "ReadFile" => ReadFile,
114 #[cfg(feature = "std")]
115 "ReadStdin" => ReadStdin,
116 #[cfg(feature = "std")]
117 "WriteFile" => WriteFile,
118 #[cfg(feature = "std")]
119 "WriteStderr" => WriteStderr,
120 #[cfg(feature = "std")]
121 "WriteStdout" => WriteStdout,
122 _ => return Err(()),
123 })
124 }
125}
126
127impl Named for BlockTag {
128 fn name(&self) -> Cow<str> {
129 Cow::Borrowed(self.as_str())
130 }
131}
132
133impl fmt::Display for BlockTag {
134 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135 write!(f, "{}", self.name())
136 }
137}
138
139impl BlockInstantiation for BlockTag {
140 fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
141 use BlockTag::*;
142 match self {
143 Buffer => Box::new(super::Buffer::<Any>::with_system(system)),
144 Const => Box::new(super::Const::<String>::with_system(system, String::new())),
145 Count => Box::new(super::Count::<Any>::with_system(system)),
146 Delay => Box::new(super::Delay::<Any>::with_system(system, None)),
147 Drop => Box::new(super::Drop::<Any>::with_system(system)),
148 Random => Box::new(super::Random::<u64>::with_system(system, None)),
149 #[cfg(feature = "hash")]
150 Hash => Box::new(super::Hash::with_system(system, None)),
151 Decode => Box::new(super::Decode::<String>::with_system(system, None)),
152 Encode => Box::new(super::Encode::<String>::with_system(system, None)),
153 EncodeHex => Box::new(super::EncodeHex::with_system(system)),
154 EncodeJson => Box::new(super::EncodeJson::with_system(system)),
155 #[cfg(feature = "std")]
156 ReadDir => Box::new(super::ReadDir::with_system(system)),
157 #[cfg(feature = "std")]
158 ReadEnv => Box::new(super::ReadEnv::<String>::with_system(system)),
159 #[cfg(feature = "std")]
160 ReadFile => Box::new(super::ReadFile::with_system(system)),
161 #[cfg(feature = "std")]
162 ReadStdin => Box::new(super::ReadStdin::with_system(system, None)),
163 #[cfg(feature = "std")]
164 WriteFile => Box::new(super::WriteFile::with_system(system, None)),
165 #[cfg(feature = "std")]
166 WriteStderr => Box::new(super::WriteStderr::with_system(system)),
167 #[cfg(feature = "std")]
168 WriteStdout => Box::new(super::WriteStdout::with_system(system)),
169 }
170 }
171}