|
| 1 | +//! 动态构建模型解析任务与共享扫描义务映射 |
| 2 | +//! |
| 3 | +//! 验收标准:detect/init/plan 仅保留待解析条件,执行期经统一 runtime 留证据 |
| 4 | +
|
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +/// 构建模型状态 |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 9 | +pub enum BuildModelState { |
| 10 | + /// 已解析 |
| 11 | + Resolved, |
| 12 | + /// 待解析 |
| 13 | + Pending, |
| 14 | + /// 解析失败 |
| 15 | + Failed, |
| 16 | +} |
| 17 | + |
| 18 | +/// 构建根 |
| 19 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 20 | +pub struct BuildRoot { |
| 21 | + /// 路径 |
| 22 | + pub path: String, |
| 23 | + /// 构建系统 |
| 24 | + pub build_system: String, |
| 25 | + /// 状态 |
| 26 | + pub state: BuildModelState, |
| 27 | + /// 待解析条件 |
| 28 | + pub pending_conditions: Vec<String>, |
| 29 | +} |
| 30 | + |
| 31 | +/// 扫描义务 |
| 32 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 33 | +pub struct ScanObligation { |
| 34 | + /// 义务 ID |
| 35 | + pub id: String, |
| 36 | + /// 构建根路径 |
| 37 | + pub build_root: String, |
| 38 | + /// 检查类别 |
| 39 | + pub category: String, |
| 40 | + /// 是否已解析 |
| 41 | + pub resolved: bool, |
| 42 | +} |
| 43 | + |
| 44 | +/// 解析结果 |
| 45 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 46 | +pub struct ResolutionResult { |
| 47 | + /// 已解析构建根 |
| 48 | + pub resolved: Vec<BuildRoot>, |
| 49 | + /// 待解析构建根 |
| 50 | + pub pending: Vec<BuildRoot>, |
| 51 | + /// 扫描义务 |
| 52 | + pub obligations: Vec<ScanObligation>, |
| 53 | +} |
| 54 | + |
| 55 | +/// 构建模型解析器 |
| 56 | +pub struct BuildModelResolver; |
| 57 | + |
| 58 | +impl BuildModelResolver { |
| 59 | + /// 解析构建模型 |
| 60 | + pub fn resolve(roots: &[BuildRoot]) -> ResolutionResult { |
| 61 | + let mut resolved = Vec::new(); |
| 62 | + let mut pending = Vec::new(); |
| 63 | + let mut obligations = Vec::new(); |
| 64 | + |
| 65 | + for root in roots { |
| 66 | + match root.state { |
| 67 | + BuildModelState::Resolved => { |
| 68 | + resolved.push(root.clone()); |
| 69 | + |
| 70 | + // 生成扫描义务 |
| 71 | + for category in &["lint", "comments", "dependencies", "cve", "security", "build"] { |
| 72 | + obligations.push(ScanObligation { |
| 73 | + id: format!("{}:{}", root.path, category), |
| 74 | + build_root: root.path.clone(), |
| 75 | + category: category.to_string(), |
| 76 | + resolved: true, |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + BuildModelState::Pending => { |
| 81 | + pending.push(root.clone()); |
| 82 | + } |
| 83 | + BuildModelState::Failed => { |
| 84 | + // 解析失败的构建根不生成义务 |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + ResolutionResult { |
| 90 | + resolved, |
| 91 | + pending, |
| 92 | + obligations, |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// 检查是否仅保留待解析条件 |
| 97 | + pub fn only_pending_conditions(roots: &[BuildRoot]) -> bool { |
| 98 | + roots.iter().all(|r| { |
| 99 | + r.state == BuildModelState::Pending && !r.pending_conditions.is_empty() |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + /// 验证执行期证据 |
| 104 | + pub fn validate_execution_evidence(obligations: &[ScanObligation]) -> bool { |
| 105 | + obligations.iter().all(|o| o.resolved) |
| 106 | + } |
| 107 | +} |
0 commit comments