Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MapFrom block #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/protoflow-blocks/doc/core/mapper.mmd
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions lib/protoflow-blocks/doc/core/mapper.seq.mmd
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/protoflow-blocks/src/blocks/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub mod core {

fn drop<T: Message + 'static>(&mut self) -> Drop<T>;

fn mapper<Input: Message + 'static, Output: Message + From<Input> + 'static>(
&mut self,
) -> Mapper<Input, Output>;

fn random<T: Message + 'static>(&mut self) -> Random<T>;

fn random_seeded<T: Message + 'static>(&mut self, seed: Option<u64>) -> Random<T>;
Expand All @@ -45,6 +49,7 @@ pub mod core {
Count,
Delay,
Drop,
Mapper,
Random,
}

Expand Down Expand Up @@ -76,6 +81,11 @@ pub mod core {
input: InputPortName,
},

Mapper {
input: InputPortName,
output: OutputPortName,
},

Random {
output: OutputPortName,
seed: Option<u64>,
Expand All @@ -91,6 +101,7 @@ pub mod core {
Count { .. } => "Count",
Delay { .. } => "Delay",
Drop { .. } => "Drop",
Mapper { .. } => "Mapper",
Random { .. } => "Random",
})
}
Expand All @@ -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()))],
}
}
Expand All @@ -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::<u64>(), *seed))
// TODO: Random::with_system(system, *seed))
Expand All @@ -156,6 +172,9 @@ pub mod core {
mod drop;
pub use drop::*;

mod mapper;
pub use mapper::*;

mod random;
pub use random::*;
}
Expand Down
82 changes: 82 additions & 0 deletions lib/protoflow-blocks/src/blocks/core/mapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This is free and unencumbered software released into the public domain.

use crate::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<Input: Message, Output: Message + From<Input>> {
/// The input message stream.
#[input]
pub input: InputPort<Input>,

/// The output message stream.
#[output]
pub output: OutputPort<Output>,
}

impl<Input: Message, Output: Message + From<Input>> Mapper<Input, Output> {
pub fn new(input: InputPort<Input>, output: OutputPort<Output>) -> Self {
Self::with_params(input, output)
}
}

impl<Input: Message, Output: Message + From<Input>> Mapper<Input, Output> {
pub fn with_params(input: InputPort<Input>, output: OutputPort<Output>) -> Self {
Self { input, output }
}
}

impl<Input: Message + 'static, Output: Message + From<Input> + 'static> Mapper<Input, Output> {
pub fn with_system(system: &System) -> Self {
use crate::SystemBuilding;
Self::with_params(system.input(), system.output())
}
}

impl<Input: Message, Output: Message + From<Input>> Block for Mapper<Input, Output> {
fn execute(&mut self, _: &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::<u32, u64>::with_params(s.input(), s.output()));
});
}
}
10 changes: 8 additions & 2 deletions lib/protoflow-blocks/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -154,6 +154,12 @@ impl CoreBlocks for System {
self.0.block(Drop::<T>::with_system(self))
}

fn mapper<Input: Message + 'static, Output: Message + From<Input> + 'static>(
&mut self,
) -> Mapper<Input, Output> {
self.0.block(Mapper::<Input, Output>::with_system(self))
}

fn random<T: Message + 'static>(&mut self) -> Random<T> {
self.0.block(Random::<T>::with_system(self, None))
}
Expand Down