|
| 1 | +//! 报告产物 freshness:陈旧或伪造空成功报告不能通过 |
| 2 | +//! |
| 3 | +//! 验收标准:scope/rule 执行覆盖核对 |
| 4 | +
|
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +/// 报告状态 |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 9 | +pub enum ReportStatus { |
| 10 | + /// 新鲜 |
| 11 | + Fresh, |
| 12 | + /// 陈旧 |
| 13 | + Stale, |
| 14 | + /// 伪造 |
| 15 | + Forged, |
| 16 | +} |
| 17 | + |
| 18 | +/// 报告元数据 |
| 19 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 20 | +pub struct ReportMetadata { |
| 21 | + /// 报告 ID |
| 22 | + pub id: String, |
| 23 | + /// 创建时间 |
| 24 | + pub created_at: u64, |
| 25 | + /// 过期时间 |
| 26 | + pub expires_at: u64, |
| 27 | + /// 覆盖的规则 |
| 28 | + pub rules_covered: Vec<String>, |
| 29 | + /// 覆盖的范围 |
| 30 | + pub scope_covered: Vec<String>, |
| 31 | +} |
| 32 | + |
| 33 | +/// Freshness 检查结果 |
| 34 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 35 | +pub struct FreshnessResult { |
| 36 | + /// 报告状态 |
| 37 | + pub status: ReportStatus, |
| 38 | + /// 是否通过 |
| 39 | + pub passed: bool, |
| 40 | + /// 原因 |
| 41 | + pub reason: Option<String>, |
| 42 | +} |
| 43 | + |
| 44 | +/// 报告 freshness 检查器 |
| 45 | +pub struct ReportFreshnessChecker; |
| 46 | + |
| 47 | +impl ReportFreshnessChecker { |
| 48 | + /// 检查报告 freshness |
| 49 | + pub fn check( |
| 50 | + metadata: &ReportMetadata, |
| 51 | + current_time: u64, |
| 52 | + expected_rules: &[String], |
| 53 | + expected_scope: &[String], |
| 54 | + ) -> FreshnessResult { |
| 55 | + // 检查是否陈旧 |
| 56 | + if current_time > metadata.expires_at { |
| 57 | + return FreshnessResult { |
| 58 | + status: ReportStatus::Stale, |
| 59 | + passed: false, |
| 60 | + reason: Some("report_expired".into()), |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // 检查是否伪造空成功 |
| 65 | + if metadata.rules_covered.is_empty() && metadata.scope_covered.is_empty() { |
| 66 | + return FreshnessResult { |
| 67 | + status: ReportStatus::Forged, |
| 68 | + passed: false, |
| 69 | + reason: Some("empty_success_forged".into()), |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + // 检查覆盖范围 |
| 74 | + let rules_covered = expected_rules.iter() |
| 75 | + .all(|r| metadata.rules_covered.contains(r)); |
| 76 | + let scope_covered = expected_scope.iter() |
| 77 | + .all(|s| metadata.scope_covered.contains(s)); |
| 78 | + |
| 79 | + if !rules_covered || !scope_covered { |
| 80 | + return FreshnessResult { |
| 81 | + status: ReportStatus::Stale, |
| 82 | + passed: false, |
| 83 | + reason: Some("incomplete_coverage".into()), |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + FreshnessResult { |
| 88 | + status: ReportStatus::Fresh, |
| 89 | + passed: true, |
| 90 | + reason: None, |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// 验证报告不伪造 |
| 95 | + pub fn validate_not_forged(metadata: &ReportMetadata) -> bool { |
| 96 | + !metadata.rules_covered.is_empty() || !metadata.scope_covered.is_empty() |
| 97 | + } |
| 98 | +} |
0 commit comments