Skip to main content

zoi_cli/cmd/
utils.rs

1//! Utility functions for CLI commands.
2
3use anyhow::Result;
4
5use crate::pkg::{local, resolve};
6
7/// Expands split packages into their individual sub-packages if they are
8/// installed.
9///
10/// # Errors
11///
12/// Returns an error if:
13/// - The list of installed packages cannot be retrieved.
14/// - A package name cannot be parsed.
15/// - A package cannot be resolved to a specific version.
16pub fn expand_split_packages(
17    package_names: &[String],
18    action: &str
19) -> Result<Vec<String>> {
20    let mut expanded_names = Vec::new();
21    let installed_packages = local::get_installed_packages()?;
22
23    for name in package_names {
24        let request = resolve::parse_source_string(name)?;
25        let mut was_expanded = false;
26
27        if request.sub_package.is_none()
28            && let Ok((pkg, _, _, _, _, _, _)) =
29                resolve::resolve_package_and_version(name, None, true, false)
30            && pkg.sub_packages.is_some()
31        {
32            let mut installed_subs = Vec::new();
33            for manifest in &installed_packages {
34                if manifest.name == pkg.name
35                    && let Some(sub) = &manifest.sub_package
36                {
37                    installed_subs.push(sub.clone());
38                }
39            }
40
41            if !installed_subs.is_empty() {
42                println!(
43                    "'{}' is a split package. {} all installed sub-packages: \
44                     {}",
45                    name,
46                    action,
47                    installed_subs.join(", ")
48                );
49                for sub in installed_subs {
50                    expanded_names.push(format!("{name}:{sub}"));
51                }
52                was_expanded = true;
53            }
54        }
55
56        if !was_expanded {
57            expanded_names.push(name.clone());
58        }
59    }
60
61    Ok(expanded_names)
62}