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
use types::*;

use std::fs::read_to_string;

mod types;

/// Returns a vector of BuildFile structs parsed from the given YAML file.
///
/// # Arguments
///
/// * `filename` - The name of the YAML file to parse.
///
/// # Panics
///
/// Panics if the file cannot be read or parsed.
///
/// # Notes
///
/// This function is a wrapper around the serde_yaml::from_str function.
///
/// # See Also
///
/// * [serde_yaml::from_str](https://docs.rs/serde_yaml/0.8.11/serde_yaml/fn.from_str.html)
fn get_build_config(filename: &str) -> BuildConfig {
    let content =
        read_to_string(filename).unwrap_or_else(|_| panic!("Failed to read file: {filename}"));

    serde_yaml::from_str(&content).unwrap_or_else(|_| panic!("Failed to parse file: {filename}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_yaml_file_parsed_correctly() {
        let config = get_build_config("examples/ex1.yaml");

        assert_eq!(config.len(), 1);
        assert_eq!(config[0].name, "main.c");
        assert_eq!(config[0].out, "main");
        assert_eq!(config[0].uses.len(), 1);
        assert_eq!(config[0].uses[0], "aux.c");
        assert_eq!(config[0].headers.len(), 1);
        assert_eq!(config[0].headers[0], "include/aux.h");
        assert_eq!(config[0].includes.len(), 1);
        assert_eq!(config[0].includes[0], "include");
    }
}