protoflow_blocks/
block_tag.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// This is free and unencumbered software released into the public domain.

use crate::{
    prelude::{fmt, Box, Cow, FromStr, Named, String, Vec},
    BlockInstantiation, System,
};
use enum_iterator::Sequence;
use protoflow_core::{types::Any, Block};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Sequence)]
pub enum BlockTag {
    // CoreBlocks
    Buffer,
    Const,
    Count,
    Delay,
    Drop,
    Random,
    // FlowBlocks
    // HashBlocks
    #[cfg(feature = "hash")]
    Hash,
    // IoBlocks
    Decode,
    Encode,
    EncodeHex,
    // MathBlocks
    // SysBlocks
    #[cfg(feature = "std")]
    ReadDir,
    #[cfg(feature = "std")]
    ReadEnv,
    #[cfg(feature = "std")]
    ReadFile,
    #[cfg(feature = "std")]
    ReadStdin,
    #[cfg(feature = "std")]
    WriteFile,
    #[cfg(feature = "std")]
    WriteStderr,
    #[cfg(feature = "std")]
    WriteStdout,
    // TextBlocks
}

impl BlockTag {
    pub fn count() -> usize {
        enum_iterator::cardinality::<Self>()
    }

    pub fn all() -> Vec<Self> {
        enum_iterator::all::<Self>().collect()
    }

    pub fn as_str(&self) -> &'static str {
        use BlockTag::*;
        match self {
            Buffer => "Buffer",
            Const => "Const",
            Count => "Count",
            Delay => "Delay",
            Drop => "Drop",
            Random => "Random",
            #[cfg(feature = "hash")]
            Hash => "Hash",
            Decode => "Decode",
            Encode => "Encode",
            EncodeHex => "EncodeHex",
            #[cfg(feature = "std")]
            ReadDir => "ReadDir",
            #[cfg(feature = "std")]
            ReadEnv => "ReadEnv",
            #[cfg(feature = "std")]
            ReadFile => "ReadFile",
            #[cfg(feature = "std")]
            ReadStdin => "ReadStdin",
            #[cfg(feature = "std")]
            WriteFile => "WriteFile",
            #[cfg(feature = "std")]
            WriteStderr => "WriteStderr",
            #[cfg(feature = "std")]
            WriteStdout => "WriteStdout",
        }
    }
}

impl FromStr for BlockTag {
    type Err = ();

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        use BlockTag::*;
        Ok(match input {
            "Buffer" => Buffer,
            "Const" => Const,
            "Count" => Count,
            "Delay" => Delay,
            "Drop" => Drop,
            "Random" => Random,
            #[cfg(feature = "hash")]
            "Hash" => Hash,
            "Decode" => Decode,
            "Encode" => Encode,
            "EncodeHex" => EncodeHex,
            #[cfg(feature = "std")]
            "ReadDir" => ReadDir,
            #[cfg(feature = "std")]
            "ReadEnv" => ReadEnv,
            #[cfg(feature = "std")]
            "ReadFile" => ReadFile,
            #[cfg(feature = "std")]
            "ReadStdin" => ReadStdin,
            #[cfg(feature = "std")]
            "WriteFile" => WriteFile,
            #[cfg(feature = "std")]
            "WriteStderr" => WriteStderr,
            #[cfg(feature = "std")]
            "WriteStdout" => WriteStdout,
            _ => return Err(()),
        })
    }
}

impl Named for BlockTag {
    fn name(&self) -> Cow<str> {
        Cow::Borrowed(self.as_str())
    }
}

impl fmt::Display for BlockTag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl BlockInstantiation for BlockTag {
    fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
        use BlockTag::*;
        match self {
            Buffer => Box::new(super::Buffer::<Any>::with_system(system)),
            Const => Box::new(super::Const::<String>::with_system(system, String::new())),
            Count => Box::new(super::Count::<Any>::with_system(system)),
            Delay => Box::new(super::Delay::<Any>::with_system(system, None)),
            Drop => Box::new(super::Drop::<Any>::with_system(system)),
            Random => Box::new(super::Random::<u64>::with_system(system, None)),
            #[cfg(feature = "hash")]
            Hash => Box::new(super::Hash::with_system(system, None)),
            Decode => Box::new(super::Decode::<String>::with_system(system, None)),
            Encode => Box::new(super::Encode::<String>::with_system(system, None)),
            EncodeHex => Box::new(super::EncodeHex::with_system(system)),
            #[cfg(feature = "std")]
            ReadDir => Box::new(super::ReadDir::with_system(system)),
            #[cfg(feature = "std")]
            ReadEnv => Box::new(super::ReadEnv::<String>::with_system(system)),
            #[cfg(feature = "std")]
            ReadFile => Box::new(super::ReadFile::with_system(system)),
            #[cfg(feature = "std")]
            ReadStdin => Box::new(super::ReadStdin::with_system(system, None)),
            #[cfg(feature = "std")]
            WriteFile => Box::new(super::WriteFile::with_system(system)),
            #[cfg(feature = "std")]
            WriteStderr => Box::new(super::WriteStderr::with_system(system)),
            #[cfg(feature = "std")]
            WriteStdout => Box::new(super::WriteStdout::with_system(system)),
        }
    }
}