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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::collections::HashMap;

// // TODO
// pub struct EvaluateOptions {
//     numeric: HashMap<String, Box<f64>>,
//     textual: HashMap<String, String>,
//     boolean: HashMap<String, Box<bool>>
// }

// // TODO
// pub struct IntermediateHooks {
//     before_evaulate_triggers: Vec<String>,
//     after_evaulate_triggers: Vec<String>
// }

#[derive(Debug)]
pub struct FilePatternOptions {
    pub exclude_patterns: Vec<String>,
    pub skip_files: Vec<String>,
    pub extensions: Vec<String>,
}

fn read_yaml(file: &str) -> Result<serde_yaml::Value, Box<dyn std::error::Error>> {
    let f = std::fs::File::open(file)?;
    Ok(serde_yaml::from_reader(f)?)
}

pub fn parse(
    file: &str,
) -> (
    HashMap<String, Box<f64>>,
    HashMap<String, String>,
    HashMap<String, Box<bool>>,
    Vec<String>,
    Vec<String>,
    FilePatternOptions,
) {
    let yaml_file = read_yaml(file).unwrap();

    // options that can be represented as a number
    let mut number_options: HashMap<String, Box<f64>> = HashMap::new();
    // options that can be represented as text
    let mut text_options: HashMap<String, String> = HashMap::new();
    // options that can be represented as boolean
    let mut bool_options: HashMap<String, Box<bool>> = HashMap::new();
    // trigger before evaluate
    let mut before_evaulate_triggers: Vec<String> = Vec::new();
    // trigger after evaluate
    let mut after_evaluate_triggers: Vec<String> = Vec::new();
    // options that fit in a vector
    let mut exclude_patterns: Vec<String> = Vec::new();
    let mut skip_files: Vec<String> = Vec::new();
    let mut extensions: Vec<String> = Vec::new();

    // parsing the standard sections
    for section in vec!["load_options", "preprocess", "evaluate"].iter() {
        for feature in yaml_file[section].as_sequence().iter() {
            for entities in feature.iter() {
                let load_option: serde_yaml::Value = serde_yaml::to_value(entities).unwrap();
                if let serde_yaml::Value::Mapping(options) = load_option {
                    for option in options.iter() {
                        match option {
                            (serde_yaml::Value::String(key), serde_yaml::Value::Number(val)) => {
                                number_options
                                    .insert(key.to_string(), Box::new(val.as_f64().unwrap()));
                            }
                            (serde_yaml::Value::String(key), serde_yaml::Value::String(val)) => {
                                text_options.insert(key.to_string(), val.to_string());
                            }
                            (serde_yaml::Value::String(key), serde_yaml::Value::Bool(val)) => {
                                bool_options.insert(key.to_string(), Box::new(*val));
                            }
                            (serde_yaml::Value::String(key), serde_yaml::Value::Sequence(seq)) => {
                                match key.as_str() {
                                    "exclude_patterns" => {
                                        seq.iter()
                                            .filter_map(|d| match d {
                                                serde_yaml::Value::String(string) => {
                                                    Some(string.to_owned())
                                                }
                                                _ => None,
                                            })
                                            .collect::<Vec<String>>()
                                            .as_slice()
                                            .clone_into(&mut exclude_patterns);
                                    }
                                    "skip" => {
                                        seq.iter()
                                            .filter_map(|d| match d {
                                                serde_yaml::Value::String(string) => {
                                                    Some(string.to_owned())
                                                }
                                                _ => None,
                                            })
                                            .collect::<Vec<String>>()
                                            .as_slice()
                                            .clone_into(&mut skip_files);
                                    }
                                    "extensions" => {
                                        seq.iter()
                                            .filter_map(|d| match d {
                                                serde_yaml::Value::String(string) => {
                                                    Some(string.to_owned())
                                                }
                                                _ => None,
                                            })
                                            .collect::<Vec<String>>()
                                            .as_slice()
                                            .clone_into(&mut extensions);
                                    }
                                    _ => {}
                                }
                            }
                            _ => panic!(
                                "yaml contains values that are unknown in this context: {:?}",
                                option
                            ),
                        }
                    }
                }
            }
        }
    }
    // parsing the "before_evaluate" section for subcommands to run
    for commands in yaml_file["before_evaluate"].as_sequence().iter() {
        for command in commands.iter() {
            if let serde_yaml::Value::String(cmd) = command {
                before_evaulate_triggers.push(cmd.to_string());
            }
        }
    }

    // parsing the "after_evaluate" section for subcommands to run
    for commands in yaml_file["after_evaluate"].as_sequence().iter() {
        for command in commands.iter() {
            if let serde_yaml::Value::String(cmd) = command {
                after_evaluate_triggers.push(cmd.to_string());
            }
        }
    }

    // getting the method section
    for commands in yaml_file["method"].as_sequence().iter() {
        for command in commands.iter() {
            if let serde_yaml::Value::String(cmd) = command {
                match cmd.to_string().as_str() {
                    "fft" => {
                        text_options.insert(String::from("methodname"), String::from("FFTMethod"))
                    }
                    "wft" => {
                        text_options.insert(String::from("methodname"), String::from("WFTMethod"))
                    }
                    "mm" => text_options
                        .insert(String::from("methodname"), String::from("MinMaxMethod")),
                    _ => panic!("method named {:?} is not implemented", cmd),
                };
            }
        }
    }

    for commands in yaml_file["method_details"].as_sequence().iter() {
        for command in commands.iter() {
            match command {
                serde_yaml::Value::String(cmd) => {
                    bool_options.insert(cmd.to_string(), Box::new(true));
                }
                serde_yaml::Value::Mapping(options) => {
                    for option in options.iter() {
                        match option {
                            (serde_yaml::Value::String(key), serde_yaml::Value::Number(val)) => {
                                number_options
                                    .insert(key.to_string(), Box::new(val.as_f64().unwrap()));
                            }
                            (serde_yaml::Value::String(key), serde_yaml::Value::String(val)) => {
                                text_options.insert(key.to_string(), val.to_string());
                            }
                            (serde_yaml::Value::String(key), serde_yaml::Value::Bool(val)) => {
                                bool_options.insert(key.to_string(), Box::new(*val));
                            }
                            _ => panic!(
                                "yaml contains values that are unknown in this context: {:?}",
                                option
                            ),
                        }
                    }
                }
                _ => {}
            }
        }
    }

    (
        number_options,
        text_options,
        bool_options,
        before_evaulate_triggers,
        after_evaluate_triggers,
        FilePatternOptions {
            exclude_patterns,
            skip_files,
            extensions,
        },
    )
}