From 5d3681745abf5eca76f5685ab9b8621604d089c0 Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Tue, 17 Dec 2024 11:14:57 +0200 Subject: [PATCH 1/8] add Mapper block --- lib/protoflow-blocks/doc/core/mapper.mmd | 11 +++ lib/protoflow-blocks/doc/core/mapper.seq.mmd | 21 +++++ lib/protoflow-blocks/src/blocks/core.rs | 19 ++++ .../src/blocks/core/mapper.rs | 90 +++++++++++++++++++ lib/protoflow-blocks/src/system.rs | 10 ++- 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 lib/protoflow-blocks/doc/core/mapper.mmd create mode 100644 lib/protoflow-blocks/doc/core/mapper.seq.mmd create mode 100644 lib/protoflow-blocks/src/blocks/core/mapper.rs diff --git a/lib/protoflow-blocks/doc/core/mapper.mmd b/lib/protoflow-blocks/doc/core/mapper.mmd new file mode 100644 index 00000000..99fcd463 --- /dev/null +++ b/lib/protoflow-blocks/doc/core/mapper.mmd @@ -0,0 +1,11 @@ +block-beta + columns 7 + Source space:2 Mapper space:2 Sink + Source-- "input" -->Mapper + Mapper-- "output" -->Sink + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Mapper block + class Source hidden + class Sink hidden diff --git a/lib/protoflow-blocks/doc/core/mapper.seq.mmd b/lib/protoflow-blocks/doc/core/mapper.seq.mmd new file mode 100644 index 00000000..2cbad473 --- /dev/null +++ b/lib/protoflow-blocks/doc/core/mapper.seq.mmd @@ -0,0 +1,21 @@ +sequenceDiagram + autonumber + participant BlockA as Another block + participant Mapper.input as Mapper.input port + participant Mapper as Mapper block + participant Mapper.output as Mapper.output port + participant BlockB as Another block + + BlockA-->>Mapper: Connect + Mapper-->>BlockB: Connect + + loop Mapper process + BlockA->>Mapper: Message + Mapper->>Mapper: Transform + Mapper->>BlockB: Message + end + + BlockA-->>Mapper: Disconnect + Mapper-->>Mapper.input: Close + Mapper-->>Mapper.output: Close + Mapper-->>BlockB: Disconnect diff --git a/lib/protoflow-blocks/src/blocks/core.rs b/lib/protoflow-blocks/src/blocks/core.rs index 15557ba0..eb118d45 100644 --- a/lib/protoflow-blocks/src/blocks/core.rs +++ b/lib/protoflow-blocks/src/blocks/core.rs @@ -32,6 +32,10 @@ pub mod core { fn drop(&mut self) -> Drop; + fn mapper + 'static>( + &mut self, + ) -> Mapper; + fn random(&mut self) -> Random; fn random_seeded(&mut self, seed: Option) -> Random; @@ -45,6 +49,7 @@ pub mod core { Count, Delay, Drop, + Mapper, Random, } @@ -76,6 +81,11 @@ pub mod core { input: InputPortName, }, + Mapper { + input: InputPortName, + output: OutputPortName, + }, + Random { output: OutputPortName, seed: Option, @@ -91,6 +101,7 @@ pub mod core { Count { .. } => "Count", Delay { .. } => "Delay", Drop { .. } => "Drop", + Mapper { .. } => "Mapper", Random { .. } => "Random", }) } @@ -107,6 +118,7 @@ pub mod core { } Delay { output, .. } => vec![("output", Some(output.clone()))], Drop { .. } => vec![], + Mapper { output, .. } => vec![("output", Some(output.clone()))], Random { output, .. } => vec![("output", Some(output.clone()))], } } @@ -133,6 +145,10 @@ pub mod core { // TODO: Delay::with_system(system, Some(delay.clone()))) } Drop { .. } => Box::new(super::Drop::new(system.input_any())), // TODO: Drop::with_system(system) + Mapper { .. } => Box::new(super::Mapper::with_params( + system.input_any(), + system.output_any(), + )), Random { seed, .. } => { Box::new(super::Random::with_params(system.output::(), *seed)) // TODO: Random::with_system(system, *seed)) @@ -156,6 +172,9 @@ pub mod core { mod drop; pub use drop::*; + mod mapper; + pub use mapper::*; + mod random; pub use random::*; } diff --git a/lib/protoflow-blocks/src/blocks/core/mapper.rs b/lib/protoflow-blocks/src/blocks/core/mapper.rs new file mode 100644 index 00000000..0a3dfccc --- /dev/null +++ b/lib/protoflow-blocks/src/blocks/core/mapper.rs @@ -0,0 +1,90 @@ +// This is free and unencumbered software released into the public domain. + +use core::time::Duration; + +use crate::{ + prelude::{vec, String}, + StdioConfig, StdioError, StdioSystem, System, +}; +use protoflow_core::{Block, BlockResult, BlockRuntime, InputPort, Message, OutputPort}; +use protoflow_derive::Block; +use simple_mermaid::mermaid; + +/// A block to map a message from one type to another. +/// +/// # Block Diagram +// #[doc = mermaid!("../../../doc/core/mapper.mmd")] +/// +/// # Sequence Diagram +// #[doc = mermaid!("../../../doc/core/mapper.seq.mmd" framed)] +/// +/// # Examples +/// +/// ## Using the block in a system +/// +/// ```rust +/// # use protoflow_blocks::*; +/// # fn main() { +/// System::build(|s| { +/// // TODO +/// }); +/// # } +/// ``` +/// +#[derive(Block, Clone)] +pub struct Mapper> { + /// The input message stream. + #[input] + pub input: InputPort, + + /// The output message stream. + #[output] + pub output: OutputPort, +} + +impl> Mapper { + pub fn new(input: InputPort, output: OutputPort) -> Self { + Self::with_params(input, output) + } +} + +impl> Mapper { + pub fn with_params(input: InputPort, output: OutputPort) -> Self { + Self { input, output } + } +} + +impl + 'static> Mapper { + pub fn with_system(system: &System) -> Self { + use crate::SystemBuilding; + Self::with_params(system.input(), system.output()) + } +} + +impl> Block for Mapper { + fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult { + while let Some(input) = self.input.recv()? { + let output: Output = From::from(input); + self.output.send(&output)?; + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::Mapper; + use crate::{System, SystemBuilding}; + + #[test] + fn instantiate_block() { + // Check that the block is constructible: + let _ = System::build(|s| { + let _ = s.block(Mapper::::with_params( + system.input(), + system.output(), + )); + }); + } +} diff --git a/lib/protoflow-blocks/src/system.rs b/lib/protoflow-blocks/src/system.rs index 695ccfba..527adfbc 100644 --- a/lib/protoflow-blocks/src/system.rs +++ b/lib/protoflow-blocks/src/system.rs @@ -7,8 +7,8 @@ use crate::{ types::{DelayType, Encoding}, AllBlocks, Buffer, ConcatStrings, Const, CoreBlocks, Count, Decode, DecodeCsv, DecodeHex, DecodeJson, Delay, Drop, Encode, EncodeCsv, EncodeHex, EncodeJson, FlowBlocks, HashBlocks, - IoBlocks, MathBlocks, Random, ReadDir, ReadEnv, ReadFile, ReadSocket, ReadStdin, SplitString, - SysBlocks, TextBlocks, WriteFile, WriteSocket, WriteStderr, WriteStdout, + IoBlocks, Mapper, MathBlocks, Random, ReadDir, ReadEnv, ReadFile, ReadSocket, ReadStdin, + SplitString, SysBlocks, TextBlocks, WriteFile, WriteSocket, WriteStderr, WriteStdout, }; use protoflow_core::{ Block, BlockID, BlockResult, BoxedBlockType, InputPort, Message, OutputPort, PortID, @@ -154,6 +154,12 @@ impl CoreBlocks for System { self.0.block(Drop::::with_system(self)) } + fn mapper + 'static>( + &mut self, + ) -> Mapper { + self.0.block(Mapper::::with_system(self)) + } + fn random(&mut self) -> Random { self.0.block(Random::::with_system(self, None)) } From 629cc3c744f4e06ac5fc0cd254ccd96e04900749 Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Tue, 17 Dec 2024 13:46:38 +0200 Subject: [PATCH 2/8] add Mapper block to README --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index a6222644..f1c8858f 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ The built-in blocks provided by Protoflow are listed below: | [`EncodeHex`] | Encodes a byte stream into hexadecimal form. | | [`EncodeJSON`] | Encodes messages into JSON format. | | [`Hash`] | Computes the cryptographic hash of a byte stream. | +| [`Mapper`] | Maps a message from one type to another. | | [`Random`] | Generates and sends a random value. | | [`ReadDir`] | Reads file names from a file system directory. | | [`ReadEnv`] | Reads the value of an environment variable. | @@ -483,6 +484,24 @@ block-beta protoflow execute Hash algorithm=blake3 ``` +#### [`Mapper`] + +A block to map a message from one type to another. + +```mermaid +block-beta + columns 7 + Source space:2 Mapper space:2 Sink + Source-- "input" -->Mapper + Mapper-- "output" -->Sink + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Mapper block + class Source hidden + class Sink hidden +``` + #### [`Random`] A block for generating and sending a random value. @@ -809,6 +828,7 @@ To add a new block type implementation, make sure to examine and amend: [`EncodeHex`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.EncodeHex.html [`EncodeJSON`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.EncodeJson.html [`Hash`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Hash.html +[`Mapper`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Mapper.html [`Random`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Random.html [`ReadDir`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadDir.html [`ReadEnv`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadEnv.html From 6cd9ee5e21d473db9d85635ac80f720d8e7cfdf6 Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Tue, 17 Dec 2024 13:57:24 +0200 Subject: [PATCH 3/8] fix tests --- lib/protoflow-blocks/src/blocks/core/mapper.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/protoflow-blocks/src/blocks/core/mapper.rs b/lib/protoflow-blocks/src/blocks/core/mapper.rs index 0a3dfccc..9d231881 100644 --- a/lib/protoflow-blocks/src/blocks/core/mapper.rs +++ b/lib/protoflow-blocks/src/blocks/core/mapper.rs @@ -81,10 +81,7 @@ mod tests { fn instantiate_block() { // Check that the block is constructible: let _ = System::build(|s| { - let _ = s.block(Mapper::::with_params( - system.input(), - system.output(), - )); + let _ = s.block(Mapper::::with_params(s.input(), s.output())); }); } } From 9738f1b728654c747a30371c63fe30ff0d54fb0c Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Wed, 18 Dec 2024 08:35:11 +0200 Subject: [PATCH 4/8] fix warnings --- lib/protoflow-blocks/src/blocks/core/mapper.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/protoflow-blocks/src/blocks/core/mapper.rs b/lib/protoflow-blocks/src/blocks/core/mapper.rs index 9d231881..addf88f5 100644 --- a/lib/protoflow-blocks/src/blocks/core/mapper.rs +++ b/lib/protoflow-blocks/src/blocks/core/mapper.rs @@ -1,11 +1,6 @@ // This is free and unencumbered software released into the public domain. -use core::time::Duration; - -use crate::{ - prelude::{vec, String}, - StdioConfig, StdioError, StdioSystem, System, -}; +use crate::System; use protoflow_core::{Block, BlockResult, BlockRuntime, InputPort, Message, OutputPort}; use protoflow_derive::Block; use simple_mermaid::mermaid; @@ -62,7 +57,7 @@ impl + 'static> Mapper> Block for Mapper { - fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult { + fn execute(&mut self, _: &dyn BlockRuntime) -> BlockResult { while let Some(input) = self.input.recv()? { let output: Output = From::from(input); self.output.send(&output)?; From abb951956c9fcc9a62da23fc6a745e87f0523aad Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Wed, 18 Dec 2024 08:35:48 +0200 Subject: [PATCH 5/8] fix mermaid --- lib/protoflow-blocks/src/blocks/core/mapper.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/protoflow-blocks/src/blocks/core/mapper.rs b/lib/protoflow-blocks/src/blocks/core/mapper.rs index addf88f5..41328964 100644 --- a/lib/protoflow-blocks/src/blocks/core/mapper.rs +++ b/lib/protoflow-blocks/src/blocks/core/mapper.rs @@ -8,10 +8,10 @@ use simple_mermaid::mermaid; /// A block to map a message from one type to another. /// /// # Block Diagram -// #[doc = mermaid!("../../../doc/core/mapper.mmd")] +#[doc = mermaid!("../../../doc/core/mapper.mmd")] /// /// # Sequence Diagram -// #[doc = mermaid!("../../../doc/core/mapper.seq.mmd" framed)] +#[doc = mermaid!("../../../doc/core/mapper.seq.mmd" framed)] /// /// # Examples /// From 3db3badffb4398a17941f2dc5891263118b85f38 Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Thu, 26 Dec 2024 11:55:31 +0200 Subject: [PATCH 6/8] rename Mapper to MapFrom --- README.md | 14 ++++++------- .../doc/core/{mapper.mmd => map_from.mmd} | 8 +++---- .../doc/core/map_from.seq.mmd | 21 +++++++++++++++++++ lib/protoflow-blocks/doc/core/mapper.seq.mmd | 21 ------------------- lib/protoflow-blocks/src/blocks/core.rs | 18 ++++++++-------- .../blocks/core/{mapper.rs => map_from.rs} | 18 ++++++++-------- lib/protoflow-blocks/src/system.rs | 8 +++---- 7 files changed, 54 insertions(+), 54 deletions(-) rename lib/protoflow-blocks/doc/core/{mapper.mmd => map_from.mmd} (54%) create mode 100644 lib/protoflow-blocks/doc/core/map_from.seq.mmd delete mode 100644 lib/protoflow-blocks/doc/core/mapper.seq.mmd rename lib/protoflow-blocks/src/blocks/core/{mapper.rs => map_from.rs} (77%) diff --git a/README.md b/README.md index f1c8858f..a2f27a19 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ The built-in blocks provided by Protoflow are listed below: | [`EncodeHex`] | Encodes a byte stream into hexadecimal form. | | [`EncodeJSON`] | Encodes messages into JSON format. | | [`Hash`] | Computes the cryptographic hash of a byte stream. | -| [`Mapper`] | Maps a message from one type to another. | +| [`MapFrom`] | Maps a message from one type to another. | | [`Random`] | Generates and sends a random value. | | [`ReadDir`] | Reads file names from a file system directory. | | [`ReadEnv`] | Reads the value of an environment variable. | @@ -484,20 +484,20 @@ block-beta protoflow execute Hash algorithm=blake3 ``` -#### [`Mapper`] +#### [`MapFrom`] A block to map a message from one type to another. ```mermaid block-beta columns 7 - Source space:2 Mapper space:2 Sink - Source-- "input" -->Mapper - Mapper-- "output" -->Sink + Source space:2 MapFrom space:2 Sink + Source-- "input" -->MapFrom + MapFrom-- "output" -->Sink classDef block height:48px,padding:8px; classDef hidden visibility:none; - class Mapper block + class MapFrom block class Source hidden class Sink hidden ``` @@ -828,7 +828,7 @@ To add a new block type implementation, make sure to examine and amend: [`EncodeHex`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.EncodeHex.html [`EncodeJSON`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.EncodeJson.html [`Hash`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Hash.html -[`Mapper`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Mapper.html +[`MapFrom`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.MapFrom.html [`Random`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Random.html [`ReadDir`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadDir.html [`ReadEnv`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadEnv.html diff --git a/lib/protoflow-blocks/doc/core/mapper.mmd b/lib/protoflow-blocks/doc/core/map_from.mmd similarity index 54% rename from lib/protoflow-blocks/doc/core/mapper.mmd rename to lib/protoflow-blocks/doc/core/map_from.mmd index 99fcd463..42d0e520 100644 --- a/lib/protoflow-blocks/doc/core/mapper.mmd +++ b/lib/protoflow-blocks/doc/core/map_from.mmd @@ -1,11 +1,11 @@ block-beta columns 7 - Source space:2 Mapper space:2 Sink - Source-- "input" -->Mapper - Mapper-- "output" -->Sink + Source space:2 MapFrom space:2 Sink + Source-- "input" -->MapFrom + MapFrom-- "output" -->Sink classDef block height:48px,padding:8px; classDef hidden visibility:none; - class Mapper block + class MapFrom block class Source hidden class Sink hidden diff --git a/lib/protoflow-blocks/doc/core/map_from.seq.mmd b/lib/protoflow-blocks/doc/core/map_from.seq.mmd new file mode 100644 index 00000000..48cb5c7f --- /dev/null +++ b/lib/protoflow-blocks/doc/core/map_from.seq.mmd @@ -0,0 +1,21 @@ +sequenceDiagram + autonumber + participant BlockA as Another block + participant MapFrom.input as MapFrom.input port + participant MapFrom as MapFrom block + participant MapFrom.output as MapFrom.output port + participant BlockB as Another block + + BlockA-->>MapFrom: Connect + MapFrom-->>BlockB: Connect + + loop MapFrom process + BlockA->>MapFrom: Message + MapFrom->>MapFrom: Transform + MapFrom->>BlockB: Message + end + + BlockA-->>MapFrom: Disconnect + MapFrom-->>MapFrom.input: Close + MapFrom-->>MapFrom.output: Close + MapFrom-->>BlockB: Disconnect diff --git a/lib/protoflow-blocks/doc/core/mapper.seq.mmd b/lib/protoflow-blocks/doc/core/mapper.seq.mmd deleted file mode 100644 index 2cbad473..00000000 --- a/lib/protoflow-blocks/doc/core/mapper.seq.mmd +++ /dev/null @@ -1,21 +0,0 @@ -sequenceDiagram - autonumber - participant BlockA as Another block - participant Mapper.input as Mapper.input port - participant Mapper as Mapper block - participant Mapper.output as Mapper.output port - participant BlockB as Another block - - BlockA-->>Mapper: Connect - Mapper-->>BlockB: Connect - - loop Mapper process - BlockA->>Mapper: Message - Mapper->>Mapper: Transform - Mapper->>BlockB: Message - end - - BlockA-->>Mapper: Disconnect - Mapper-->>Mapper.input: Close - Mapper-->>Mapper.output: Close - Mapper-->>BlockB: Disconnect diff --git a/lib/protoflow-blocks/src/blocks/core.rs b/lib/protoflow-blocks/src/blocks/core.rs index ce27e598..f5f81759 100644 --- a/lib/protoflow-blocks/src/blocks/core.rs +++ b/lib/protoflow-blocks/src/blocks/core.rs @@ -34,9 +34,9 @@ pub mod core { fn drop(&mut self) -> Drop; - fn mapper + 'static>( + fn map_from + 'static>( &mut self, - ) -> Mapper; + ) -> MapFrom; fn random(&mut self) -> Random; @@ -51,7 +51,7 @@ pub mod core { Count, Delay, Drop, - Mapper, + MapFrom, Random, } @@ -83,7 +83,7 @@ pub mod core { input: InputPortName, }, - Mapper { + MapFrom { input: InputPortName, output: OutputPortName, }, @@ -103,7 +103,7 @@ pub mod core { Count { .. } => "Count", Delay { .. } => "Delay", Drop { .. } => "Drop", - Mapper { .. } => "Mapper", + MapFrom { .. } => "MapFrom", Random { .. } => "Random", }) } @@ -120,7 +120,7 @@ pub mod core { } Delay { output, .. } => vec![("output", Some(output.clone()))], Drop { .. } => vec![], - Mapper { output, .. } => vec![("output", Some(output.clone()))], + MapFrom { output, .. } => vec![("output", Some(output.clone()))], Random { output, .. } => vec![("output", Some(output.clone()))], } } @@ -147,7 +147,7 @@ pub mod core { // TODO: Delay::with_system(system, Some(delay.clone()))) } Drop { .. } => Box::new(super::Drop::new(system.input_any())), // TODO: Drop::with_system(system) - Mapper { .. } => Box::new(super::Mapper::with_params( + MapFrom { .. } => Box::new(super::MapFrom::with_params( system.input_any(), system.output_any(), )), @@ -174,8 +174,8 @@ pub mod core { mod drop; pub use drop::*; - mod mapper; - pub use mapper::*; + mod map_from; + pub use map_from::*; mod random; pub use random::*; diff --git a/lib/protoflow-blocks/src/blocks/core/mapper.rs b/lib/protoflow-blocks/src/blocks/core/map_from.rs similarity index 77% rename from lib/protoflow-blocks/src/blocks/core/mapper.rs rename to lib/protoflow-blocks/src/blocks/core/map_from.rs index 41328964..f53e997c 100644 --- a/lib/protoflow-blocks/src/blocks/core/mapper.rs +++ b/lib/protoflow-blocks/src/blocks/core/map_from.rs @@ -8,10 +8,10 @@ use simple_mermaid::mermaid; /// A block to map a message from one type to another. /// /// # Block Diagram -#[doc = mermaid!("../../../doc/core/mapper.mmd")] +#[doc = mermaid!("../../../doc/core/map_from.mmd")] /// /// # Sequence Diagram -#[doc = mermaid!("../../../doc/core/mapper.seq.mmd" framed)] +#[doc = mermaid!("../../../doc/core/map_from.seq.mmd" framed)] /// /// # Examples /// @@ -27,7 +27,7 @@ use simple_mermaid::mermaid; /// ``` /// #[derive(Block, Clone)] -pub struct Mapper> { +pub struct MapFrom> { /// The input message stream. #[input] pub input: InputPort, @@ -37,26 +37,26 @@ pub struct Mapper> { pub output: OutputPort, } -impl> Mapper { +impl> MapFrom { pub fn new(input: InputPort, output: OutputPort) -> Self { Self::with_params(input, output) } } -impl> Mapper { +impl> MapFrom { pub fn with_params(input: InputPort, output: OutputPort) -> Self { Self { input, output } } } -impl + 'static> Mapper { +impl + 'static> MapFrom { pub fn with_system(system: &System) -> Self { use crate::SystemBuilding; Self::with_params(system.input(), system.output()) } } -impl> Block for Mapper { +impl> Block for MapFrom { fn execute(&mut self, _: &dyn BlockRuntime) -> BlockResult { while let Some(input) = self.input.recv()? { let output: Output = From::from(input); @@ -69,14 +69,14 @@ impl> Block for Mapper::with_params(s.input(), s.output())); + let _ = s.block(MapFrom::::with_params(s.input(), s.output())); }); } } diff --git a/lib/protoflow-blocks/src/system.rs b/lib/protoflow-blocks/src/system.rs index 566f8594..818e8130 100644 --- a/lib/protoflow-blocks/src/system.rs +++ b/lib/protoflow-blocks/src/system.rs @@ -7,7 +7,7 @@ use crate::{ types::{DelayType, Encoding}, AllBlocks, Buffer, ConcatStrings, Const, CoreBlocks, Count, Decode, DecodeCsv, DecodeHex, DecodeJson, Delay, Drop, Encode, EncodeCsv, EncodeHex, EncodeJson, FlowBlocks, HashBlocks, - IoBlocks, Mapper, MathBlocks, Random, ReadDir, ReadEnv, ReadFile, ReadStdin, SplitString, + IoBlocks, MapFrom, MathBlocks, Random, ReadDir, ReadEnv, ReadFile, ReadStdin, SplitString, SysBlocks, TextBlocks, WriteFile, WriteStderr, WriteStdout, }; #[cfg(all(feature = "std", feature = "serde"))] @@ -161,10 +161,10 @@ impl CoreBlocks for System { self.0.block(Drop::::with_system(self)) } - fn mapper + 'static>( + fn map_from + 'static>( &mut self, - ) -> Mapper { - self.0.block(Mapper::::with_system(self)) + ) -> MapFrom { + self.0.block(MapFrom::::with_system(self)) } fn random(&mut self) -> Random { From 48877f17fbf035817e74dca7a36171814b53a73c Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Thu, 26 Dec 2024 12:09:22 +0200 Subject: [PATCH 7/8] add missing implementation --- lib/protoflow-blocks/src/block_config.rs | 2 +- lib/protoflow-blocks/src/block_tag.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/protoflow-blocks/src/block_config.rs b/lib/protoflow-blocks/src/block_config.rs index a6c86827..744a351d 100644 --- a/lib/protoflow-blocks/src/block_config.rs +++ b/lib/protoflow-blocks/src/block_config.rs @@ -51,7 +51,7 @@ impl<'de> serde::Deserialize<'de> for BlockConfig { tag, value: Value::Mapping(_mapping), } => Ok(match tag.string.as_str() { - "Buffer" | "Const" | "Count" | "Delay" | "Drop" | "Random" => { + "Buffer" | "Const" | "Count" | "Delay" | "Drop" | "MapFrom" | "Random" => { CoreBlockConfig::deserialize(value.clone()) .map(BlockConfig::Core) .unwrap() diff --git a/lib/protoflow-blocks/src/block_tag.rs b/lib/protoflow-blocks/src/block_tag.rs index 546a3f46..f0ec2c6b 100644 --- a/lib/protoflow-blocks/src/block_tag.rs +++ b/lib/protoflow-blocks/src/block_tag.rs @@ -16,6 +16,7 @@ pub enum BlockTag { Count, Delay, Drop, + MapFrom, Random, // FlowBlocks // HashBlocks @@ -77,6 +78,7 @@ impl BlockTag { Count => "Count", Delay => "Delay", Drop => "Drop", + MapFrom => "MapFrom", Random => "Random", #[cfg(any( feature = "hash-blake3", @@ -128,6 +130,7 @@ impl FromStr for BlockTag { "Count" => Count, "Delay" => Delay, "Drop" => Drop, + "MapFrom" => MapFrom, "Random" => Random, #[cfg(any( feature = "hash-blake3", @@ -204,6 +207,7 @@ impl BlockInstantiation for BlockTag { Encode => Box::new(super::Encode::::with_system(system, None)), EncodeHex => Box::new(super::EncodeHex::with_system(system)), EncodeJson => Box::new(super::EncodeJson::with_system(system)), + MapFrom => Box::new(super::MapFrom::::with_system(system)), #[cfg(feature = "std")] ReadDir => Box::new(super::ReadDir::with_system(system)), #[cfg(feature = "std")] From 87c7d60c608f7970213ac674e54661b06c255f20 Mon Sep 17 00:00:00 2001 From: Yura Menshov Date: Thu, 26 Dec 2024 12:10:25 +0200 Subject: [PATCH 8/8] implement stdio --- .../src/blocks/core/map_from.rs | 17 ++++++++++++++++- lib/protoflow-blocks/src/lib.rs | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/protoflow-blocks/src/blocks/core/map_from.rs b/lib/protoflow-blocks/src/blocks/core/map_from.rs index f53e997c..8252ef52 100644 --- a/lib/protoflow-blocks/src/blocks/core/map_from.rs +++ b/lib/protoflow-blocks/src/blocks/core/map_from.rs @@ -1,6 +1,6 @@ // This is free and unencumbered software released into the public domain. -use crate::System; +use crate::{prelude::Bytes, StdioConfig, StdioError, StdioSystem, System}; use protoflow_core::{Block, BlockResult, BlockRuntime, InputPort, Message, OutputPort}; use protoflow_derive::Block; use simple_mermaid::mermaid; @@ -67,6 +67,21 @@ impl> Block for MapFrom> StdioSystem for MapFrom { + fn build_system(config: StdioConfig) -> Result { + use crate::SystemBuilding; + + config.reject_any()?; + + Ok(System::build(|s| { + let stdin = config.read_stdin(s); + let map = s.block(MapFrom::::with_system(s)); + s.connect(&stdin.output, &map.input); + })) + } +} + #[cfg(test)] mod tests { use super::MapFrom; diff --git a/lib/protoflow-blocks/src/lib.rs b/lib/protoflow-blocks/src/lib.rs index 41d1decd..62ff3469 100644 --- a/lib/protoflow-blocks/src/lib.rs +++ b/lib/protoflow-blocks/src/lib.rs @@ -58,6 +58,7 @@ pub fn build_stdio_system( "Count" => Count::::build_system(config)?, "Delay" => Delay::::build_system(config)?, "Drop" => Drop::::build_system(config)?, + "MapFrom" => MapFrom::::build_system(config)?, "Random" => Random::::build_system(config)?, // FlowBlocks // HashBlocks