1use serde_json::Value;
4
5use crate::integrations::arch_toolkit::ToolkitContext;
6use crate::state::{PackageDetails, PackageItem, Source};
7use crate::util::{arrs, ss, u64_of};
8
9type Result<T> = super::Result<T>;
11
12fn split_ws_or_none(s: Option<&String>) -> Vec<String> {
20 match s {
21 Some(v) if v != "None" => v.split_whitespace().map(ToString::to_string).collect(),
22 _ => Vec::new(),
23 }
24}
25
26fn process_continuation_line(
39 map: &mut std::collections::BTreeMap<String, String>,
40 key: &str,
41 line: &str,
42) {
43 let entry = map.entry(key.to_string()).or_default();
44 if key == "Optional Deps" {
45 entry.push('\n');
46 } else if !entry.ends_with(' ') {
47 entry.push(' ');
48 }
49 entry.push_str(line.trim());
50}
51
52fn run_pacman_si(repo: &str, name: &str) -> Result<String> {
64 let spec = if repo.is_empty() {
65 name.to_string()
66 } else {
67 format!("{repo}/{name}")
68 };
69 let out = std::process::Command::new("pacman")
70 .env("LC_ALL", "C")
71 .env("LANG", "C")
72 .args(["-Si", &spec])
73 .output()?;
74 if !out.status.success() {
75 return Err(format!("pacman -Si failed: {:?}", out.status).into());
76 }
77 String::from_utf8(out.stdout).map_err(std::convert::Into::into)
78}
79
80fn parse_pacman_output(text: &str) -> std::collections::BTreeMap<String, String> {
92 let mut map: std::collections::BTreeMap<String, String> = std::collections::BTreeMap::new();
93 let mut last_key: Option<String> = None;
94 for line in text.lines() {
95 if line.trim().is_empty() {
96 continue;
97 }
98 if let Some((k, v)) = line.split_once(':') {
99 let key = k.trim().to_string();
100 let val = v.trim().to_string();
101 map.insert(key.clone(), val);
102 last_key = Some(key);
103 } else if line.starts_with(' ')
104 && let Some(k) = &last_key
105 {
106 process_continuation_line(&mut map, k, line);
107 }
108 }
109 map
110}
111
112struct ParsedFields {
116 licenses: Vec<String>,
118 groups: Vec<String>,
120 provides: Vec<String>,
122 depends: Vec<String>,
124 opt_depends: Vec<String>,
126 required_by: Vec<String>,
128 optional_for: Vec<String>,
130 conflicts: Vec<String>,
132 replaces: Vec<String>,
134 description: String,
136 architecture: String,
138 download_size: Option<u64>,
140 install_size: Option<u64>,
142}
143
144fn extract_fields(map: &std::collections::BTreeMap<String, String>) -> ParsedFields {
156 let licenses = split_ws_or_none(map.get("Licenses").or_else(|| map.get("License")));
157 let groups = split_ws_or_none(map.get("Groups"));
158 let provides = split_ws_or_none(map.get("Provides"));
159 let depends = split_ws_or_none(map.get("Depends On"));
160 let opt_depends = map
161 .get("Optional Deps")
162 .map(|s| {
163 s.lines()
164 .filter_map(|l| l.split_once(':').map(|(pkg, _)| pkg.trim().to_string()))
165 .filter(|x| !x.is_empty() && x != "None")
166 .collect()
167 })
168 .unwrap_or_default();
169 let required_by = split_ws_or_none(map.get("Required By"));
170 let optional_for = split_ws_or_none(map.get("Optional For"));
171 let conflicts = split_ws_or_none(map.get("Conflicts With"));
172 let replaces = split_ws_or_none(map.get("Replaces"));
173
174 ParsedFields {
175 licenses,
176 groups,
177 provides,
178 depends,
179 opt_depends,
180 required_by,
181 optional_for,
182 conflicts,
183 replaces,
184 description: map.get("Description").cloned().unwrap_or_default(),
185 architecture: map.get("Architecture").cloned().unwrap_or_default(),
186 download_size: map.get("Download Size").and_then(|s| parse_size_bytes(s)),
187 install_size: map.get("Installed Size").and_then(|s| parse_size_bytes(s)),
188 }
189}
190
191fn fill_missing_fields(name: &str, description: &mut String, architecture: &mut String) {
205 if description.is_empty() || architecture.is_empty() {
206 let mut from_idx = None;
207 let official_results = crate::index::search_official(name, false);
209 for (it, _) in official_results {
210 if it.name.eq_ignore_ascii_case(name) {
211 from_idx = Some(it);
212 break;
213 }
214 }
215 if let Some(it) = from_idx {
216 if description.is_empty() {
217 *description = it.description;
218 }
219 if architecture.is_empty()
220 && let Source::Official { arch, .. } = it.source
221 {
222 *architecture = arch;
223 }
224 }
225 }
226}
227
228fn build_package_details(
239 repo: &str,
240 name: &str,
241 map: &std::collections::BTreeMap<String, String>,
242 fields: ParsedFields,
243) -> PackageDetails {
244 PackageDetails {
245 repository: map
246 .get("Repository")
247 .cloned()
248 .unwrap_or_else(|| repo.to_string()),
249 name: map.get("Name").cloned().unwrap_or_else(|| name.to_string()),
250 version: map.get("Version").cloned().unwrap_or_default(),
251 description: fields.description,
252 architecture: fields.architecture,
253 url: map.get("URL").cloned().unwrap_or_default(),
254 licenses: fields.licenses,
255 groups: fields.groups,
256 provides: fields.provides,
257 depends: fields.depends,
258 opt_depends: fields.opt_depends,
259 required_by: fields.required_by,
260 optional_for: fields.optional_for,
261 conflicts: fields.conflicts,
262 replaces: fields.replaces,
263 download_size: fields.download_size,
264 install_size: fields.install_size,
265 owner: map.get("Packager").cloned().unwrap_or_default(),
266 build_date: map.get("Build Date").cloned().unwrap_or_default(),
267 popularity: None,
268 out_of_date: None,
269 orphaned: false,
270 }
271}
272
273fn pacman_si(repo: &str, name: &str) -> Result<PackageDetails> {
282 let text = run_pacman_si(repo, name)?;
283 let map = parse_pacman_output(&text);
284 let mut fields = extract_fields(&map);
285 fill_missing_fields(name, &mut fields.description, &mut fields.architecture);
286 Ok(build_package_details(repo, name, &map, fields))
287}
288
289fn parse_size_bytes(s: &str) -> Option<u64> {
297 const MAX_U64_AS_F64: f64 = 18_446_744_073_709_551_615.0; let mut it = s.split_whitespace();
301 let num = it.next()?.parse::<f64>().ok()?;
302 let unit = it.next().unwrap_or("");
303 let mult = match unit {
304 "KiB" => 1024.0,
305 "MiB" => 1024.0 * 1024.0,
306 "GiB" => 1024.0 * 1024.0 * 1024.0,
307 "TiB" => 1024.0 * 1024.0 * 1024.0 * 1024.0,
308 _ => 1.0,
309 };
310 let result = num * mult;
311 if result < 0.0 {
313 return None;
314 }
315 if result > MAX_U64_AS_F64 {
316 return None;
317 }
318 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
319 let bytes = result as u64;
320 Some(bytes)
321}
322
323#[cfg(test)]
324mod size_tests {
325 #[test]
326 fn details_parse_size_bytes_units() {
337 assert_eq!(super::parse_size_bytes("10 B"), Some(10));
338 assert_eq!(super::parse_size_bytes("1 KiB"), Some(1024));
339 assert_eq!(super::parse_size_bytes("2 MiB"), Some(2 * 1024 * 1024));
340 assert_eq!(
341 super::parse_size_bytes("3 GiB"),
342 Some(3 * 1024 * 1024 * 1024)
343 );
344 assert_eq!(
345 super::parse_size_bytes("4 TiB"),
346 Some(4 * 1024 * 1024 * 1024 * 1024)
347 );
348 assert!(super::parse_size_bytes("bad").is_none());
349 }
350}
351
352pub async fn fetch_details(item: PackageItem) -> Result<PackageDetails> {
366 let context = ToolkitContext::new()?;
367 fetch_details_with_context(&context, item).await
368}
369
370pub async fn fetch_details_with_context(
382 context: &ToolkitContext,
383 item: PackageItem,
384) -> Result<PackageDetails> {
385 match item.source.clone() {
386 Source::Official { repo, arch } => {
387 fetch_official_details_with_context(context, repo, arch, item).await
388 }
389 Source::Aur => crate::integrations::arch_toolkit::aur::details(context, item)
390 .await
391 .map_err(Into::into),
392 }
393}
394
395pub async fn fetch_official_details_with_context(
418 context: &ToolkitContext,
419 repo: String,
420 arch: String,
421 item: PackageItem,
422) -> Result<PackageDetails> {
423 if let Ok(Ok(pd)) = tokio::task::spawn_blocking({
424 let repo = repo.clone();
425 let name = item.name.clone();
426 move || pacman_si(&repo, &name)
427 })
428 .await
429 {
430 let has_core =
431 !(pd.description.is_empty() && pd.architecture.is_empty() && pd.licenses.is_empty());
432 if has_core {
433 return Ok(pd);
434 }
435 }
436
437 let arch_candidates: Vec<String> = if arch.trim().is_empty() {
438 vec!["x86_64".to_string(), "any".to_string()]
439 } else if arch.to_lowercase() == "any" {
440 vec!["any".to_string()]
441 } else {
442 vec![arch.clone(), "any".to_string()]
443 };
444 let repo_candidates: Vec<String> = if repo.trim().is_empty() {
445 vec!["core".to_string(), "extra".to_string()]
446 } else {
447 vec![repo.clone()]
448 };
449 let mut v: Option<Value> = None;
450 let mut repo_selected = repo.clone();
451 let mut arch_selected = arch.clone();
452 'outer: for r in &repo_candidates {
453 for a in &arch_candidates {
454 let url = format!(
455 "https://archlinux.org/packages/{}/{}/{}/json/",
456 r.to_lowercase(),
457 a,
458 item.name
459 );
460 if let Ok(Ok(val)) = tokio::task::spawn_blocking({
461 let url = url.clone();
462 move || crate::util::curl::curl_json(&url)
463 })
464 .await
465 {
466 v = Some(val);
467 repo_selected.clone_from(r);
468 arch_selected.clone_from(a);
469 break 'outer;
470 }
471 }
472 }
473
474 if let Some(v) = v {
475 let obj = v.get("pkg").unwrap_or(&v);
476 let d = PackageDetails {
477 repository: repo_selected,
478 name: item.name.clone(),
479 version: ss(obj, &["pkgver", "Version"]).unwrap_or(item.version),
480 description: ss(obj, &["pkgdesc", "Description"]).unwrap_or(item.description),
481 architecture: ss(obj, &["arch", "Architecture"]).unwrap_or(arch_selected),
482 url: ss(obj, &["url", "URL"]).unwrap_or_default(),
483 licenses: arrs(obj, &["licenses", "Licenses"]),
484 groups: arrs(obj, &["groups", "Groups"]),
485 provides: arrs(obj, &["provides", "Provides"]),
486 depends: arrs(obj, &["depends", "Depends"]),
487 opt_depends: arrs(obj, &["optdepends", "OptDepends"]),
488 required_by: arrs(obj, &["requiredby", "RequiredBy"]),
489 optional_for: vec![],
490 conflicts: arrs(obj, &["conflicts", "Conflicts"]),
491 replaces: arrs(obj, &["replaces", "Replaces"]),
492 download_size: u64_of(obj, &["compressed_size", "CompressedSize"]),
493 install_size: u64_of(obj, &["installed_size", "InstalledSize"]),
494 owner: ss(obj, &["packager", "Packager"]).unwrap_or_default(),
495 build_date: ss(obj, &["build_date", "BuildDate"]).unwrap_or_default(),
496 popularity: None,
497 out_of_date: None,
498 orphaned: false,
499 };
500 return Ok(d);
501 }
502
503 let selector = arch_toolkit::OfficialPackage {
504 name: item.name.clone(),
505 repo,
506 arch,
507 version: item.version.clone(),
508 description: item.description.clone(),
509 };
510 if let Some(package) = arch_toolkit::fetch_arch_package_detail(
511 context.http_client(),
512 &selector,
513 arch_toolkit::types::package::MetadataFetchLimits::default(),
514 )
515 .await
516 .map_err(|error| format!("official package metadata unavailable: {error}"))?
517 {
518 return Ok(PackageDetails {
519 repository: package.repo,
520 name: package.name,
521 version: package.version,
522 description: package.description,
523 architecture: package.arch,
524 ..PackageDetails::default()
525 });
526 }
527
528 Err("official details unavailable; verify pacman repositories and network access".into())
529}
530
531#[cfg(test)]
532mod tests {
533 #[test]
536 fn sources_details_parse_official_json_defaults_and_fields() {
548 fn parse_official_from_json(
549 obj: &serde_json::Value,
550 repo_selected: String,
551 arch_selected: String,
552 item: &crate::state::PackageItem,
553 ) -> crate::state::PackageDetails {
554 use crate::util::{arrs, ss, u64_of};
555 crate::state::PackageDetails {
556 repository: repo_selected,
557 name: item.name.clone(),
558 version: ss(obj, &["pkgver", "Version"]).unwrap_or_else(|| item.version.clone()),
559 description: ss(obj, &["pkgdesc", "Description"])
560 .unwrap_or_else(|| item.description.clone()),
561 architecture: ss(obj, &["arch", "Architecture"]).unwrap_or(arch_selected),
562 url: ss(obj, &["url", "URL"]).unwrap_or_default(),
563 licenses: arrs(obj, &["licenses", "Licenses"]),
564 groups: arrs(obj, &["groups", "Groups"]),
565 provides: arrs(obj, &["provides", "Provides"]),
566 depends: arrs(obj, &["depends", "Depends"]),
567 opt_depends: arrs(obj, &["optdepends", "OptDepends"]),
568 required_by: arrs(obj, &["requiredby", "RequiredBy"]),
569 optional_for: vec![],
570 conflicts: arrs(obj, &["conflicts", "Conflicts"]),
571 replaces: arrs(obj, &["replaces", "Replaces"]),
572 download_size: u64_of(obj, &["compressed_size", "CompressedSize"]),
573 install_size: u64_of(obj, &["installed_size", "InstalledSize"]),
574 owner: ss(obj, &["packager", "Packager"]).unwrap_or_default(),
575 build_date: ss(obj, &["build_date", "BuildDate"]).unwrap_or_default(),
576 popularity: None,
577 out_of_date: None,
578 orphaned: false,
579 }
580 }
581 let v: serde_json::Value = serde_json::json!({
582 "pkg": {
583 "pkgver": "14",
584 "pkgdesc": "ripgrep fast search",
585 "arch": "x86_64",
586 "url": "https://example.com",
587 "licenses": ["MIT"],
588 "groups": [],
589 "provides": ["rg"],
590 "depends": ["pcre2"],
591 "optdepends": ["bash: completions"],
592 "requiredby": [],
593 "conflicts": [],
594 "replaces": [],
595 "compressed_size": 1024u64,
596 "installed_size": 2048u64,
597 "packager": "Arch Dev",
598 "build_date": "2024-01-01"
599 }
600 });
601 let item = crate::state::PackageItem {
602 name: "ripgrep".into(),
603 version: String::new(),
604 description: String::new(),
605 source: crate::state::Source::Official {
606 repo: "extra".into(),
607 arch: "x86_64".into(),
608 },
609 popularity: None,
610 out_of_date: None,
611 orphaned: false,
612 };
613 let d = parse_official_from_json(&v["pkg"], "extra".into(), "x86_64".into(), &item);
614 assert_eq!(d.repository, "extra");
615 assert_eq!(d.name, "ripgrep");
616 assert_eq!(d.version, "14");
617 assert_eq!(d.description, "ripgrep fast search");
618 assert_eq!(d.architecture, "x86_64");
619 assert_eq!(d.url, "https://example.com");
620 assert_eq!(d.download_size, Some(1024));
621 assert_eq!(d.install_size, Some(2048));
622 assert_eq!(d.owner, "Arch Dev");
623 assert_eq!(d.build_date, "2024-01-01");
624 }
625
626 #[test]
627 fn sources_details_parse_aur_json_defaults_and_popularity() {
639 fn parse_aur_from_json(
640 obj: &serde_json::Value,
641 item: &crate::state::PackageItem,
642 ) -> crate::state::PackageDetails {
643 use crate::util::{arrs, s};
644 let version0 = s(obj, "Version");
645 let description0 = s(obj, "Description");
646 let popularity0 = obj.get("Popularity").and_then(serde_json::Value::as_f64);
647 crate::state::PackageDetails {
648 repository: "AUR".into(),
649 name: item.name.clone(),
650 version: if version0.is_empty() {
651 item.version.clone()
652 } else {
653 version0
654 },
655 description: if description0.is_empty() {
656 item.description.clone()
657 } else {
658 description0
659 },
660 architecture: "any".into(),
661 url: s(obj, "URL"),
662 licenses: arrs(obj, &["License", "Licenses"]),
663 groups: arrs(obj, &["Groups"]),
664 provides: arrs(obj, &["Provides"]),
665 depends: arrs(obj, &["Depends"]),
666 opt_depends: arrs(obj, &["OptDepends"]),
667 required_by: vec![],
668 optional_for: vec![],
669 conflicts: arrs(obj, &["Conflicts"]),
670 replaces: arrs(obj, &["Replaces"]),
671 download_size: None,
672 install_size: None,
673 owner: s(obj, "Maintainer"),
674 build_date: crate::util::ts_to_date(
675 obj.get("LastModified").and_then(serde_json::Value::as_i64),
676 ),
677 popularity: popularity0,
678 out_of_date: None,
679 orphaned: false,
680 }
681 }
682 let obj: serde_json::Value = serde_json::json!({
683 "Version": "1.2.3",
684 "Description": "cool",
685 "Popularity": std::f64::consts::PI,
686 "URL": "https://aur.example/ripgrep"
687 });
688 let item = crate::state::PackageItem {
689 name: "ripgrep-git".into(),
690 version: String::new(),
691 description: String::new(),
692 source: crate::state::Source::Aur,
693 popularity: None,
694 out_of_date: None,
695 orphaned: false,
696 };
697 let d = parse_aur_from_json(&obj, &item);
698 assert_eq!(d.repository, "AUR");
699 assert_eq!(d.name, "ripgrep-git");
700 assert_eq!(d.version, "1.2.3");
701 assert_eq!(d.description, "cool");
702 assert_eq!(d.architecture, "any");
703 assert_eq!(d.url, "https://aur.example/ripgrep");
704 assert_eq!(d.popularity, Some(std::f64::consts::PI));
705 }
706
707 #[test]
708 fn sources_details_parse_aur_status_fields() {
719 use crate::util::s;
720 let obj: serde_json::Value = serde_json::json!({
721 "Version": "1.0.0",
722 "Description": "test package",
723 "OutOfDate": 1_704_067_200_i64, "Maintainer": "" });
726 let _item = crate::state::PackageItem {
727 name: "test-pkg".into(),
728 version: String::new(),
729 description: String::new(),
730 source: crate::state::Source::Aur,
731 popularity: None,
732 out_of_date: None,
733 orphaned: false,
734 };
735 let out_of_date = obj
737 .get("OutOfDate")
738 .and_then(serde_json::Value::as_i64)
739 .and_then(|ts| u64::try_from(ts).ok())
740 .filter(|&ts| ts > 0);
741 let maintainer = s(&obj, "Maintainer");
743 let orphaned = maintainer.is_empty();
744
745 assert_eq!(out_of_date, Some(1_704_067_200));
746 assert!(orphaned);
747 }
748
749 #[test]
750 fn sources_details_parse_aur_with_maintainer() {
761 use crate::util::s;
762 let obj: serde_json::Value = serde_json::json!({
763 "Version": "1.0.0",
764 "Description": "test package",
765 "Maintainer": "someuser"
766 });
767 let maintainer = s(&obj, "Maintainer");
768 let orphaned = maintainer.is_empty();
769
770 assert!(!orphaned);
771 assert_eq!(maintainer, "someuser");
772 }
773}