From f8f74a56a95779ed52adf670bfd9cea09c6ed7a3 Mon Sep 17 00:00:00 2001 From: AlexSanches1 Date: Tue, 26 Nov 2024 00:30:42 -0500 Subject: [PATCH 1/6] Add example flows (top-level) WIP --- examples/env_var_inspector.rs | 30 ++++++++++++++++++++++++++++++ examples/hash_file.rs | 21 +++++++++++++++++++++ examples/log_directories.rs | 26 ++++++++++++++++++++++++++ examples/read_file.rs | 19 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100755 examples/env_var_inspector.rs create mode 100755 examples/hash_file.rs create mode 100644 examples/log_directories.rs create mode 100755 examples/read_file.rs diff --git a/examples/env_var_inspector.rs b/examples/env_var_inspector.rs new file mode 100755 index 00000000..6b122d6c --- /dev/null +++ b/examples/env_var_inspector.rs @@ -0,0 +1,30 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let name_param = s.const_string("PATH"); + let env_reader = s.read_env(); + let split_var = s.split_string(":"); + let line_encoder = s.encode_lines(); + let stdout = s.write_stdout(); + s.connect(&name_param.output, &env_reader.name); + s.connect(&env_reader.output, &split_var.input); + s.connect(&split_var.output, &line_encoder.input); + + s.connect(&line_encoder.output, &stdout.input); + + // // TODO use Split block + // let path_const = s.const_string("env_parts_log.txt"); + // let write_file = s.write_file(); + // s.connect(&path_const.output, &write_file.path); + // s.connect(&line_encoder.output, &write_file.input); + }) +} diff --git a/examples/hash_file.rs b/examples/hash_file.rs new file mode 100755 index 00000000..f7a5c197 --- /dev/null +++ b/examples/hash_file.rs @@ -0,0 +1,21 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let file_path = s.const_string("log_directories.txt"); + let reader = s.read_file(); + let hash_file = s.hash_sha2(); + let stdout = s.write_stdout(); + s.connect(&file_path.output, &reader.path); + s.connect(&reader.output, &hash_file.input); + s.connect(&hash_file.hash, &stdout.input); + }) +} diff --git a/examples/log_directories.rs b/examples/log_directories.rs new file mode 100644 index 00000000..789baba9 --- /dev/null +++ b/examples/log_directories.rs @@ -0,0 +1,26 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + // The path to a directory + let dir_path = s.const_string("/"); + let dir_reader = s.read_dir(); + let concat_paths = s.concat_strings_by("\n"); + let line_encoder = s.encode_lines(); + let write_path = s.const_string("log_directories.txt"); + let write_file = s.write_file(); + s.connect(&dir_path.output, &dir_reader.path); + s.connect(&dir_reader.output, &concat_paths.input); + s.connect(&concat_paths.output, &line_encoder.input); + s.connect(&write_path.output, &write_file.path); + s.connect(&line_encoder.output, &write_file.input); + }) +} diff --git a/examples/read_file.rs b/examples/read_file.rs new file mode 100755 index 00000000..cad4a326 --- /dev/null +++ b/examples/read_file.rs @@ -0,0 +1,19 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let file_path = s.const_string("log_directories.txt"); + let reader = s.read_file(); + let stdout = s.write_stdout(); + s.connect(&file_path.output, &reader.path); + s.connect(&reader.output, &stdout.input); + }) +} From bcb3597a6f58cdeca32d5be3437ab8e637287071 Mon Sep 17 00:00:00 2001 From: AlexSanches1 Date: Wed, 27 Nov 2024 01:00:25 -0500 Subject: [PATCH 2/6] Add more couple of examples --- examples/csv_roundtrip.rs | 26 ++++++++++++++++++++++++++ examples/env_var_inspector.rs | 7 ------- examples/hash_content.rs | 21 +++++++++++++++++++++ examples/hash_file.rs | 21 --------------------- examples/json_sanitizer.rs | 29 +++++++++++++++++++++++++++++ examples/read_file.rs | 19 ------------------- examples/write_and_read_file.rs | 27 +++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 47 deletions(-) create mode 100644 examples/csv_roundtrip.rs create mode 100755 examples/hash_content.rs delete mode 100755 examples/hash_file.rs create mode 100755 examples/json_sanitizer.rs delete mode 100755 examples/read_file.rs create mode 100755 examples/write_and_read_file.rs diff --git a/examples/csv_roundtrip.rs b/examples/csv_roundtrip.rs new file mode 100644 index 00000000..9d382c41 --- /dev/null +++ b/examples/csv_roundtrip.rs @@ -0,0 +1,26 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let csv_content = s.const_string("Name,Age,Score\nAlice,25,90\nBob,30,85\nCharlie,22,95"); + let line_encoder = s.encode_lines(); + let decoder = s.decode_csv(); + let encoder = s.encode_csv(); + let output_path = s.const_string("output.csv"); + let write_file = s.write_file(); + s.connect(&csv_content.output, &line_encoder.input); + s.connect(&line_encoder.output, &decoder.input); + s.connect(&decoder.header, &encoder.header); + s.connect(&decoder.rows, &encoder.rows); + s.connect(&output_path.output, &write_file.path); + s.connect(&encoder.output, &write_file.input); + }) +} diff --git a/examples/env_var_inspector.rs b/examples/env_var_inspector.rs index 6b122d6c..dedffc48 100755 --- a/examples/env_var_inspector.rs +++ b/examples/env_var_inspector.rs @@ -18,13 +18,6 @@ fn main() -> BlockResult { s.connect(&name_param.output, &env_reader.name); s.connect(&env_reader.output, &split_var.input); s.connect(&split_var.output, &line_encoder.input); - s.connect(&line_encoder.output, &stdout.input); - - // // TODO use Split block - // let path_const = s.const_string("env_parts_log.txt"); - // let write_file = s.write_file(); - // s.connect(&path_const.output, &write_file.path); - // s.connect(&line_encoder.output, &write_file.input); }) } diff --git a/examples/hash_content.rs b/examples/hash_content.rs new file mode 100755 index 00000000..9f3be83d --- /dev/null +++ b/examples/hash_content.rs @@ -0,0 +1,21 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let content = s.const_string("Hello, World!"); + let line_encoder = s.encode_lines(); + let hash_content = s.hash_sha2(); + let stdout = s.write_stdout(); + s.connect(&content.output, &line_encoder.input); + s.connect(&line_encoder.output, &hash_content.input); + s.connect(&hash_content.hash, &stdout.input); + }) +} diff --git a/examples/hash_file.rs b/examples/hash_file.rs deleted file mode 100755 index f7a5c197..00000000 --- a/examples/hash_file.rs +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rust-script -//! This is free and unencumbered software released into the public domain. -//! -//! ```cargo -//! [dependencies] -//! protoflow = "0.4.3" -//! ``` - -use protoflow::{blocks::*, BlockResult}; - -fn main() -> BlockResult { - System::run(|s| { - let file_path = s.const_string("log_directories.txt"); - let reader = s.read_file(); - let hash_file = s.hash_sha2(); - let stdout = s.write_stdout(); - s.connect(&file_path.output, &reader.path); - s.connect(&reader.output, &hash_file.input); - s.connect(&hash_file.hash, &stdout.input); - }) -} diff --git a/examples/json_sanitizer.rs b/examples/json_sanitizer.rs new file mode 100755 index 00000000..9d1dacd2 --- /dev/null +++ b/examples/json_sanitizer.rs @@ -0,0 +1,29 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let json_content = s.const_string(r#"{ + "Name": "Alice", + "Age": 25, + "Score": 90 + }"#); + let line_encoder = s.encode_lines(); + let decoder = s.decode_json(); + let encoder = s.encode_json(); + let sanitized_path = s.const_string("sanitized.json"); + let write_file = s.write_file(); + s.connect(&json_content.output, &line_encoder.input); + s.connect(&line_encoder.output, &decoder.input); + s.connect(&decoder.output, &encoder.input); + s.connect(&sanitized_path.output, &write_file.path); + s.connect(&encoder.output, &write_file.input); + }) +} diff --git a/examples/read_file.rs b/examples/read_file.rs deleted file mode 100755 index cad4a326..00000000 --- a/examples/read_file.rs +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env rust-script -//! This is free and unencumbered software released into the public domain. -//! -//! ```cargo -//! [dependencies] -//! protoflow = "0.4.3" -//! ``` - -use protoflow::{blocks::*, BlockResult}; - -fn main() -> BlockResult { - System::run(|s| { - let file_path = s.const_string("log_directories.txt"); - let reader = s.read_file(); - let stdout = s.write_stdout(); - s.connect(&file_path.output, &reader.path); - s.connect(&reader.output, &stdout.input); - }) -} diff --git a/examples/write_and_read_file.rs b/examples/write_and_read_file.rs new file mode 100755 index 00000000..b5bc3e22 --- /dev/null +++ b/examples/write_and_read_file.rs @@ -0,0 +1,27 @@ +#!/usr/bin/env rust-script +//! This is free and unencumbered software released into the public domain. +//! +//! ```cargo +//! [dependencies] +//! protoflow = "0.4.3" +//! ``` + +use protoflow::{blocks::*, BlockResult}; + +fn main() -> BlockResult { + System::run(|s| { + let file_content = s.const_string("/home/user/documents\n/home/user/downloads\n/home/user/music\n/home/user/pictures\n/home/user/videos"); + let line_encoder = s.encode_lines(); + let content_path = s.const_string("content.txt"); + let write_file = s.write_file(); + s.connect(&file_content.output, &line_encoder.input); + s.connect(&content_path.output, &write_file.path); + s.connect(&line_encoder.output, &write_file.input); + + let file_path = s.const_string("content.txt"); + let reader = s.read_file(); + let stdout = s.write_stdout(); + s.connect(&file_path.output, &reader.path); + s.connect(&reader.output, &stdout.input); + }) +} From 8c0da7cbfd3d5b645a114026083a34514d96a04d Mon Sep 17 00:00:00 2001 From: AlexSanches1 Date: Thu, 28 Nov 2024 22:30:55 -0500 Subject: [PATCH 3/6] Add example flows (metacrate) --- .../examples/csv_processing/README.md | 29 +++++++++++++++ lib/protoflow/examples/csv_processing/main.rs | 22 ++++++++++++ lib/protoflow/examples/decode_hex/README.md | 27 ++++++++++++++ lib/protoflow/examples/decode_hex/main.rs | 18 ++++++++++ lib/protoflow/examples/delay_output/README.md | 29 +++++++++++++++ lib/protoflow/examples/delay_output/main.rs | 21 +++++++++++ lib/protoflow/examples/encode_hex/README.md | 27 ++++++++++++++ lib/protoflow/examples/encode_hex/main.rs | 18 ++++++++++ .../examples/json_processing/README.md | 30 ++++++++++++++++ .../examples/json_processing/main.rs | 21 +++++++++++ .../examples/process_env_var/README.md | 32 +++++++++++++++++ .../examples/process_env_var/main.rs | 23 ++++++++++++ lib/protoflow/examples/read_file/README.md | 36 +++++++++++++++++++ lib/protoflow/examples/read_file/main.rs | 23 ++++++++++++ lib/protoflow/examples/write_file/README.md | 25 +++++++++++++ lib/protoflow/examples/write_file/main.rs | 13 +++++++ 16 files changed, 394 insertions(+) create mode 100644 lib/protoflow/examples/csv_processing/README.md create mode 100644 lib/protoflow/examples/csv_processing/main.rs create mode 100644 lib/protoflow/examples/decode_hex/README.md create mode 100644 lib/protoflow/examples/decode_hex/main.rs create mode 100644 lib/protoflow/examples/delay_output/README.md create mode 100644 lib/protoflow/examples/delay_output/main.rs create mode 100644 lib/protoflow/examples/encode_hex/README.md create mode 100644 lib/protoflow/examples/encode_hex/main.rs create mode 100644 lib/protoflow/examples/json_processing/README.md create mode 100644 lib/protoflow/examples/json_processing/main.rs create mode 100644 lib/protoflow/examples/process_env_var/README.md create mode 100644 lib/protoflow/examples/process_env_var/main.rs create mode 100644 lib/protoflow/examples/read_file/README.md create mode 100644 lib/protoflow/examples/read_file/main.rs create mode 100644 lib/protoflow/examples/write_file/README.md create mode 100644 lib/protoflow/examples/write_file/main.rs diff --git a/lib/protoflow/examples/csv_processing/README.md b/lib/protoflow/examples/csv_processing/README.md new file mode 100644 index 00000000..a3aa8192 --- /dev/null +++ b/lib/protoflow/examples/csv_processing/README.md @@ -0,0 +1,29 @@ +# CSV Processing Example + +This is a simple five-block example program that starts with a constant string +containing CSV data, encodes it into a byte stream, decodes it back into +structured CSV data consisting of headers and rows, re-encodes that structured +data back into CSV format, and writes the result to standard output (stdout). + +Note that this program ensures the integrity of the CSV data during the decoding +and re-encoding process and demonstrates the handling of structured data in CSV format. + +## Block Diagram + +```mermaid +block-beta + columns 13 + Const space:2 Encode space:2 DecodeCSV space:2 EncodeCSV space:2 WriteStdout + Const-- "output → input" -->Encode + Encode-- "output → input" -->DecodeCSV + DecodeCSV-- "header,rows, → header,rows" -->EncodeCSV + EncodeCSV-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const block + class Encode block + class DecodeCSV block + class EncodeCSV block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/csv_processing/main.rs b/lib/protoflow/examples/csv_processing/main.rs new file mode 100644 index 00000000..3545d8eb --- /dev/null +++ b/lib/protoflow/examples/csv_processing/main.rs @@ -0,0 +1,22 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let csv = s.const_string("Name,Age,Score\nAlice,25,90\nBob,30,85\nCharlie,22,95"); + + let encoder = s.encode_lines(); + s.connect(&csv.output, &encoder.input); + + let csv_decoder = s.decode_csv(); + s.connect(&encoder.output, &csv_decoder.input); + + let csv_encoder = s.encode_csv(); + s.connect(&csv_decoder.header, &csv_encoder.header); + s.connect(&csv_decoder.rows, &csv_encoder.rows); + + let stdout = s.write_stdout(); + s.connect(&csv_encoder.output, &stdout.input); + }) +} diff --git a/lib/protoflow/examples/decode_hex/README.md b/lib/protoflow/examples/decode_hex/README.md new file mode 100644 index 00000000..06e8dce9 --- /dev/null +++ b/lib/protoflow/examples/decode_hex/README.md @@ -0,0 +1,27 @@ +# Hex Decode Example + +This is a simple four-block example program that starts with a constant string +containing hexadecimal-encoded data, encodes it into a byte stream, +decodes the hexadecimal back into raw bytes, and writes the decoded data to +standard output (stdout). + +Note that this program demonstrates how to handle hexadecimal input without +relying on external files or sources, making it fully self-contained. + +## Block Diagram + +```mermaid +block-beta + columns 10 + Const space:2 Encode space:2 DecodeHex space:2 WriteStdout + Const-- "output → input" -->Encode + Encode-- "output → input" -->DecodeHex + DecodeHex-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const block + class Encode block + class DecodeHex block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/decode_hex/main.rs b/lib/protoflow/examples/decode_hex/main.rs new file mode 100644 index 00000000..1f4102ab --- /dev/null +++ b/lib/protoflow/examples/decode_hex/main.rs @@ -0,0 +1,18 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let input_string = s.const_string("54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f670a"); + + let encoder = s.encode_lines(); + s.connect(&input_string.output, &encoder.input); + + let hex_decoder = s.decode_hex(); + s.connect(&encoder.output, &hex_decoder.input); + + let stdout = s.write_stdout(); + s.connect(&hex_decoder.output, &stdout.input); + }) +} diff --git a/lib/protoflow/examples/delay_output/README.md b/lib/protoflow/examples/delay_output/README.md new file mode 100644 index 00000000..9cc84c18 --- /dev/null +++ b/lib/protoflow/examples/delay_output/README.md @@ -0,0 +1,29 @@ +# Delayed Line Output Example + +This is a simple five-block example program that starts with a constant string +containing multiple lines, splits the string into individual lines, +introduces a delay between processing each line, and writes the delayed lines +to standard output (stdout). + +Note that this program demonstrates how to control the timing of data processing, +making it suitable for simulating real-time output or throttling data streams + +## Block Diagram + +```mermaid +block-beta + columns 13 + Const space:2 SplitString space:2 Delay space:2 Encode space:2 WriteStdout + Const-- "output → input" -->SplitString + SplitString-- "output → input" -->Delay + Delay-- "output → input" -->Encode + Encode-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const block + class SplitString block + class Delay block + class Encode block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/delay_output/main.rs b/lib/protoflow/examples/delay_output/main.rs new file mode 100644 index 00000000..4ae622fd --- /dev/null +++ b/lib/protoflow/examples/delay_output/main.rs @@ -0,0 +1,21 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let input_string = s.const_string("Line 1\nLine 2\nLine 3\nLine 4"); + + let split_string = s.split_string("\n"); + s.connect(&input_string.output, &split_string.input); + + let delay = s.delay(); + s.connect(&split_string.output, &delay.input); + + let encoder = s.encode_lines(); + s.connect(&delay.output, &encoder.input); + + let stdout = s.write_stdout(); + s.connect(&encoder.output, &stdout.input); + }) +} diff --git a/lib/protoflow/examples/encode_hex/README.md b/lib/protoflow/examples/encode_hex/README.md new file mode 100644 index 00000000..531d2fd2 --- /dev/null +++ b/lib/protoflow/examples/encode_hex/README.md @@ -0,0 +1,27 @@ +# Hex Encode Example + +This is a simple four-block example program that starts with a constant string, +encodes it into a byte stream, converts the bytes into hexadecimal format, +and writes the hexadecimal-encoded output to standard output (stdout). + +Note that this program demonstrates how to transform human-readable text into +its hexadecimal representation, which can be useful for encoding data for secure +transmission or storage. + +## Block Diagram + +```mermaid +block-beta + columns 10 + Const space:2 Encode space:2 EncodeHex space:2 WriteStdout + Const-- "output → input" -->Encode + Encode-- "output → input" -->EncodeHex + EncodeHex-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const block + class Encode block + class EncodeHex block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/encode_hex/main.rs b/lib/protoflow/examples/encode_hex/main.rs new file mode 100644 index 00000000..dfd78c79 --- /dev/null +++ b/lib/protoflow/examples/encode_hex/main.rs @@ -0,0 +1,18 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let input_string = s.const_string("The quick brown fox jumps over the lazy dog"); + + let encoder = s.encode_lines(); + s.connect(&input_string.output, &encoder.input); + + let hex_encoder = s.encode_hex(); + s.connect(&encoder.output, &hex_encoder.input); + + let stdout = s.write_stdout(); + s.connect(&hex_encoder.output, &stdout.input); + }) +} diff --git a/lib/protoflow/examples/json_processing/README.md b/lib/protoflow/examples/json_processing/README.md new file mode 100644 index 00000000..18d03740 --- /dev/null +++ b/lib/protoflow/examples/json_processing/README.md @@ -0,0 +1,30 @@ +# JSON Processing Example + +This is a simple five-block example program that starts with a constant string +containing JSON data, encodes it into a byte stream, decodes it back into +structured JSON data, re-encodes that structured data into JSON format, +and writes the re-encoded JSON data to standard output (stdout). + +Note that this program demonstrates the complete processing cycle of JSON data, +ensuring that the data is parsed and re-encoded while preserving its original +structure. + +## Block Diagram + +```mermaid +block-beta + columns 13 + Const space:2 Encode space:2 DecodeJSON space:2 EncodeJSON space:2 WriteStdout + Const-- "output → input" -->Encode + Encode-- "output → input" -->DecodeJSON + DecodeJSON-- "output, → input" -->EncodeJSON + EncodeJSON-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const block + class Encode block + class DecodeJSON block + class EncodeJSON block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/json_processing/main.rs b/lib/protoflow/examples/json_processing/main.rs new file mode 100644 index 00000000..9749ea6e --- /dev/null +++ b/lib/protoflow/examples/json_processing/main.rs @@ -0,0 +1,21 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let json = s.const_string(r#"{"Name": "Alice","Age": 25,"Score": 90}"#); + + let line_encoder = s.encode_lines(); + s.connect(&json.output, &line_encoder.input); + + let decoder = s.decode_json(); + s.connect(&line_encoder.output, &decoder.input); + + let encoder = s.encode_json(); + s.connect(&decoder.output, &encoder.input); + + let stdout = s.write_stdout(); + s.connect(&encoder.output, &stdout.input); + }) +} diff --git a/lib/protoflow/examples/process_env_var/README.md b/lib/protoflow/examples/process_env_var/README.md new file mode 100644 index 00000000..b7ee6f22 --- /dev/null +++ b/lib/protoflow/examples/process_env_var/README.md @@ -0,0 +1,32 @@ +# Environment Variable Processor Example + +This is a simple five-block example program that starts by reading the PATH +environment variable, splits its value into individual components based on +the : delimiter, transforms the components into a newline-separated list, +and writes the resulting list to standard output (stdout). + +Note that this program demonstrates how to process and format environment +variable values, making it suitable for scenarios where structured output +of environment variables is required. + +## Block Diagram + +```mermaid +block-beta + columns 16 + Const space:2 ReadEnv space:2 SplitString space:2 ConcatStrings space:2 Encode space:2 WriteStdout + Const-- "output → name" -->ReadEnv + ReadEnv-- "output → input" -->SplitString + SplitString-- "output, → input" -->ConcatStrings + ConcatStrings-- "output → input" -->Encode + Encode-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const block + class ReadEnv block + class SplitString block + class ConcatStrings block + class Encode block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/process_env_var/main.rs b/lib/protoflow/examples/process_env_var/main.rs new file mode 100644 index 00000000..e0426be8 --- /dev/null +++ b/lib/protoflow/examples/process_env_var/main.rs @@ -0,0 +1,23 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let env_var_name = s.const_string("PATH"); + let read_env = s.read_env(); + s.connect(&env_var_name.output, &read_env.name); + + let split_env = s.split_string(":"); + s.connect(&read_env.output, &split_env.input); + + let transform_paths = s.concat_strings_by("\n"); + s.connect(&split_env.output, &transform_paths.input); + + let line_encoder = s.encode_lines(); + s.connect(&transform_paths.output, &line_encoder.input); + + let write_stdout = s.write_stdout(); + s.connect(&line_encoder.output, &write_stdout.input); + }) +} diff --git a/lib/protoflow/examples/read_file/README.md b/lib/protoflow/examples/read_file/README.md new file mode 100644 index 00000000..f69f0004 --- /dev/null +++ b/lib/protoflow/examples/read_file/README.md @@ -0,0 +1,36 @@ +# Read File Example + +This is a simple seven-block example program that begins with a constant string +containing multiple lines of text, writes the content to a file, reads the file +back into the system, and writes the file's content to standard output (stdout). + +Note that this program is fully self-contained, creating and processing the file +entirely within the flow, making it suitable for demonstrating file handling +in a standalone scenario. + +## Block Diagram + +```mermaid +block-beta + columns 7 + space:3 Const_wpath space:3 + space:6 WriteFile + Const_content space:2 Encode space:3 + space:7 + Const_rpath space:2 ReadFile space:2 WriteStdout + Const_wpath-- "output → path" -->WriteFile + Const_content-- "output → input" -->Encode + Encode-- "output → input" -->WriteFile + Const_rpath-- "output → path" -->ReadFile + ReadFile-- "output → input" -->WriteStdout + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class Const_wpath block + class Const_content block + class Const_rpath block + class Encode block + class WriteFile block + class ReadFile block + class WriteStdout block +``` diff --git a/lib/protoflow/examples/read_file/main.rs b/lib/protoflow/examples/read_file/main.rs new file mode 100644 index 00000000..87f6fe6b --- /dev/null +++ b/lib/protoflow/examples/read_file/main.rs @@ -0,0 +1,23 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let initial_content = s.const_string("This is line 1\nThis is line 2\nThis is line 3"); + let encoder = s.encode_lines(); + s.connect(&initial_content.output, &encoder.input); + + let write_file = s.write_file(); + let write_path = s.const_string("self_contained_file.txt"); + s.connect(&encoder.output, &write_file.input); + s.connect(&write_path.output, &write_file.path); + + let read_path = s.const_string("self_contained_file.txt"); + let read_file = s.read_file(); + s.connect(&read_path.output, &read_file.path); + + let stdout = s.write_stdout(); + s.connect(&read_file.output, &stdout.input); + }) +} diff --git a/lib/protoflow/examples/write_file/README.md b/lib/protoflow/examples/write_file/README.md new file mode 100644 index 00000000..242c0641 --- /dev/null +++ b/lib/protoflow/examples/write_file/README.md @@ -0,0 +1,25 @@ +# Write File Example + +This is a simple three-block example program that reads input from standard input +(stdin) and writes the content directly to a file (`output.txt`). + +Note that this program demonstrates how to capture user input or piped data +and store it persistently in a file. + +## Block Diagram + +```mermaid +block-beta + columns 4 + Const space:3 + space:3 WriteFile + ReadStdin + ReadStdin-- "output → input" -->WriteFile + Const-- "output → path" -->WriteFile + + classDef block height:48px,padding:8px; + classDef hidden visibility:none; + class ReadStdin block + class Const block + class WriteFile block +``` diff --git a/lib/protoflow/examples/write_file/main.rs b/lib/protoflow/examples/write_file/main.rs new file mode 100644 index 00000000..54da2f05 --- /dev/null +++ b/lib/protoflow/examples/write_file/main.rs @@ -0,0 +1,13 @@ +// This is free and unencumbered software released into the public domain. + +use protoflow::{blocks::*, BlockResult}; + +pub fn main() -> BlockResult { + System::run(|s| { + let stdin = s.read_stdin(); + let file_path = s.const_string("output.txt"); + let write_file = s.write_file(); + s.connect(&stdin.output, &write_file.input); + s.connect(&file_path.output, &write_file.path); + }) +} From 04e2117986b8956a21b64b3e2f6291b66f220c43 Mon Sep 17 00:00:00 2001 From: AlexSanches1 Date: Tue, 3 Dec 2024 14:39:10 -0500 Subject: [PATCH 4/6] Fixes by cargo fmt --- examples/json_sanitizer.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/json_sanitizer.rs b/examples/json_sanitizer.rs index 9d1dacd2..d65c8270 100755 --- a/examples/json_sanitizer.rs +++ b/examples/json_sanitizer.rs @@ -10,11 +10,13 @@ use protoflow::{blocks::*, BlockResult}; fn main() -> BlockResult { System::run(|s| { - let json_content = s.const_string(r#"{ + let json_content = s.const_string( + r#"{ "Name": "Alice", "Age": 25, "Score": 90 - }"#); + }"#, + ); let line_encoder = s.encode_lines(); let decoder = s.decode_json(); let encoder = s.encode_json(); From 38803ff4363218bbd6f7ff47897cbf3cf107c538 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:41:38 +0400 Subject: [PATCH 5/6] Update lib/protoflow/examples/process_env_var/main.rs Co-authored-by: Samuel Sarle Signed-off-by: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com> --- lib/protoflow/examples/process_env_var/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/protoflow/examples/process_env_var/main.rs b/lib/protoflow/examples/process_env_var/main.rs index e0426be8..bc6a0254 100644 --- a/lib/protoflow/examples/process_env_var/main.rs +++ b/lib/protoflow/examples/process_env_var/main.rs @@ -11,11 +11,8 @@ pub fn main() -> BlockResult { let split_env = s.split_string(":"); s.connect(&read_env.output, &split_env.input); - let transform_paths = s.concat_strings_by("\n"); - s.connect(&split_env.output, &transform_paths.input); - let line_encoder = s.encode_lines(); - s.connect(&transform_paths.output, &line_encoder.input); + s.connect(&split_env.output, &line_encoder.input); let write_stdout = s.write_stdout(); s.connect(&line_encoder.output, &write_stdout.input); From 5a3198367898465c068e529eb1526b45ef72381c Mon Sep 17 00:00:00 2001 From: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:44:39 +0400 Subject: [PATCH 6/6] Update examples/hash_content.rs Co-authored-by: Samuel Sarle Signed-off-by: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com> --- examples/hash_content.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/hash_content.rs b/examples/hash_content.rs index 9f3be83d..3e5d7daa 100755 --- a/examples/hash_content.rs +++ b/examples/hash_content.rs @@ -13,9 +13,11 @@ fn main() -> BlockResult { let content = s.const_string("Hello, World!"); let line_encoder = s.encode_lines(); let hash_content = s.hash_sha2(); + let encode_hex = s.encode_hex(); let stdout = s.write_stdout(); s.connect(&content.output, &line_encoder.input); s.connect(&line_encoder.output, &hash_content.input); - s.connect(&hash_content.hash, &stdout.input); + s.connect(&hash_content.hash, &encode_hex.input); + s.connect(&encode_hex.output, &stdout.input); }) }