use std::io::{self, Read};
use std::path::Path;
use assura_config::{CompilerConfig, ProjectConfig};
pub(crate) fn load_project_config(
start_path: &Path,
) -> Option<(ProjectConfig, std::path::PathBuf)> {
assura_config::load_project_config(start_path, assura_resolve::find_project_root)
}
pub(crate) fn read_source_arg(path: &str) -> io::Result<(String, String)> {
if path == "-" {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf)?;
Ok((buf, "<stdin>".to_string()))
} else {
let source = std::fs::read_to_string(path)?;
Ok((source, path.to_string()))
}
}
pub(crate) fn is_stdin_arg(path: &str) -> bool {
path == "-"
}
pub(crate) fn validate_human_json_format(format: &str, cmd: &str, as_json: bool) {
match format {
"human" | "json" => {}
other => {
if as_json {
let report = serde_json::json!({
"ok": false,
"command": cmd,
"error": "invalid_format",
"format": other,
"message": format!(
"invalid --format '{other}' for {cmd} (expected human or json)"
),
});
println!("{}", serde_json::to_string_pretty(&report).unwrap());
} else {
eprintln!("Error: invalid --format '{other}' for {cmd} (expected human or json)");
}
std::process::exit(2);
}
}
}
pub(crate) type CompilationResult = assura_pipeline::CompilationOutput;
pub(crate) fn format_counterexample_summary(
counter_model: &Option<assura_smt::CounterexampleModel>,
raw_model: &str,
) -> String {
let lines = assura_smt::display::format_counterexample_lines(counter_model, raw_model);
let pairs: Vec<&str> = lines
.iter()
.map(|l| l.strip_prefix("| ").unwrap_or(l.as_str()))
.collect();
if pairs.is_empty() {
return "counterexample found".to_string();
}
format!("counterexample: {}", pairs.join("; "))
}
pub(crate) fn load_project_deps(
project_dir: &Path,
) -> (
std::path::PathBuf,
assura_resolve::DependencyMap,
Vec<String>,
) {
let project_root = if project_dir.join("assura.toml").exists() {
project_dir.to_path_buf()
} else {
assura_resolve::find_project_root(project_dir).unwrap_or_else(|| project_dir.to_path_buf())
};
let config = load_project_config(&project_root);
let (dep_map, dep_warnings) = if let Some((ref cfg, ref root)) = config {
assura_resolve::resolve_dependency_map(root, cfg)
} else {
(assura_resolve::DependencyMap::new(), vec![])
};
(project_root, dep_map, dep_warnings)
}
pub(crate) fn compile(source: &str, filename: &str) -> CompilationResult {
assura_pipeline::compile(source, filename, &CompilerConfig::default())
}
pub(crate) fn compile_with_config(
source: &str,
filename: &str,
config: &CompilerConfig,
) -> CompilationResult {
assura_pipeline::compile(source, filename, config)
}