use std::time::Duration;
#[derive(Debug, Clone)]
pub struct VerificationResult {
pub name: String,
pub command: String,
pub passed: bool,
pub output: String,
pub exit_code: Option<i32>,
pub duration: Duration,
}
impl VerificationResult {
pub fn passed(name: impl Into<String>, command: impl Into<String>, output: String, duration: Duration) -> Self {
Self {
name: name.into(),
command: command.into(),
passed: true,
output,
exit_code: Some(0),
duration,
}
}
pub fn failed(
name: impl Into<String>,
command: impl Into<String>,
output: String,
exit_code: Option<i32>,
duration: Duration,
) -> Self {
Self {
name: name.into(),
command: command.into(),
passed: false,
output,
exit_code,
duration,
}
}
}
pub fn format_verification_errors(results: &[VerificationResult]) -> String {
let failed: Vec<_> = results.iter().filter(|r| !r.passed).collect();
if failed.is_empty() {
return String::new();
}
let mut message = String::new();
for result in failed {
message.push_str(&format!(
"VERIFICATION FAILED: {}\n\n",
result.name.to_uppercase()
));
message.push_str(&format!("Command: $ {}\n", result.command));
if let Some(code) = result.exit_code {
message.push_str(&format!("Exit code: {}\n", code));
}
message.push_str("\nOutput:\n```\n");
message.push_str(&result.output);
message.push_str("\n```\n\n");
}
message.push_str("Please fix the errors above and try again.");
message
}
pub fn format_verification_status(attempt: u32, max_attempts: u32, results: &[VerificationResult]) -> String {
let failed_count = results.iter().filter(|r| !r.passed).count();
if failed_count == 0 {
"All verification checks passed".to_string()
} else {
format!(
"Verification failed (attempt {}/{}): {} check(s) failed",
attempt, max_attempts, failed_count
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_verification_result_creation() {
let passed = VerificationResult::passed(
"build",
"cargo build",
"Compiling...".to_string(),
Duration::from_secs(5),
);
assert!(passed.passed);
assert_eq!(passed.exit_code, Some(0));
let failed = VerificationResult::failed(
"test",
"cargo test",
"test failed".to_string(),
Some(1),
Duration::from_secs(10),
);
assert!(!failed.passed);
assert_eq!(failed.exit_code, Some(1));
}
#[test]
fn test_format_verification_errors() {
let results = vec![
VerificationResult::passed("build", "cargo build", "ok".to_string(), Duration::from_secs(1)),
VerificationResult::failed(
"test",
"cargo test",
"error: test failed".to_string(),
Some(1),
Duration::from_secs(2),
),
];
let message = format_verification_errors(&results);
assert!(message.contains("VERIFICATION FAILED: TEST"));
assert!(message.contains("cargo test"));
assert!(message.contains("error: test failed"));
assert!(message.contains("Exit code: 1"));
assert!(message.contains("Please fix the errors"));
}
#[test]
fn test_format_verification_errors_empty() {
let results = vec![
VerificationResult::passed("build", "cargo build", "ok".to_string(), Duration::from_secs(1)),
];
let message = format_verification_errors(&results);
assert!(message.is_empty());
}
#[test]
fn test_format_verification_status() {
let failed_results = vec![
VerificationResult::failed("build", "cargo build", "error".to_string(), Some(1), Duration::from_secs(1)),
];
let status = format_verification_status(1, 3, &failed_results);
assert!(status.contains("attempt 1/3"));
assert!(status.contains("1 check(s) failed"));
let passed_results = vec![
VerificationResult::passed("build", "cargo build", "ok".to_string(), Duration::from_secs(1)),
];
let status = format_verification_status(1, 3, &passed_results);
assert!(status.contains("passed"));
}
}