1use serde_json::Value;
10
11use crate::HapError;
12use crate::record;
13use crate::value::{self, Findings, Line, Note, Obj};
14
15#[derive(Debug, Clone, PartialEq)]
17pub struct PkgContextInfo {
18 packages: Vec<PkgContext>,
20 pub notes: Vec<Note>,
22 pub unmodelled: Vec<String>,
24 raw: Value,
25}
26
27#[derive(Debug, Clone, Default, PartialEq, Eq)]
30pub struct PkgContext {
31 pub name: String,
33 pub package_name: String,
35 pub bundle_name: String,
37 pub module_name: String,
38 pub version: String,
40 pub entry_path: String,
43 pub is_so: bool,
45 pub dependency_alias: String,
46}
47
48impl PkgContext {
49 pub fn entry_record_name(&self) -> Option<String> {
56 if self.is_so || self.entry_path.is_empty() || self.entry_path.ends_with('/') {
57 return None;
58 }
59 Some(format!("&{}/{}&{}", self.name, record::strip_source_extension(&self.entry_path), self.version))
60 }
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub struct RecordOwner<'c, 'r> {
66 pub package: &'c PkgContext,
67 pub bundle: &'r str,
69 pub path: &'r str,
72 pub version: &'r str,
74}
75
76impl PkgContextInfo {
77 pub fn parse(bytes: &[u8]) -> Result<Self, HapError> {
83 let map = value::parse_object(bytes)?;
84 let mut findings = Findings::default();
85 let mut packages = Vec::with_capacity(map.len());
86 for (name, entry) in &map {
87 let Some(entry) = entry.as_object() else {
88 findings.notes.push(Note { path: name.clone(), message: format!("expected object, found {}", value::kind(entry)) });
89 continue;
90 };
91 let mut scratch = Findings::default();
94 let mut o = Obj::root(entry, &mut scratch);
95 packages.push(PkgContext {
96 name: name.clone(),
97 package_name: o.str("packageName").unwrap_or_default(),
98 bundle_name: o.str("bundleName").unwrap_or_default(),
99 module_name: o.str("moduleName").unwrap_or_default(),
100 version: o.text("version").unwrap_or_default(),
101 entry_path: o.str("entryPath").unwrap_or_default(),
102 is_so: o.bool("isSO").unwrap_or_default(),
103 dependency_alias: o.str("dependencyAlias").unwrap_or_default(),
104 });
105 o.finish();
106 findings.notes.extend(scratch.notes.into_iter().map(|n| Note { path: format!("{name}.{}", n.path), ..n }));
107 findings.unmodelled.extend(scratch.unmodelled.into_iter().map(|path| format!("{name}.{path}")));
108 }
109 packages.sort_by(|a, b| a.name.cmp(&b.name));
110 Ok(PkgContextInfo { packages, notes: findings.notes, unmodelled: findings.unmodelled, raw: Value::Object(map) })
111 }
112
113 pub fn packages(&self) -> &[PkgContext] {
115 &self.packages
116 }
117
118 pub fn get(&self, name: &str) -> Option<&PkgContext> {
120 self.packages.binary_search_by(|p| p.name.as_str().cmp(name)).ok().map(|i| &self.packages[i])
121 }
122
123 pub fn locate<'r>(&self, record: &'r str) -> Option<RecordOwner<'_, 'r>> {
132 let (bundle, rest) = record.split_once('&')?;
133 let (body, version) = rest.rsplit_once('&')?;
134 let boundaries = body.match_indices('/').map(|(i, _)| i).chain([body.len()]).collect::<Vec<_>>();
135 boundaries.into_iter().rev().find_map(|end| {
136 let package = self.get(&body[..end])?;
137 Some(RecordOwner { package, bundle, path: body[end..].trim_start_matches('/'), version })
138 })
139 }
140
141 pub fn raw(&self) -> &Value {
143 &self.raw
144 }
145
146 pub fn pretty(&self) -> String {
148 value::pretty(&self.raw)
149 }
150
151 pub fn summary_lines(&self) -> Vec<String> {
154 let text = |s: &str| if s.is_empty() { "-".to_owned() } else { value::token(s).into_owned() };
155 let mut out: Vec<String> = self
156 .packages
157 .iter()
158 .map(|p| {
159 let mut line = Line::new("package")
160 .word(&p.name)
161 .raw("version", text(&p.version))
162 .raw("entryPath", text(&p.entry_path))
163 .raw("isSO", p.is_so)
164 .raw("bundleName", text(&p.bundle_name))
165 .raw("moduleName", text(&p.module_name))
166 .raw("dependencyAlias", text(&p.dependency_alias));
167 if p.package_name != p.name {
168 line = line.raw("packageName", text(&p.package_name));
169 }
170 line.end()
171 })
172 .collect();
173 out.extend(self.unmodelled.iter().map(|path| Line::new("unmodelled").word(path).end()));
174 out.extend(self.notes.iter().map(|note| format!("note {note}")));
175 out
176 }
177
178 pub fn summary(&self) -> String {
181 crate::join_lines(&self.summary_lines())
182 }
183}