protoflow_blocks/blocks/
io.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
// This is free and unencumbered software released into the public domain.

pub mod io {
    use super::{
        prelude::{vec, Box, Cow, Named, String, Vec},
        BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
    };
    use crate::{
        prelude::{FromStr, ToString},
        types::Encoding,
    };
    use protoflow_core::{Block, Message};

    pub trait IoBlocks {
        fn decode<T: Message + FromStr + 'static>(&mut self) -> Decode<T>;
        fn decode_with<T: Message + FromStr + 'static>(&mut self, encoding: Encoding) -> Decode<T>;

        fn decode_lines<T: Message + FromStr + 'static>(&mut self) -> Decode<T> {
            self.decode_with::<T>(Encoding::TextWithNewlineSuffix)
        }

        fn encode<T: Message + ToString + 'static>(&mut self) -> Encode<T>;
        fn encode_with<T: Message + ToString + 'static>(&mut self, encoding: Encoding)
            -> Encode<T>;

        fn encode_lines<T: Message + ToString + 'static>(&mut self) -> Encode<T> {
            self.encode_with::<T>(Encoding::TextWithNewlineSuffix)
        }

        fn encode_hex(&mut self) -> EncodeHex;
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub enum IoBlockTag {
        Decode,
        Encode,
        EncodeHex,
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Debug)]
    pub enum IoBlockConfig {
        Decode {
            input: InputPortName,
            output: OutputPortName,
            encoding: Option<Encoding>,
        },

        Encode {
            input: InputPortName,
            output: OutputPortName,
            encoding: Option<Encoding>,
        },

        EncodeHex {
            input: InputPortName,
            output: OutputPortName,
        },
    }

    impl Named for IoBlockConfig {
        fn name(&self) -> Cow<str> {
            use IoBlockConfig::*;
            Cow::Borrowed(match self {
                Decode { .. } => "Decode",
                Encode { .. } => "Encode",
                EncodeHex { .. } => "EncodeHex",
            })
        }
    }

    impl BlockConnections for IoBlockConfig {
        fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
            use IoBlockConfig::*;
            match self {
                Decode { output, .. } | Encode { output, .. } | EncodeHex { output, .. } => {
                    vec![("output", Some(output.clone()))]
                }
            }
        }
    }

    impl BlockInstantiation for IoBlockConfig {
        fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
            use IoBlockConfig::*;
            match self {
                Decode { encoding, .. } => {
                    Box::new(super::Decode::<String>::with_system(system, *encoding))
                }
                Encode { encoding, .. } => {
                    Box::new(super::Encode::<String>::with_system(system, *encoding))
                }
                EncodeHex { .. } => Box::new(super::EncodeHex::with_system(system)),
            }
        }
    }

    mod decode;
    pub use decode::*;

    mod encode;
    pub use encode::*;

    mod encode_hex;
    pub use encode_hex::*;
}

pub use io::*;