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::parse::model_from_json;
9
use crate::Model;
10

            
11
/// Searches recursively in `../tests/integration` folder 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
45
pub fn get_example_model(filename: &str) -> Result<Model, anyhow::Error> {
22
    // define relative path -> integration tests dir
23
45
    let base_dir = get_project_root()?;
24
45
    let mut essence_path = PathBuf::new();
25

            
26
    // walk through directory tree recursively starting at base
27
154125
    for entry in WalkDir::new(base_dir).into_iter().filter_map(|e| e.ok()) {
28
154125
        let path = entry.path();
29
154125
        if path.is_file()
30
132180
            && path.extension().map_or(false, |e| e == "essence")
31
555
            && path.file_stem() == Some(std::ffi::OsStr::new(filename))
32
        {
33
30
            essence_path = path.to_path_buf();
34
30
            break;
35
154095
        }
36
    }
37

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

            
40
    // return error if file not found
41
45
    if essence_path.as_os_str().is_empty() {
42
15
        return Err(anyhow::Error::new(std::io::Error::new(
43
15
            std::io::ErrorKind::NotFound,
44
15
            "ERROR: File not found in any subdirectory",
45
15
        )));
46
30
    }
47
30

            
48
30
    // let path = PathBuf::from(format!("../tests/integration/basic/comprehension{}.essence", filename));
49
30
    let mut cmd = std::process::Command::new("conjure");
50
30
    let output = cmd
51
30
        .arg("pretty")
52
30
        .arg("--output-format=astjson")
53
30
        .arg(essence_path)
54
30
        .output()?;
55

            
56
    // convert Conjure's stdout from bytes to string
57
30
    let astjson = String::from_utf8(output.stdout)?;
58

            
59
    //println!("ASTJSON: {}", astjson);
60

            
61
    // parse AST JSON from desired Model format
62
30
    let generated_mdl = model_from_json(&astjson, Default::default())?;
63

            
64
30
    Ok(generated_mdl)
65
45
}
66

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

            
80
30
    // return error if file not found
81
30
    if essence_path.as_os_str().is_empty() {
82
15
        return Err(anyhow::Error::new(std::io::Error::new(
83
15
            std::io::ErrorKind::NotFound,
84
15
            "ERROR: File not found in any subdirectory",
85
15
        )));
86
15
    }
87
15

            
88
15
    // println!("PATH TO FILE: {}", essence_path.display());
89
15

            
90
15
    // Command execution using 'conjure' CLI tool with provided path
91
15
    let mut cmd = std::process::Command::new("conjure");
92
15
    let output = cmd
93
15
        .arg("pretty")
94
15
        .arg("--output-format=astjson")
95
15
        .arg(&essence_path)
96
15
        .output()?;
97

            
98
    // convert Conjure's stdout from bytes to string
99
15
    let astjson = String::from_utf8(output.stdout)?;
100

            
101
    // println!("ASTJSON: {}", astjson);
102

            
103
    // parse AST JSON into the desired Model format
104
15
    let generated_model = model_from_json(&astjson, Default::default())?;
105

            
106
15
    Ok(generated_model)
107
30
}