fastsim_core/
resources.rs

1use include_dir::{include_dir, Dir};
2pub const RESOURCES_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources");
3
4/// List the available resources in the resources directory
5/// - subdir: &str, a subdirectory to choose from the resources directory
6///
7/// NOTE: if subdir cannot be resolved, returns an empty list
8/// RETURNS: a vector of strings for resources that can be loaded
9pub fn list_resources(subdir: &str) -> Vec<String> {
10    if subdir.is_empty() {
11        Vec::<String>::new()
12    } else if let Some(resources_path) = RESOURCES_DIR.get_dir(subdir) {
13        let mut file_names: Vec<String> = resources_path
14            .files()
15            .filter_map(|entry| entry.path().file_name()?.to_str().map(String::from))
16            .collect();
17        file_names.sort();
18        file_names
19    } else {
20        Vec::<String>::new()
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn test_list_resources() {
30        let result = list_resources("cycles");
31        assert!(result.len() == 2);
32        assert!(result[0] == "hwfet.csv");
33        // NOTE: at the time of writing this test, there is no
34        // vehicles subdirectory. The agreed-upon behavior in
35        // that case is that list_resources should return an
36        // empty vector of string.
37        let another_result = list_resources("vehicles");
38        assert!(another_result.len() == 5);
39    }
40}