1
use std::{path::PathBuf, sync::Arc};
2

            
3
use anyhow::anyhow;
4
use clap::ValueHint;
5

            
6
use conjure_cp_cli::utils::testing::{serialize_domains, serialize_model};
7

            
8
use crate::cli::{GlobalArgs, LOGGING_HELP_HEADING};
9
use crate::solve::{init_context, parse};
10

            
11
#[derive(Clone, Debug, clap::Args)]
12
pub struct Args {
13
    /// The input Essence file
14
    #[arg(value_name = "INPUT_ESSENCE", value_hint = ValueHint::FilePath)]
15
    pub input_file: PathBuf,
16

            
17
    // The format you would like to print out (e.g. ast-json)
18
    #[arg(long, help_heading=LOGGING_HELP_HEADING)]
19
    pub output_format: String,
20
}
21

            
22
14
pub fn run_pretty_command(global_args: GlobalArgs, pretty_args: Args) -> anyhow::Result<()> {
23
    // Preamble
24
14
    let input_file = pretty_args.input_file.clone();
25
14
    let context = init_context(&global_args, input_file, None)?;
26

            
27
    // Get the file path back from the context
28
14
    let ctx_lock = context.read().unwrap();
29
14
    let file = ctx_lock.essence_file_name.as_ref().unwrap();
30

            
31
14
    let model = parse(&global_args, Arc::clone(&context), file)?;
32

            
33
    // Running the correct method to acquire pretty string
34
14
    let output = match pretty_args.output_format.as_str() {
35
14
        "ast-json" => serialize_model(&model),
36
12
        "expression-domains" => serialize_domains(&model),
37
        // "add_new_flag" => method(),
38
        _ => {
39
            return Err(anyhow!(
40
                "Unknown output format {}; supports [ast-json, expression-domains]",
41
                &pretty_args.output_format
42
            ));
43
        }
44
    };
45

            
46
14
    let output = output.map_err(|err| anyhow!("Could not pretty print: {err}"))?;
47
14
    print!("{output}");
48
14
    Ok(())
49
14
}