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

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

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

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

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

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

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

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

            
64
    Ok(generated_mdl)
65
}
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
pub fn get_example_model_by_path(filepath: &str) -> Result<Model, anyhow::Error> {
78
    let essence_path = PathBuf::from(filepath);
79

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

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

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

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

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

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

            
106
    Ok(generated_model)
107
}