1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#![crate_name = "processing_chain"]

//! The `processing-chain` crate provides a convenient way to seamlessly set up processing
//! chains for large amounts of data.
//!

use anyhow::{Ok, Result};
use log::{info, error};
use process_trait::ProcessingCore;
use items::Item;

pub mod process_trait;
pub mod items;
pub mod processes;

pub fn run_process<P, F>(mut proc: P, f: F) -> Result<P>
where
    P: ProcessingCore,
    F: Fn(&Item) -> Result<bool> + Send + Sync,
{
    proc.set_items()?;

    if !proc.check_all_inputs_exist()? {
        error!("Not all input files exist!");
    }

    if proc.check_tmp_dir_exist()? {
        proc.create_tmp_directory()?;
    }

    if proc.process_items(f)? {
        info!("All file processed succesfully!");
    }

    if proc.check_tmp_dir_exist()? {
        proc.move_files()?;
    }

    Ok(proc)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use log::info;
    use std::path::PathBuf;
    use processes::Process;
    use processes::json_process::JsonProcess;

    fn _process_item(item: &Item) -> Result<bool> {
        // define how to process a single item
        info!(
            "Processing {} {:?} -> {:?}",
            item.name, item.input_item_paths, item.output_item_paths
        );
        Ok(true)
    }

    #[test]
    fn process_default_test() {
        let proc = Process {
            name: String::from("Test"),
            inputs_dir_path: env::current_dir().unwrap(),
            inputs_extenion: String::from("toml"),
            ..Process::default()
        };
        assert_eq!(proc.overwrite, false);
        assert_eq!(proc.tmp_dir_path, None);
        assert_eq!(proc.inputs_extenion, "toml");
        assert_eq!(proc.outputs_dir_path.to_str().unwrap(), "");
    }

    #[test]
    fn run_process_items_test() {
        let proc = Process {
            name: String::from("Test"),
            inputs_dir_path: env::current_dir().unwrap(),
            inputs_extenion: String::from("toml"),
            outputs_dir_path: PathBuf::from("Test"),
            ..Process::default()
        };

        let proc = run_process(proc, _process_item).unwrap();
        let first_item = proc.items.first().unwrap();
        assert_eq!(first_item.name, "file_0");
        assert_eq!(
            first_item.input_item_paths.first().unwrap().file_name().unwrap(),
            "Cargo.toml"
        );
        assert_eq!(first_item.input_item_paths.first().unwrap().extension().unwrap(), "toml");
        assert_eq!(first_item.output_item_paths.first().unwrap().extension().unwrap(), "toml");
    }

    #[test]
    fn run_process_empty_items_test() {
        let proc = Process {
            name: String::from("Test"),
            inputs_dir_path: env::current_dir().unwrap(),
            inputs_extenion: String::from("toml"),
            outputs_dir_path: env::current_dir().unwrap(),
            ..Process::default()
        };

        let proc = run_process(proc, _process_item).unwrap();
        assert!(proc.items.is_empty());
    }

    #[test]
    fn run_process_json() {

        let proc = JsonProcess {
            name: String::from("JSON process"),
            json_items: String::from("examples/items.json"),
            ..JsonProcess::default()
        };
        let proc = run_process(proc, _process_item).unwrap();
        assert_eq!(proc.items.len(), 5);

    }

}