Skip to main content

pacsea/logic/
deps.rs

1//! Dependency resolution and analysis through the arch-toolkit integration boundary.
2
3use std::collections::HashSet;
4use std::hash::BuildHasher;
5
6use crate::state::modal::{DependencyInfo, ReverseRootSummary};
7use crate::state::types::PackageItem;
8
9/// What: Aggregate reverse-dependency data for Pacsea preflight views.
10///
11/// Inputs:
12/// - Produced by [`resolve_reverse_dependencies`].
13///
14/// Output:
15/// - Dependency rows and per-root summary counts.
16///
17/// Details:
18/// - Preserves Pacsea's public modal/cache contract while arch-toolkit owns traversal.
19#[derive(Clone, Debug, Default)]
20pub struct ReverseDependencyReport {
21    /// Flattened dependency rows reused by the preflight modal.
22    pub dependencies: Vec<DependencyInfo>,
23    /// Per-root direct and transitive dependency counts.
24    pub summaries: Vec<ReverseRootSummary>,
25}
26
27/// What: Resolve direct dependencies for selected packages through arch-toolkit.
28///
29/// Inputs:
30/// - `items`: Packages selected for install or update preflight.
31///
32/// Output:
33/// - Pacsea dependency rows sorted by urgency.
34///
35/// Details:
36/// - Host-tool failures remain nonfatal and are logged with actionable guidance.
37#[must_use]
38pub fn resolve_dependencies(items: &[PackageItem]) -> Vec<DependencyInfo> {
39    crate::integrations::arch_toolkit::deps::resolve_dependencies(items)
40}
41
42/// What: Resolve reverse dependencies for selected removal targets through arch-toolkit.
43///
44/// Inputs:
45/// - `items`: Packages selected for removal.
46///
47/// Output:
48/// - Pacsea-compatible reverse dependency report.
49///
50/// Details:
51/// - Toolkit traversal results are converted before reaching UI state.
52#[must_use]
53pub fn resolve_reverse_dependencies(items: &[PackageItem]) -> ReverseDependencyReport {
54    crate::integrations::arch_toolkit::deps::resolve_reverse_dependencies(items)
55}
56
57/// What: Query installed package names through arch-toolkit.
58///
59/// Inputs:
60/// - None.
61///
62/// Output:
63/// - Installed package names or an empty set when pacman is unavailable.
64///
65/// Details:
66/// - The query runs with a stable C locale.
67#[must_use]
68pub fn get_installed_packages() -> HashSet<String> {
69    crate::integrations::arch_toolkit::deps::installed_packages()
70}
71
72/// What: Query packages with available upgrades through arch-toolkit.
73///
74/// Inputs:
75/// - None.
76///
77/// Output:
78/// - Upgradable package names or an empty set when pacman is unavailable.
79///
80/// Details:
81/// - Nonzero pacman status remains a nonfatal empty result.
82#[must_use]
83pub fn get_upgradable_packages() -> HashSet<String> {
84    crate::integrations::arch_toolkit::deps::upgradable_packages()
85}
86
87/// What: Return caller-compatible virtual provider state through arch-toolkit.
88///
89/// Inputs:
90/// - `installed`: Installed package names.
91///
92/// Output:
93/// - Provided package names known by the toolkit query policy.
94///
95/// Details:
96/// - Lazy host lookup still occurs in [`is_package_installed_or_provided`].
97#[must_use]
98pub fn get_provided_packages<S: BuildHasher + Default>(
99    installed: &HashSet<String, S>,
100) -> HashSet<String> {
101    crate::integrations::arch_toolkit::deps::provided_packages(installed)
102}
103
104/// What: Check installed or virtual-provider membership through arch-toolkit.
105///
106/// Inputs:
107/// - `name`: Package or virtual dependency name.
108/// - `installed`: Caller-supplied installed package names.
109/// - `provided`: Caller-supplied virtual provider names.
110///
111/// Output:
112/// - `true` when the dependency is satisfied.
113///
114/// Details:
115/// - Supplied sets are honored before lazy host fallback.
116#[must_use]
117pub fn is_package_installed_or_provided<S: BuildHasher>(
118    name: &str,
119    installed: &HashSet<String, S>,
120    provided: &HashSet<String, S>,
121) -> bool {
122    crate::integrations::arch_toolkit::deps::is_installed_or_provided(name, installed, provided)
123}
124
125/// What: Query one installed package version through arch-toolkit.
126///
127/// Inputs:
128/// - `name`: Package name.
129///
130/// Output:
131/// - Installed version or a user-facing string error.
132///
133/// # Errors
134///
135/// Returns an error when pacman is unavailable, the package is missing, or output cannot be parsed.
136///
137/// Details:
138/// - Preserves the pre-migration result shape used by preflight call sites.
139pub fn get_installed_version(name: &str) -> Result<String, String> {
140    crate::integrations::arch_toolkit::deps::installed_version(name)
141}
142
143/// What: Evaluate a pacman-style version requirement through arch-toolkit.
144///
145/// Inputs:
146/// - `version`: Installed version.
147/// - `requirement`: Comparison expression.
148///
149/// Output:
150/// - Whether the requirement is satisfied.
151///
152/// Details:
153/// - Uses epoch/pkgver/pkgrel-aware ordering.
154#[must_use]
155pub fn version_satisfies(version: &str, requirement: &str) -> bool {
156    crate::integrations::arch_toolkit::deps::version_satisfies(version, requirement)
157}
158
159/// What: Check whether an installed package has installed reverse dependencies.
160///
161/// Inputs:
162/// - `name`: Package name.
163///
164/// Output:
165/// - Whether at least one installed package requires it.
166///
167/// Details:
168/// - Query failure degrades to `false`.
169#[must_use]
170pub fn has_installed_required_by(name: &str) -> bool {
171    crate::integrations::arch_toolkit::deps::has_installed_required_by(name)
172}
173
174/// What: List installed packages that require another package.
175///
176/// Inputs:
177/// - `name`: Package name.
178///
179/// Output:
180/// - Installed reverse dependency names.
181///
182/// Details:
183/// - Query failure degrades to an empty vector.
184#[must_use]
185pub fn get_installed_required_by(name: &str) -> Vec<String> {
186    crate::integrations::arch_toolkit::deps::installed_required_by(name)
187}
188
189#[cfg(test)]
190mod tests {
191    /// What: Verify empty dependency input remains a deterministic no-op.
192    ///
193    /// Inputs:
194    /// - Empty package slice.
195    ///
196    /// Output:
197    /// - Empty dependency rows without host queries.
198    ///
199    /// Details:
200    /// - Protects preflight initialization and cache-reset paths.
201    #[test]
202    fn empty_dependency_resolution_is_noop() {
203        assert!(super::resolve_dependencies(&[]).is_empty());
204    }
205
206    /// What: Verify empty reverse dependency input remains a deterministic no-op.
207    ///
208    /// Inputs:
209    /// - Empty package slice.
210    ///
211    /// Output:
212    /// - Empty dependency and summary collections.
213    ///
214    /// Details:
215    /// - Protects removal-preflight initialization.
216    #[test]
217    fn empty_reverse_resolution_is_noop() {
218        let report = super::resolve_reverse_dependencies(&[]);
219        assert!(report.dependencies.is_empty());
220        assert!(report.summaries.is_empty());
221    }
222}