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

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

/// A block for sending a constant value.
///
/// This block sends a constant value on its output port.
/// It can also be used to send a constant value to multiple blocks.
///
/// The value to send is specified as a parameter, and can be of any
/// type that implements the [`Message`] trait.
///
/// The block waits for the output port to be connected before sending
/// the value, and closes the port after the value is sent.
///
/// The block does not have any input ports nor state.
///
/// # Block Diagram
#[doc = mermaid!("../../doc/core/const.mmd")]
///
/// # Sequence Diagram
#[doc = mermaid!("../../doc/core/const.seq.mmd" framed)]
///
/// # Examples
///
/// ## Using the block in a system
///
/// ```rust
/// # use protoflow_blocks::*;
/// # fn main() {
/// System::build(|s| {
///     let const_value = s.const_string("Hello, world!");
///     let line_encoder = s.encode_lines();
///     let stdout = s.write_stdout();
///     s.connect(&const_value.output, &line_encoder.input);
///     s.connect(&line_encoder.output, &stdout.input);
/// });
/// # }
/// ```
///
/// ## Running the block via the CLI
///
/// ```console
/// $ protoflow execute Const value=Hello
/// ```
///
#[derive(Block, Clone)]
pub struct Const<T: Message = String> {
    /// The port to send the value on.
    #[output]
    pub output: OutputPort<T>,

    /// A parameter for the value to send.
    #[parameter]
    pub value: T,
}

impl<T: Message + Default> Const<T> {
    pub fn new(output: OutputPort<T>) -> Self {
        Self::with_params(output, T::default())
    }
}

impl<T: Message> Const<T> {
    pub fn with_params(output: OutputPort<T>, value: T) -> Self {
        Self { output, value }
    }
}

impl<T: Message> Block for Const<T> {
    fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult {
        runtime.wait_for(&self.output)?;

        self.output.send(&self.value)?;

        Ok(())
    }
}

#[cfg(feature = "std")]
impl<T: Message> StdioSystem for Const<T> {
    fn build_system(config: StdioConfig) -> Result<System, StdioError> {
        use crate::{CoreBlocks, IoBlocks, SysBlocks, SystemBuilding};

        let Some(value) = config.params.get("value").map(String::clone) else {
            return Err(StdioError::MissingParameter("value"))?;
        };

        Ok(System::build(|s| {
            let const_value = s.const_string(value); // FIXME
            let line_encoder = s.encode_with(config.encoding);
            let stdout = s.write_stdout();
            s.connect(&const_value.output, &line_encoder.input);
            s.connect(&line_encoder.output, &stdout.input);
        }))
    }
}

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

    #[test]
    fn instantiate_block() {
        // Check that the block is constructible:
        let _ = System::build(|s| {
            let _ = s.block(Const::<i32>::with_params(s.output(), 0x00BAB10C));
        });
    }
}