Skip to main content

conjure_cp/
find_conjure.rs

1//! Locate and validate the optional external Conjure parser.
2
3use anyhow::{Result, anyhow, bail};
4use versions::Versioning;
5
6const CONJURE_MIN_VERSION: &str = "2.6.1"; // v2.6.1
7const CORRECT_FIRST_LINE: &str = "Conjure: The Automated Constraint Modelling Tool";
8
9/// Checks if the conjure executable is present in PATH and if it is the correct version.
10/// Returns () on success and an error on failure.
11pub fn conjure_executable() -> Result<()> {
12    let mut cmd = std::process::Command::new("conjure");
13    let output = cmd.arg("--version").output()?;
14    let stdout = String::from_utf8(output.stdout)?;
15    let stderr = String::from_utf8(output.stderr)?;
16
17    if !stderr.is_empty() {
18        bail!("'conjure' results in error: ".to_string() + &stderr);
19    }
20    let first = stdout
21        .lines()
22        .next()
23        .ok_or(anyhow!("Could not read stdout"))?;
24    if first != CORRECT_FIRST_LINE {
25        let path = std::env::var("PATH")?;
26        let paths = std::env::split_paths(&path);
27        let num_conjures = paths.filter(|path| path.join("conjure").exists()).count();
28        if num_conjures > 1 {
29            bail!(
30                "Conjure may be present in PATH after a conflicting name. \
31            Make sure to prepend the correct path to Conjure to PATH."
32            )
33        } else {
34            bail!("The correct Conjure executable is not present in PATH.")
35        }
36    }
37
38    let version_line = stdout
39        .lines()
40        .nth(1)
41        .ok_or(anyhow!("Could not read Conjure's stdout"))?;
42    let version_and_repo = version_line.strip_prefix("Conjure v").ok_or(anyhow!(
43        "Could not read Conjure's version from: {version_line}"
44    ))?;
45    let (version, _) = version_and_repo
46        .split_once(" (Repository version ")
47        .ok_or(anyhow!(
48            "Could not read Conjure's version from: {version_line}"
49        ))?;
50
51    if Versioning::new(version) < Versioning::new(CONJURE_MIN_VERSION) {
52        bail!("Conjure version is too old (< {CONJURE_MIN_VERSION}): {version}");
53    }
54    Ok(())
55}