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

extern crate std;

use crate::{
    prelude::{vec, Bytes},
    types::ByteSize,
    StdioConfig, StdioError, StdioSystem, System,
};
use protoflow_core::{Block, BlockResult, BlockRuntime, OutputPort};
use protoflow_derive::Block;
use simple_mermaid::mermaid;
use std::io::Read;

/// The default buffer size for reading from standard input.
const DEFAULT_BUFFER_SIZE: ByteSize = ByteSize::new(1024);

/// A block that reads bytes from standard input (aka stdin).
///
/// # Block Diagram
#[doc = mermaid!("../../../doc/sys/read_stdin.mmd")]
///
/// # Sequence Diagram
#[doc = mermaid!("../../../doc/sys/read_stdin.seq.mmd" framed)]
///
/// # Examples
///
/// ## Using the block in a system
///
/// ```rust
/// # use protoflow_blocks::*;
/// # fn main() {
/// System::build(|s| {
///     let stdin = s.read_stdin();
///     let stdout = s.write_stdout();
///     s.connect(&stdin.output, &stdout.input);
/// });
/// # }
/// ```
///
/// ## Running the block via the CLI
///
/// ```console
/// $ protoflow execute ReadStdin < input.txt
/// ```
///
/// ```console
/// $ protoflow execute ReadStdin buffer-size=1024 < input.txt
/// ```
///
#[derive(Block, Clone)]
pub struct ReadStdin {
    /// The output message stream.
    #[output]
    pub output: OutputPort<Bytes>,

    /// The maximum number of bytes to read at a time.
    #[parameter]
    pub buffer_size: ByteSize,
}

impl ReadStdin {
    pub fn new(output: OutputPort<Bytes>) -> Self {
        Self::with_params(output, None)
    }

    pub fn with_params(output: OutputPort<Bytes>, buffer_size: Option<ByteSize>) -> Self {
        Self {
            output,
            buffer_size: buffer_size.unwrap_or(DEFAULT_BUFFER_SIZE),
        }
    }

    pub fn with_system(system: &System, buffer_size: Option<ByteSize>) -> Self {
        use crate::SystemBuilding;
        Self::with_params(system.output(), buffer_size)
    }
}

impl Block for ReadStdin {
    fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult {
        let stdin = std::io::stdin().lock();
        let mut reader = std::io::BufReader::new(stdin);
        let mut buffer = vec![0; self.buffer_size.into()];

        runtime.wait_for(&self.output)?;

        loop {
            buffer.resize(self.buffer_size.into(), b'\0'); // reinitialize the buffer
            buffer.fill(b'\0');

            match reader.read(&mut buffer) {
                Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                Err(err) => return Err(err.into()),
                Ok(0) => break, // EOF
                Ok(buffer_len) => {
                    buffer.resize(buffer_len, b'\0'); // truncate the buffer
                    let bytes = Bytes::from(buffer.clone());
                    self.output.send(&bytes)?;
                }
            }
        }

        Ok(())
    }
}

#[cfg(feature = "std")]
impl StdioSystem for ReadStdin {
    fn build_system(config: StdioConfig) -> Result<System, StdioError> {
        use crate::SystemBuilding;

        config.allow_only(vec!["buffer_size"])?;

        Ok(System::build(|s| {
            let stdin = config.read_stdin(s);
            let stdout = config.write_stdout(s);
            s.connect(&stdin.output, &stdout.input);
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::ReadStdin;
    use crate::{System, SystemBuilding};

    #[test]
    fn instantiate_block() {
        // Check that the block is constructible:
        let _ = System::build(|s| {
            let _ = s.block(ReadStdin::new(s.output()));
        });
    }
}