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

#[cfg(not(feature = "std"))]
pub mod sys {
    pub trait SysBlocks {}
    pub enum SysBlockConfig {}
}

#[cfg(feature = "std")]
pub mod sys {
    use super::{
        prelude::{vec, Box, Cow, Named, String, Vec},
        types::ByteSize,
        BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
    };
    use protoflow_core::Block;

    pub trait SysBlocks {
        fn read_dir(&mut self) -> ReadDir;
        fn read_env(&mut self) -> ReadEnv;
        fn read_file(&mut self) -> ReadFile;
        fn read_stdin(&mut self) -> ReadStdin;
        fn write_file(&mut self) -> WriteFile;
        fn write_stderr(&mut self) -> WriteStderr;
        fn write_stdout(&mut self) -> WriteStdout;
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub enum SysBlockTag {
        ReadDir,
        ReadEnv,
        ReadFile,
        ReadStdin,
        WriteFile,
        WriteStderr,
        WriteStdout,
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Debug)]
    pub enum SysBlockConfig {
        ReadDir {
            path: InputPortName,
            output: OutputPortName,
        },

        ReadEnv {
            name: InputPortName,
            output: OutputPortName,
        },

        ReadFile {
            path: InputPortName,
            output: OutputPortName,
        },

        ReadStdin {
            output: OutputPortName,
            buffer_size: Option<ByteSize>,
        },

        WriteFile {
            path: InputPortName,
            input: InputPortName,
        },

        WriteStderr {
            input: InputPortName,
        },

        WriteStdout {
            input: InputPortName,
        },
    }

    impl Named for SysBlockConfig {
        fn name(&self) -> Cow<str> {
            use SysBlockConfig::*;
            Cow::Borrowed(match self {
                ReadDir { .. } => "ReadDir",
                ReadEnv { .. } => "ReadEnv",
                ReadFile { .. } => "ReadFile",
                ReadStdin { .. } => "ReadStdin",
                WriteFile { .. } => "WriteFile",
                WriteStderr { .. } => "WriteStderr",
                WriteStdout { .. } => "WriteStdout",
            })
        }
    }

    impl BlockConnections for SysBlockConfig {
        fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
            use SysBlockConfig::*;
            match self {
                ReadDir { output, .. }
                | ReadEnv { output, .. }
                | ReadFile { output, .. }
                | ReadStdin { output, .. } => {
                    vec![("output", Some(output.clone()))]
                }
                WriteFile { .. } | WriteStderr { .. } | WriteStdout { .. } => vec![],
            }
        }
    }

    impl BlockInstantiation for SysBlockConfig {
        fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
            use SysBlockConfig::*;
            match self {
                ReadDir { .. } => Box::new(super::ReadDir::with_system(system)),
                ReadEnv { .. } => Box::new(super::ReadEnv::<String>::with_system(system)),
                ReadFile { .. } => Box::new(super::ReadFile::with_system(system)),
                ReadStdin { buffer_size, .. } => {
                    Box::new(super::ReadStdin::with_system(system, *buffer_size))
                }
                WriteFile { .. } => Box::new(super::WriteFile::with_system(system)),
                WriteStderr { .. } => Box::new(super::WriteStderr::with_system(system)),
                WriteStdout { .. } => Box::new(super::WriteStdout::with_system(system)),
            }
        }
    }

    mod read_dir;
    pub use read_dir::*;

    mod read_env;
    pub use read_env::*;

    mod read_file;
    pub use read_file::*;

    mod read_stdin;
    pub use read_stdin::*;

    mod write_file;
    pub use write_file::*;

    mod write_stderr;
    pub use write_stderr::*;

    mod write_stdout;
    pub use write_stdout::*;
}

pub use sys::*;