protoflow_blocks/blocks/
io.rs

1// This is free and unencumbered software released into the public domain.
2
3pub mod io {
4    use super::{
5        prelude::{vec, Box, Cow, Named, String, Vec},
6        BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
7    };
8    use crate::{
9        prelude::{FromStr, ToString},
10        types::Encoding,
11    };
12    use protoflow_core::{Block, Message};
13
14    pub trait IoBlocks {
15        fn decode<T: Message + FromStr + 'static>(&mut self) -> Decode<T>;
16        fn decode_json(&mut self) -> DecodeJson;
17        fn decode_with<T: Message + FromStr + 'static>(&mut self, encoding: Encoding) -> Decode<T>;
18
19        fn decode_lines<T: Message + FromStr + 'static>(&mut self) -> Decode<T> {
20            self.decode_with::<T>(Encoding::TextWithNewlineSuffix)
21        }
22
23        fn encode<T: Message + ToString + 'static>(&mut self) -> Encode<T>;
24        fn encode_with<T: Message + ToString + 'static>(&mut self, encoding: Encoding)
25            -> Encode<T>;
26
27        fn encode_lines<T: Message + ToString + 'static>(&mut self) -> Encode<T> {
28            self.encode_with::<T>(Encoding::TextWithNewlineSuffix)
29        }
30
31        fn encode_hex(&mut self) -> EncodeHex;
32
33        fn encode_json(&mut self) -> EncodeJson;
34    }
35
36    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
38    pub enum IoBlockTag {
39        Decode,
40        Encode,
41        EncodeHex,
42        DecodeJson,
43        EncodeJson,
44    }
45
46    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47    #[derive(Clone, Debug)]
48    pub enum IoBlockConfig {
49        Decode {
50            input: InputPortName,
51            output: OutputPortName,
52            encoding: Option<Encoding>,
53        },
54
55        #[cfg_attr(feature = "serde", serde(rename = "DecodeJSON"))]
56        DecodeJson {
57            input: InputPortName,
58            output: OutputPortName,
59        },
60
61        Encode {
62            input: InputPortName,
63            output: OutputPortName,
64            encoding: Option<Encoding>,
65        },
66
67        EncodeHex {
68            input: InputPortName,
69            output: OutputPortName,
70        },
71
72        #[cfg_attr(feature = "serde", serde(rename = "EncodeJSON"))]
73        EncodeJson {
74            input: InputPortName,
75            output: OutputPortName,
76        },
77    }
78
79    impl Named for IoBlockConfig {
80        fn name(&self) -> Cow<str> {
81            use IoBlockConfig::*;
82            Cow::Borrowed(match self {
83                Decode { .. } => "Decode",
84                DecodeJson { .. } => "DecodeJSON",
85                Encode { .. } => "Encode",
86                EncodeHex { .. } => "EncodeHex",
87                EncodeJson { .. } => "EncodeJSON",
88            })
89        }
90    }
91
92    impl BlockConnections for IoBlockConfig {
93        fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
94            use IoBlockConfig::*;
95            match self {
96                Decode { output, .. }
97                | DecodeJson { output, .. }
98                | Encode { output, .. }
99                | EncodeHex { output, .. }
100                | EncodeJson { output, .. } => {
101                    vec![("output", Some(output.clone()))]
102                }
103            }
104        }
105    }
106
107    impl BlockInstantiation for IoBlockConfig {
108        fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
109            use IoBlockConfig::*;
110            match self {
111                Decode { encoding, .. } => {
112                    Box::new(super::Decode::<String>::with_system(system, *encoding))
113                }
114                DecodeJson { .. } => Box::new(super::DecodeJson::with_system(system)),
115                Encode { encoding, .. } => {
116                    Box::new(super::Encode::<String>::with_system(system, *encoding))
117                }
118                EncodeHex { .. } => Box::new(super::EncodeHex::with_system(system)),
119                EncodeJson { .. } => Box::new(super::EncodeJson::with_system(system)),
120            }
121        }
122    }
123
124    mod decode;
125    pub use decode::*;
126
127    mod decode_json;
128    pub use decode_json::*;
129
130    mod encode;
131    pub use encode::*;
132
133    mod encode_hex;
134    pub use encode_hex::*;
135
136    mod encode_json;
137    pub use encode_json::*;
138}
139
140pub use io::*;