aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-core/src/dependency_resolver/request.rs
blob: 4d650b0b6938e2ab903fbb62c01c5bb5832de86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use super::pool::PackageId;
use indexmap::IndexMap;

/// A requirement: package name + version constraint string.
#[derive(Debug, Clone)]
pub struct Require {
    pub package_name: String,
    pub constraint: Option<String>,
}

/// A request for the solver: what to install/fix/lock.
///
/// Port of Composer's Request.php.
#[derive(Debug, Clone)]
pub struct Request {
    /// Root requirements: package name → constraint string.
    pub requires: IndexMap<String, Option<String>>,
    /// Fixed packages (must be installed, cannot be modified).
    pub fixed_packages: Vec<PackageId>,
    /// Locked packages (installed but can be removed if nothing requires them).
    pub locked_packages: Vec<PackageId>,
}

impl Request {
    pub fn new() -> Self {
        Request {
            requires: IndexMap::new(),
            fixed_packages: Vec::new(),
            locked_packages: Vec::new(),
        }
    }

    /// Add a root requirement.
    pub fn require_name(&mut self, package_name: &str, constraint: Option<&str>) {
        self.requires.insert(
            package_name.to_lowercase(),
            constraint.map(|s| s.to_string()),
        );
    }

    /// Mark a package as fixed (must remain installed).
    pub fn fix_package(&mut self, package_id: PackageId) {
        if !self.fixed_packages.contains(&package_id) {
            self.fixed_packages.push(package_id);
        }
    }

    /// Mark a package as locked.
    pub fn lock_package(&mut self, package_id: PackageId) {
        if !self.locked_packages.contains(&package_id) {
            self.locked_packages.push(package_id);
        }
    }

    /// Check if a package is fixed.
    pub fn is_fixed(&self, package_id: PackageId) -> bool {
        self.fixed_packages.contains(&package_id)
    }
}

impl Default for Request {
    fn default() -> Self {
        Self::new()
    }
}