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

use crate::{IoBlocks, StdioConfig, StdioError, StdioSystem, System};
use protoflow_core::{
    prelude::{format, Bytes, String},
    Block, BlockResult, BlockRuntime, InputPort, OutputPort,
};
use protoflow_derive::Block;
use simple_mermaid::mermaid;

/// A block that encodes a byte stream into hexadecimal form.
///
/// # Block Diagram
#[doc = mermaid!("../../doc/io/encode_hex.mmd")]
///
/// # Sequence Diagram
#[doc = mermaid!("../../doc/io/encode_hex.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 hex_encoder = s.encode_hex();
///     let stdout = s.write_stdout();
///     s.connect(&stdin.output, &hex_encoder.input);
///     s.connect(&hex_encoder.output, &stdout.input);
/// });
/// # }
/// ```
///
/// ## Running the block via the CLI
///
/// ```console
/// $ protoflow execute EncodeHex
/// ```
///
#[derive(Block, Clone)]
pub struct EncodeHex {
    /// The input byte stream.
    #[input]
    pub input: InputPort<Bytes>,

    /// The output text stream.
    #[output]
    pub output: OutputPort<Bytes>,
}

impl EncodeHex {
    pub fn new(input: InputPort<Bytes>, output: OutputPort<Bytes>) -> Self {
        Self { input, output }
    }
}

impl Block for EncodeHex {
    fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult {
        runtime.wait_for(&self.input)?;

        while let Some(message) = self.input.recv()? {
            let mut buffer = String::with_capacity(message.len() * 2);
            for byte in message.iter() {
                buffer.push_str(format!("{:02x}", byte).as_str()); // TODO: optimize
            }
            let message = Bytes::from(buffer);
            self.output.send(&message)?;
        }

        Ok(())
    }
}

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

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

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

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