processing_chain/
lib.rs

1#![crate_name = "processing_chain"]
2
3//! The `processing-chain` crate provides a convenient way to seamlessly set up processing
4//! chains for large amounts of data.
5//!
6
7use anyhow::{Ok, Result};
8use log::{info, error};
9use process_trait::ProcessingCore;
10use items::Item;
11
12pub mod process_trait;
13pub mod items;
14pub mod processes;
15
16pub fn run_process<P, F>(mut proc: P, f: F) -> Result<P>
17where
18    P: ProcessingCore,
19    F: Fn(&Item) -> Result<bool> + Send + Sync,
20{
21    proc.set_items()?;
22
23    if !proc.check_all_inputs_exist()? {
24        error!("Not all input files exist!");
25    }
26
27    if proc.check_tmp_dir_exist()? {
28        proc.create_tmp_directory()?;
29    }
30
31    if proc.process_items(f)? {
32        info!("All file processed succesfully!");
33    }
34
35    if proc.check_tmp_dir_exist()? {
36        proc.move_files()?;
37    }
38
39    Ok(proc)
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use std::env;
46    use log::info;
47    use std::path::PathBuf;
48    use processes::Process;
49    use processes::json_process::JsonProcess;
50
51    fn _process_item(item: &Item) -> Result<bool> {
52        // define how to process a single item
53        info!(
54            "Processing {} {:?} -> {:?}",
55            item.name, item.input_item_paths, item.output_item_paths
56        );
57        Ok(true)
58    }
59
60    #[test]
61    fn process_default_test() {
62        let proc = Process {
63            name: String::from("Test"),
64            inputs_dir_path: env::current_dir().unwrap(),
65            inputs_extenion: String::from("toml"),
66            ..Process::default()
67        };
68        assert_eq!(proc.overwrite, false);
69        assert_eq!(proc.tmp_dir_path, None);
70        assert_eq!(proc.inputs_extenion, "toml");
71        assert_eq!(proc.outputs_dir_path.to_str().unwrap(), "");
72    }
73
74    #[test]
75    fn run_process_items_test() {
76        let proc = Process {
77            name: String::from("Test"),
78            inputs_dir_path: env::current_dir().unwrap(),
79            inputs_extenion: String::from("toml"),
80            outputs_dir_path: PathBuf::from("Test"),
81            ..Process::default()
82        };
83
84        let proc = run_process(proc, _process_item).unwrap();
85        let first_item = proc.items.first().unwrap();
86        assert_eq!(first_item.name, "file_0");
87        assert_eq!(
88            first_item.input_item_paths.first().unwrap().file_name().unwrap(),
89            "Cargo.toml"
90        );
91        assert_eq!(first_item.input_item_paths.first().unwrap().extension().unwrap(), "toml");
92        assert_eq!(first_item.output_item_paths.first().unwrap().extension().unwrap(), "toml");
93    }
94
95    #[test]
96    fn run_process_empty_items_test() {
97        let proc = Process {
98            name: String::from("Test"),
99            inputs_dir_path: env::current_dir().unwrap(),
100            inputs_extenion: String::from("toml"),
101            outputs_dir_path: env::current_dir().unwrap(),
102            ..Process::default()
103        };
104
105        let proc = run_process(proc, _process_item).unwrap();
106        assert!(proc.items.is_empty());
107    }
108
109    #[test]
110    fn run_process_json() {
111
112        let proc = JsonProcess {
113            name: String::from("JSON process"),
114            json_items: String::from("examples/items.json"),
115            ..JsonProcess::default()
116        };
117        let proc = run_process(proc, _process_item).unwrap();
118        assert_eq!(proc.items.len(), 5);
119
120    }
121
122}