rustquty-core 0.4.1

Core library for rustquty, a local-first quality scanner for Rust projects
Documentation
//! Deny collector — runs `cargo deny check`.

use super::{Collector, CollectorError, CollectorOutput};
use crate::context::Context;
use std::process::Command;

pub struct DenyCollector;

impl DenyCollector {
    pub fn new() -> Self {
        Self
    }

    fn parse_json_output(&self, stdout: &str) -> (u32, u32) {
        let mut banned_count = 0u32;
        let mut license_violations = 0u32;

        if let Ok(json) = serde_json::from_str::<serde_json::Value>(stdout) {
            if let Some(ban) = json.get("ban").and_then(|v| v.get("list"))
                && let Some(arr) = ban.as_array()
            {
                banned_count = arr.len() as u32;
            }
            if let Some(license) = json.get("license").and_then(|v| v.get("violations"))
                && let Some(arr) = license.as_array()
            {
                license_violations = arr.len() as u32;
            }
        }

        (banned_count, license_violations)
    }
}

impl Collector for DenyCollector {
    fn name(&self) -> &'static str {
        "deny"
    }

    fn is_available(&self) -> bool {
        Command::new("cargo-deny")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn collect(&self, ctx: &Context) -> Result<CollectorOutput, CollectorError> {
        let start = std::time::Instant::now();
        let output = Command::new("cargo-deny")
            .args(["check", "--format=json"])
            .current_dir(&ctx.workspace_root)
            .output()
            .map_err(|e| CollectorError::IoError(e.to_string()))?;

        let duration_ms = start.elapsed().as_millis() as u64;
        let raw_stdout = String::from_utf8_lossy(&output.stdout).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();

        let (banned_count, license_violations) = self.parse_json_output(&raw_stdout);
        let status = if output.status.success() {
            crate::schema::CollectorStatus::Pass
        } else {
            crate::schema::CollectorStatus::Fail
        };

        let details = serde_json::json!({
            "bannedCount": banned_count,
            "licenseViolations": license_violations,
        });

        Ok(CollectorOutput {
            status,
            duration_ms,
            stdout: serde_json::to_string(&details).unwrap_or_default(),
            stderr,
        })
    }
}

impl Default for DenyCollector {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_json_output_empty() {
        let collector = DenyCollector::new();
        let (banned, license) = collector.parse_json_output("{}");
        assert_eq!(banned, 0);
        assert_eq!(license, 0);
    }
}