feat: complete rule evidence and frozen review fixtures

This commit is contained in:
cflmy committed 2026-11-23 19:10:56 +08:00
1 parent 1f4e120a53
commit 344ef28870
27 files changed
+1992 -356

No files matched your search

+73 -4
View File
@@ -4,6 +4,7 @@ use std::{
process::{Command, Output},
};
use serde::Deserialize;
use serde_json::Value;
use tempfile::NamedTempFile;
@@ -19,6 +20,44 @@ pub struct CheckOutcome {
pub success: bool,
pub analysis: Option<Value>,
pub output: String,
pub diagnostics: Vec<CheckDiagnostic>,
}
#[derive(Debug, Deserialize)]
pub struct CheckDiagnostic {
pub code: String,
pub severity: String,
pub file: Option<PathBuf>,
pub line: Option<usize>,
pub column: Option<usize>,
pub message: String,
#[serde(default)]
pub hints: Vec<String>,
}
impl CheckDiagnostic {
fn render(&self) -> String {
let location = self
.file
.as_ref()
.map(|path| {
format!(
"{}:{}:{}: ",
path.display(),
self.line.unwrap_or(1),
self.column.unwrap_or(1)
)
})
.unwrap_or_default();
let mut text = format!(
"{}[{}]: {location}{}",
self.severity, self.code, self.message
);
for hint in &self.hints {
text.push_str(&format!("\n hint: {hint}"));
}
text
}
}
impl MarqdoRunner {
@@ -42,7 +81,10 @@ impl MarqdoRunner {
return Err(LintError::Core(format_output(&output)));
}
let version = String::from_utf8_lossy(&output.stdout);
let version = version.trim().strip_prefix("marqdo ").unwrap_or(version.trim());
let version = version
.trim()
.strip_prefix("marqdo ")
.unwrap_or(version.trim());
let major = version
.split('.')
.next()
@@ -69,7 +111,9 @@ impl MarqdoRunner {
let value: Value = serde_json::from_slice(&output.stdout)
.map_err(|error| LintError::Protocol(format!("inspect AST JSON 无效:{error}")))?;
if value.get("command").and_then(Value::as_str) != Some("inspect") {
return Err(LintError::Protocol("inspect 输出缺少 command=inspect".into()));
return Err(LintError::Protocol(
"inspect 输出缺少 command=inspect".into(),
));
}
value
.get("ast")
@@ -82,16 +126,27 @@ impl MarqdoRunner {
let output = Command::new(&self.binary)
.arg("check")
.arg(file)
.arg("--json")
.arg("--emit")
.arg(temporary.path())
.output()
.map_err(|error| io(&self.binary, error))?;
let text = format_output(&output);
if !output.status.success() {
let diagnostics = check_diagnostics(&output.stdout);
let text = if diagnostics.is_empty() {
format_output(&output)
} else {
diagnostics
.iter()
.map(CheckDiagnostic::render)
.collect::<Vec<_>>()
.join("\n")
};
return Ok(CheckOutcome {
success: false,
analysis: None,
output: text,
diagnostics,
});
}
let bytes = fs::read(temporary.path()).map_err(|error| io(temporary.path(), error))?;
@@ -100,7 +155,8 @@ impl MarqdoRunner {
Ok(CheckOutcome {
success: true,
analysis: Some(analysis),
output: text,
output: String::new(),
diagnostics: Vec::new(),
})
}
@@ -122,3 +178,16 @@ fn format_output(output: &Output) -> String {
(true, true) => format!("核心退出状态 {}", output.status),
}
}
fn check_diagnostics(stdout: &[u8]) -> Vec<CheckDiagnostic> {
#[derive(Deserialize)]
struct CheckJson {
command: String,
diagnostics: Vec<CheckDiagnostic>,
}
serde_json::from_slice::<CheckJson>(stdout)
.ok()
.filter(|report| report.command == "check")
.map_or_else(Vec::new, |report| report.diagnostics)
}