protoflow_blocks/blocks/sys/
read_dir.rs1extern crate std;
4
5use crate::{
6 prelude::{vec, String, ToString},
7 StdioConfig, StdioError, StdioSystem, System,
8};
9use protoflow_core::{Block, BlockResult, BlockRuntime, InputPort, OutputPort};
10use protoflow_derive::Block;
11use simple_mermaid::mermaid;
12
13#[doc = mermaid!("../../../doc/sys/read_dir.mmd")]
17#[doc = mermaid!("../../../doc/sys/read_dir.seq.mmd" framed)]
20#[derive(Block, Clone)]
47pub struct ReadDir {
48 #[input]
50 pub path: InputPort<String>,
51
52 #[output]
54 pub output: OutputPort<String>,
55}
56
57impl ReadDir {
58 pub fn new(path: InputPort<String>, output: OutputPort<String>) -> Self {
59 Self { path, output }
60 }
61
62 pub fn with_system(system: &System) -> Self {
63 use crate::SystemBuilding;
64 Self::new(system.input(), system.output())
65 }
66}
67
68impl Block for ReadDir {
69 fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult {
70 runtime.wait_for(&self.path)?;
71 let dir_path = self.path.recv()?.unwrap();
72 let dir = std::fs::read_dir(dir_path)?;
75 for dir_entry in dir {
76 let file_path = dir_entry?.path();
77 let file_path = file_path.to_string_lossy().to_string();
79 self.output.send(&file_path)?;
80 }
81
82 self.output.close()?;
83 Ok(())
84 }
85}
86
87#[cfg(feature = "std")]
88impl StdioSystem for ReadDir {
89 fn build_system(config: StdioConfig) -> Result<System, StdioError> {
90 use crate::{CoreBlocks, IoBlocks, SysBlocks, SystemBuilding};
91
92 config.allow_only(vec!["path"])?;
93 let path = config.get_string("path")?;
94
95 Ok(System::build(|s| {
96 let path_param = s.const_string(path);
97 let dir_reader = s.read_dir();
98 let line_encoder = s.encode_with(config.encoding);
99 let stdout = config.write_stdout(s);
100 s.connect(&path_param.output, &dir_reader.path);
101 s.connect(&dir_reader.output, &line_encoder.input);
102 s.connect(&line_encoder.output, &stdout.input);
103 }))
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::ReadDir;
110 use crate::{System, SystemBuilding};
111
112 #[test]
113 fn instantiate_block() {
114 let _ = System::build(|s| {
116 let _ = s.block(ReadDir::new(s.input(), s.output()));
117 });
118 }
119}