|
| 1 | +use serde::Serialize; |
| 2 | +use std::fs; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +pub use inventory; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Copy, Serialize)] |
| 8 | +pub struct SafetyCheckRef { |
| 9 | + pub check_id: &'static str, |
| 10 | + pub requirement_id: &'static str, |
| 11 | + pub description: &'static str, |
| 12 | + pub kind: &'static str, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Clone, Copy, Serialize)] |
| 16 | +pub struct SafetyCaseRef { |
| 17 | + pub package: &'static str, |
| 18 | + pub case_id: &'static str, |
| 19 | + pub function: &'static str, |
| 20 | + pub module_path: &'static str, |
| 21 | + pub file: &'static str, |
| 22 | + pub checks: &'static [SafetyCheckRef], |
| 23 | +} |
| 24 | + |
| 25 | +inventory::collect!(SafetyCaseRef); |
| 26 | + |
| 27 | +#[derive(Debug, Clone, Serialize)] |
| 28 | +pub struct PackageSafetyIndex { |
| 29 | + pub package: String, |
| 30 | + pub cases: Vec<CollectedSafetyCase>, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, Clone, Serialize)] |
| 34 | +pub struct CollectedSafetyCase { |
| 35 | + pub package: String, |
| 36 | + pub case_id: String, |
| 37 | + pub function: String, |
| 38 | + pub test_name: String, |
| 39 | + pub file: String, |
| 40 | + pub checks: Vec<CollectedSafetyCheck>, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Clone, Serialize)] |
| 44 | +pub struct CollectedSafetyCheck { |
| 45 | + pub check_id: String, |
| 46 | + pub requirement_id: String, |
| 47 | + pub description: String, |
| 48 | + pub kind: String, |
| 49 | +} |
| 50 | + |
| 51 | +pub fn collect_package_index(package: &str) -> PackageSafetyIndex { |
| 52 | + let mut cases: Vec<_> = inventory::iter::<SafetyCaseRef> |
| 53 | + .into_iter() |
| 54 | + .filter(|case_ref| case_ref.package == package) |
| 55 | + .map(|case_ref| CollectedSafetyCase { |
| 56 | + package: case_ref.package.to_string(), |
| 57 | + case_id: case_ref.case_id.to_string(), |
| 58 | + function: case_ref.function.to_string(), |
| 59 | + test_name: libtest_name(case_ref.module_path, case_ref.function), |
| 60 | + file: case_ref.file.to_string(), |
| 61 | + checks: case_ref |
| 62 | + .checks |
| 63 | + .iter() |
| 64 | + .map(|check| CollectedSafetyCheck { |
| 65 | + check_id: check.check_id.to_string(), |
| 66 | + requirement_id: check.requirement_id.to_string(), |
| 67 | + description: check.description.to_string(), |
| 68 | + kind: check.kind.to_string(), |
| 69 | + }) |
| 70 | + .collect(), |
| 71 | + }) |
| 72 | + .collect(); |
| 73 | + |
| 74 | + cases.sort_by(|left, right| left.case_id.cmp(&right.case_id)); |
| 75 | + for case in &mut cases { |
| 76 | + case.checks |
| 77 | + .sort_by(|left, right| left.check_id.cmp(&right.check_id)); |
| 78 | + } |
| 79 | + |
| 80 | + PackageSafetyIndex { |
| 81 | + package: package.to_string(), |
| 82 | + cases, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +pub fn write_package_index_json( |
| 87 | + path: impl AsRef<Path>, |
| 88 | + index: &PackageSafetyIndex, |
| 89 | +) -> std::io::Result<()> { |
| 90 | + let path = path.as_ref(); |
| 91 | + if let Some(parent) = path.parent() { |
| 92 | + fs::create_dir_all(parent)?; |
| 93 | + } |
| 94 | + let json = serde_json::to_vec_pretty(index).expect("safety index should serialize"); |
| 95 | + fs::write(path, json) |
| 96 | +} |
| 97 | + |
| 98 | +fn libtest_name(module_path: &str, function: &str) -> String { |
| 99 | + let mut segments = module_path.split("::"); |
| 100 | + let _crate_name = segments.next(); |
| 101 | + let rest: Vec<_> = segments.collect(); |
| 102 | + if rest.is_empty() { |
| 103 | + function.to_string() |
| 104 | + } else { |
| 105 | + format!("{}::{}", rest.join("::"), function) |
| 106 | + } |
| 107 | +} |
0 commit comments