Skip to main content

lux_lib/drivers/sync/
reconcile.rs

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