use crate::{
prelude::{vec, String},
StdioConfig, StdioError, StdioSystem, System,
};
use protoflow_core::{Block, BlockResult, BlockRuntime, Message, OutputPort};
use protoflow_derive::Block;
use simple_mermaid::mermaid;
#[doc = mermaid!("../../../doc/core/const.mmd")]
#[doc = mermaid!("../../../doc/core/const.seq.mmd" framed)]
#[derive(Block, Clone)]
pub struct Const<T: Message = String> {
#[output]
pub output: OutputPort<T>,
#[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 + 'static> Const<T> {
pub fn with_system(system: &System, value: T) -> Self {
use crate::SystemBuilding;
Self::with_params(system.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, SystemBuilding};
config.allow_only(vec!["value"])?;
let value = config.get_string("value")?;
Ok(System::build(|s| {
let const_value = s.const_string(value); let line_encoder = s.encode_with(config.encoding);
let stdout = config.write_stdout(s);
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() {
let _ = System::build(|s| {
let _ = s.block(Const::<i32>::with_params(s.output(), 0x00BAB10C));
});
}
}