Skip to main content

bindings_generat/output/
validator.rs

1use anyhow::{Context, Result};
2use std::path::Path;
3use std::process::Command;
4use tracing::{debug, info, warn};
5
6/// Validate generated code with cargo check
7pub fn validate_code(output_dir: &Path) -> Result<bool> {
8    info!("Validating generated code with cargo check");
9
10    let cargo_toml = output_dir.join("Cargo.toml");
11    if !cargo_toml.exists() {
12        warn!("Cargo.toml not found, skipping validation");
13        return Ok(false);
14    }
15
16    debug!("Running cargo check in {}", output_dir.display());
17
18    let output = Command::new("cargo")
19        .arg("check")
20        .arg("--manifest-path")
21        .arg(&cargo_toml)
22        .arg("--quiet")
23        .output()
24        .context("Failed to run cargo check")?;
25
26    if output.status.success() {
27        info!("✓ Generated code compiles successfully");
28        Ok(true)
29    } else {
30        warn!("Generated code has compilation errors");
31
32        if !output.stderr.is_empty() {
33            let stderr = String::from_utf8_lossy(&output.stderr);
34            debug!("cargo check stderr:\n{}", stderr);
35        }
36
37        Ok(false)
38    }
39}
40
41/// Check if cargo is available
42pub fn is_cargo_available() -> bool {
43    Command::new("cargo")
44        .arg("--version")
45        .output()
46        .map(|output| output.status.success())
47        .unwrap_or(false)
48}