Skip to main content

lux_lib/drivers/sync/
reconcile.rs

1use crate::project;
2use crate::{
3    lockfile::{LockedPackage, LockedPackageLockType, Lockfile, ReadOnly, ReadWrite},
4    tree,
5};
6
7/// The result of reconciling a workspace's lockfile with an install tree's lockfile.
8#[derive(Debug)]
9pub struct SyncReport {
10    pub(crate) added: Vec<LockedPackage>,
11    pub(crate) removed: Vec<LockedPackage>,
12}
13
14impl SyncReport {
15    pub fn added(&self) -> &[LockedPackage] {
16        &self.added
17    }
18    pub fn removed(&self) -> &[LockedPackage] {
19        &self.removed
20    }
21
22    pub(crate) fn merge(&mut self, other: SyncReport) {
23        self.added.extend(other.added);
24        self.removed.extend(other.removed);
25    }
26}
27
28/// Determines which packages are present in one lockfile but not the other, producing the
29/// packages to add (with their entry type) and the report of packages that must be removed.
30pub(crate) fn reconcile_locks(
31    workspace_lockfile: &project::Lockfile<ReadWrite>,
32    dest_lockfile: &Lockfile<ReadOnly>,
33    lock_type: &LockedPackageLockType,
34) -> (Vec<(tree::EntryType, LockedPackage)>, SyncReport) {
35    let mut to_add: Vec<(tree::EntryType, LockedPackage)> = Vec::new();
36    let mut report = SyncReport {
37        added: Vec::new(),
38        removed: Vec::new(),
39    };
40
41    for (id, local_package) in workspace_lockfile.rocks(lock_type) {
42        if dest_lockfile.get(id).is_none() {
43            let entry_type = if workspace_lockfile.is_entrypoint(&local_package.id(), lock_type) {
44                tree::EntryType::Entrypoint
45            } else {
46                tree::EntryType::DependencyOnly
47            };
48            to_add.push((entry_type, local_package.clone()));
49        }
50    }
51    for (id, local_package) in dest_lockfile.rocks() {
52        if workspace_lockfile.get(id, lock_type).is_none() {
53            report.removed.push(local_package.clone());
54        }
55    }
56
57    (to_add, report)
58}