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

Implement the Add/Div/Mul/Sub blocks #19

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ The built-in blocks provided by Protoflow are listed below:

| Block | Description |
|:------------------|:-------------------------------------------------------------------------------------------------------------------------------|
| [`Add`] | Adds each number in the input stream to a running total. |
| [`Buffer`] | Stores all messages it receives. |
| [`ConcatStrings`] | Concatenates the received string messages, with an optional delimiter string inserted between each message. |
| [`Const`] | Sends a constant value. |
Expand All @@ -121,24 +122,49 @@ The built-in blocks provided by Protoflow are listed below:
| [`DecodeHex`] | Decodes hexadecimal stream to byte stream. |
| [`DecodeJSON`] | Decodes JSON messages from a byte stream. |
| [`Delay`] | Passes messages through while delaying them by a fixed or random duration. |
| [`Div`] | Divides a running total by each number in the input stream sequentially. |
| [`Drop`] | Discards all messages it receives. |
| [`Encode`] | Encodes messages to a byte stream. |
| [`EncodeCSV`] | Encodes the provided header and rows, given as `prost_types::Value`, into a CSV-formatted byte stream. |
| [`EncodeHex`] | Encodes a byte stream into hexadecimal form. |
| [`EncodeJSON`] | Encodes messages into JSON format. |
| [`Hash`] | Computes the cryptographic hash of a byte stream. |
| [`Mul`] | Multiplies each number in the input stream with a running product. |
| [`Random`] | Generates and sends a random value. |
| [`ReadDir`] | Reads file names from a file system directory. |
| [`ReadEnv`] | Reads the value of an environment variable. |
| [`ReadFile`] | Reads bytes from the contents of a file. |
| [`ReadSocket`] | Reads bytes from a TCP socket. |
| [`ReadStdin`] | Reads bytes from standard input (aka stdin). |
| [`SplitString`] | Splits the received input message, with an optional delimiter string parameter. |
| [`Sub`] | Subtracts each number in the input stream from a running total. |
| [`WriteFile`] | Writes or appends bytes to the contents of a file. |
| [`WriteSocket`] | Writes bytes to a TCP socket |
| [`WriteStderr`] | Writes bytes to standard error (aka stderr). |
| [`WriteStdout`] | Writes bytes to standard output (aka stdout). |

#### [`Add`]

A block that adds each number in the input stream to a running total

```mermaid
block-beta
columns 7
Source space:2 Add space:2 Sink
Source-- "input" -->Add
Add-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Add block
class Source hidden
class Sink hidden
```

```bash
protoflow execute Add
```

#### [`Buffer`]

A block that simply stores all messages it receives.
Expand Down Expand Up @@ -342,6 +368,28 @@ block-beta
protoflow execute Delay fixed=2
```

#### [`Div`]

A block that divides a running total by each number in the input stream sequentially

```mermaid
block-beta
columns 7
Source space:2 Div space:2 Sink
Source-- "input" -->Div
Div-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Div block
class Source hidden
class Sink hidden
```

```bash
protoflow execute Div
```

#### [`Drop`]

A block that simply discards all messages it receives.
Expand Down Expand Up @@ -483,6 +531,28 @@ block-beta
protoflow execute Hash algorithm=blake3
```

#### [`Mul`]

A block that multiplies each number in the input stream with a running product

```mermaid
block-beta
columns 7
Source space:2 Mul space:2 Sink
Source-- "input" -->Mul
Mul-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Mul block
class Source hidden
class Sink hidden
```

```bash
protoflow execute Mul
```

#### [`Random`]

