1
// example_models with get_example_model function
2

            
3
use std::path::PathBuf;
4

            
5
use project_root::get_project_root;
6
use walkdir::WalkDir;
7

            
8
use crate::Model;
9
use crate::parse::model_from_json;
10

            
11
/// Searches recursively in `test-suite/tests/exhaustive` for an `.essence` file matching the given
12
/// filename, then uses conjure to process it into astjson, and returns the parsed model.
13
///
14
/// # Arguments
15
///
16
/// * `filename` - A string slice that holds filename without extension
17
///
18
/// # Returns
19
///
20
/// Function returns a `Result<Value, anyhow::Error>`, where `Value` is the parsed model.
21
62
pub fn get_example_model(filename: &str) -> Result<Model, anyhow::Error> {
22
    // define relative path -> exhaustive tests dir
23
62
    let base_dir = get_project_root()?.join("test-suite/tests/exhaustive");
24
62
    let mut essence_path = PathBuf::new();
25

            
26
    // walk through directory tree recursively starting at base
27
62
    for entry in WalkDir::new(base_dir)
28
62
        .sort_by_file_name()
29
62
        .into_iter()
30
2870507
        .filter_map(|e| e.ok())
31
    {
32
2870507
        let path = entry.path();
33
2870507
        if path.is_file()
34
2845955
            && path
35
2845955
                .extension()
36
2845800
                .is_some_and(|e| e == "essence" || e == "eprime")
37
22134
            && path.file_stem() == Some(std::ffi::OsStr::new(filename))
38
        {
39
31
            essence_path = path.to_path_buf();
40
31
            break;
41
2870476
        }
42
    }
43

            
44
    //println!("PATH TO FILE: {}", essence_path.display());
45

            
46
    // return error if file not found
47
62
    if essence_path.as_os_str().is_empty() {
48
31
        return Err(anyhow::Error::new(std::io::Error::new(
49
31
            std::io::ErrorKind::NotFound,
50
31
            "ERROR: File not found in any subdirectory",
51
31
        )));
52
31
    }
53

            
54
    // let path = PathBuf::from(format!("../tests/exhaustive/basic/comprehension{}.essence", filename));
55
31
    let mut cmd = std::process::Command::new("conjure");
56
31
    let output = cmd
57
31
        .arg("pretty")
58
31
        .arg("--output-format=astjson")
59
31
        .arg(essence_path)
60
31
        .output()?;
61

            
62
    // convert Conjure's stdout from bytes to string
63
31
    let astjson = String::from_utf8(output.stdout)?;
64

            
65
    //println!("ASTJSON: {}", astjson);
66

            
67
    // parse AST JSON from desired Model format
68
31
    let generated_mdl = model_from_json(&astjson, Default::default())?;
69

            
70
31
    Ok(generated_mdl)
71
62
}
72

            
73
/// Searches for an `.essence` file at the given filepath,
74
/// then uses conjure to process it into astjson, and returns the parsed model.
75
///
76
/// # Arguments
77
///
78
/// * `filepath` - A string slice that holds the full file path
79
///
80
/// # Returns
81
///
82
/// Function returns a `Result<Value, anyhow::Error>`, where `Value` is the parsed model
83
62
pub fn get_example_model_by_path(filepath: &str) -> Result<Model, anyhow::Error> {
84
62
    let essence_path = PathBuf::from(filepath);
85

            
86
    // return error if file not found
87
62
    if essence_path.as_os_str().is_empty() {
88
31
        return Err(anyhow::Error::new(std::io::Error::new(
89
31
            std::io::ErrorKind::NotFound,
90
31
            "ERROR: File not found in any subdirectory",
91
31
        )));
92
31
    }
93

            
94
    // println!("PATH TO FILE: {}", essence_path.display());
95

            
96
    // Command execution using 'conjure' CLI tool with provided path
97
31
    let mut cmd = std::process::Command::new("conjure");
98
31
    let output = cmd
99
31
        .arg("pretty")
100
31
        .arg("--output-format=astjson")
101
31
        .arg(&essence_path)
102
31
        .output()?;
103

            
104
    // convert Conjure's stdout from bytes to string
105
31
    let astjson = String::from_utf8(output.stdout)?;
106

            
107
    // println!("ASTJSON: {}", astjson);
108

            
109
    // parse AST JSON into the desired Model format
110
31
    let generated_model = model_from_json(&astjson, Default::default())?;
111

            
112
31
    Ok(generated_model)
113
62
}