A block for generating and sending a random value.
Expand Down Expand Up @@ -640,6 +710,28 @@ block-beta
protoflow execute SplitString delimiter=","
```

#### [`Sub`]

A block subtracts each number in the input stream from a running total

```mermaid
block-beta
columns 7
Source space:2 Sub space:2 Sink
Source-- "input" -->Sub
Sub-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Sub block
class Source hidden
class Sink hidden
```

```bash
protoflow execute Sub
```

#### [`WriteFile`]

A block that writes or appends bytes to the contents of a file.
Expand Down Expand Up @@ -794,6 +886,7 @@ To add a new block type implementation, make sure to examine and amend:
[`echo_lines`]: lib/protoflow/examples/echo_lines
[`examples`]: lib/protoflow/examples

[`Add`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Add.html
[`Buffer`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Buffer.html
[`ConcatStrings`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ConcatStrings.html
[`Const`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Const.html
Expand All @@ -803,19 +896,22 @@ To add a new block type implementation, make sure to examine and amend:
[`DecodeHex`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.DecodeHex.html
[`DecodeJSON`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.DecodeJson.html
[`Delay`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Delay.html
[`Div`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Div.html
[`Drop`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Drop.html
[`Encode`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Encode.html
[`EncodeCSV`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.EncodeCsv.html
[`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
[`Mul`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Mul.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
[`ReadFile`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadFile.html
[`ReadSocket`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadSocket.html
[`ReadStdin`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.ReadStdin.html
[`SplitString`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.SplitString.html
[`Sub`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.Sub.html
[`WriteFile`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.WriteFile.html
[`WriteSocket`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.WriteSocket.html
[`WriteStderr`]: https://docs.rs/protoflow-blocks/latest/protoflow_blocks/struct.WriteStderr.html
Expand Down
1 change: 1 addition & 0 deletions lib/protoflow-blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struson = "0.5"
sysml-model = { version = "=0.2.3", default-features = false, optional = true }
ubyte = { version = "0.10", default-features = false }
csv = "1.3.1"
num-traits = { version = "0.2.19", default-features = false }

[dev-dependencies]
bytes = "1.8.0"
Expand Down
11 changes: 11 additions & 0 deletions lib/protoflow-blocks/doc/math/add.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
block-beta
columns 7
Source space:2 Add space:2 Sink
Source-- "input" -->Add
Add-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Add block
class Source hidden
class Sink hidden
21 changes: 21 additions & 0 deletions lib/protoflow-blocks/doc/math/add.seq.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sequenceDiagram
autonumber
participant BlockA as Another block
participant Add.input as Add.input port
participant Add as Add block
participant Add.output as Add.output port
participant BlockB as Another block

BlockA-->>Add: Connect
Add-->>BlockB: Connect

loop Add process
BlockA->>Add: Message
Add->>Add: Add numbers
Add->>BlockB: Message
end

BlockA-->>Add: Disconnect
Add-->>Add.input: Close
Add-->>Add.output: Close
Add-->>BlockB: Disconnect
11 changes: 11 additions & 0 deletions lib/protoflow-blocks/doc/math/div.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
block-beta
columns 7
Source space:2 Div space:2 Sink
Source-- "input" -->Div
Div-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Div block
class Source hidden
class Sink hidden
21 changes: 21 additions & 0 deletions lib/protoflow-blocks/doc/math/div.seq.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sequenceDiagram
autonumber
participant BlockA as Another block
participant Div.input as Div.input port
participant Div as Div block
participant Div.output as Div.output port
participant BlockB as Another block

BlockA-->>Div: Connect
Div-->>BlockB: Connect

loop Div process
BlockA->>Div: Message
Div->>Div: Div numbers
Div->>BlockB: Message
end

BlockA-->>Div: Disconnect
Div-->>Div.input: Close
Div-->>Div.output: Close
Div-->>BlockB: Disconnect
11 changes: 11 additions & 0 deletions lib/protoflow-blocks/doc/math/mul.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
block-beta
columns 7
Source space:2 Mul space:2 Sink
Source-- "input" -->Mul
Mul-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Mul block
class Source hidden
class Sink hidden
21 changes: 21 additions & 0 deletions lib/protoflow-blocks/doc/math/mul.seq.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sequenceDiagram
autonumber
participant BlockA as Another block
participant Mul.input as Mul.input port
participant Mul as Mul block
participant Mul.output as Mul.output port
participant BlockB as Another block

BlockA-->>Mul: Connect
Mul-->>BlockB: Connect

loop Mul process
BlockA->>Mul: Message
Mul->>Mul: Mul numbers
Mul->>BlockB: Message
end

BlockA-->>Mul: Disconnect
Mul-->>Mul.input: Close
Mul-->>Mul.output: Close
Mul-->>BlockB: Disconnect
11 changes: 11 additions & 0 deletions lib/protoflow-blocks/doc/math/sub.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
block-beta
columns 7
Source space:2 Sub space:2 Sink
Source-- "input" -->Sub
Sub-- "output" -->Sink

classDef block height:48px,padding:8px;
classDef hidden visibility:none;
class Sub block
class Source hidden
class Sink hidden
21 changes: 21 additions & 0 deletions lib/protoflow-blocks/doc/math/sub.seq.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sequenceDiagram
autonumber
participant BlockA as Another block
participant Sub.input as Sub.input port
participant Sub as Sub block
participant Sub.output as Sub.output port
participant BlockB as Another block

BlockA-->>Sub: Connect
Sub-->>BlockB: Connect

loop Sub process
BlockA->>Sub: Message
Sub->>Sub: Sub numbers
Sub->>BlockB: Message
end

BlockA-->>Sub: Disconnect
Sub-->>Sub.input: Close
Sub-->>Sub.output: Close
Sub-->>BlockB: Disconnect
4 changes: 4 additions & 0 deletions lib/protoflow-blocks/src/block_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl<'de> serde::Deserialize<'de> for BlockConfig {
.unwrap()
}

"Add" | "Div" | "Mul" | "Sub" => MathBlockConfig::deserialize(value.clone())
.map(BlockConfig::Math)
.unwrap(),

#[cfg(feature = "std")]
"ReadDir" | "ReadEnv" | "ReadFile" | "ReadSocket" | "ReadStdin" | "WriteFile"
| "WriteSocket" | "WriteStderr" | "WriteStdout" => {
Expand Down
16 changes: 16 additions & 0 deletions lib/protoflow-blocks/src/block_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub enum BlockTag {
EncodeHex,
EncodeJson,
// MathBlocks
Add,
Div,
Mul,
Sub,
// SysBlocks
#[cfg(feature = "std")]
ReadDir,
Expand Down Expand Up @@ -91,6 +95,10 @@ impl BlockTag {
Encode => "Encode",
EncodeHex => "EncodeHex",
EncodeJson => "EncodeJSON",
Add => "Add",
Div => "Div",
Mul => "Mul",
Sub => "Sub",
#[cfg(feature = "std")]
ReadDir => "ReadDir",
#[cfg(feature = "std")]
Expand Down Expand Up @@ -142,6 +150,10 @@ impl FromStr for BlockTag {
"Encode" => Encode,
"EncodeHex" => EncodeHex,
"EncodeJSON" => EncodeJson,
"Add" => Add,
"Div" => Div,
"Mul" => Mul,
"Sub" => Sub,
#[cfg(feature = "std")]
"ReadDir" => ReadDir,
#[cfg(feature = "std")]
Expand Down Expand Up @@ -204,6 +216,10 @@ impl BlockInstantiation for BlockTag {
Encode => Box::new(super::Encode::<String>::with_system(system, None)),
EncodeHex => Box::new(super::EncodeHex::with_system(system)),
EncodeJson => Box::new(super::EncodeJson::with_system(system)),
Add => Box::new(super::Add::<f64>::with_system(system)),
Div => Box::new(super::Div::<f64>::with_system(system)),
Mul => Box::new(super::Mul::<f64>::with_system(system)),
Sub => Box::new(super::Sub::<f64>::with_system(system)),
#[cfg(feature = "std")]
ReadDir => Box::new(super::ReadDir::with_system(system)),
#[cfg(feature = "std")]
Expand Down
Loading
Loading