aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/dependency_resolver/operation/any_operation.rs
blob: 6675b28007c4981ee1bd99137bf84b19dd308b17 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! ref: composer/src/Composer/DependencyResolver/Operation/OperationInterface.php
//!
//! PHP's `OperationInterface` is not ported as a trait. Operations are only ever constructed by the
//! dependency resolver, so a plugin has no way to inject an implementation of its own and the set is
//! closed. The shared implementations PHP puts on `SolverOperation` (`getOperationType()`,
//! `__toString()`) live here.

use crate::dependency_resolver::operation::{
    InstallOperation, MarkAliasInstalledOperation, MarkAliasUninstalledOperation, SolverOperation,
    UninstallOperation, UpdateOperation,
};
use crate::package::PackageInterfaceHandle;

/// Any solver operation.
#[derive(Debug, Clone)]
pub enum AnyOperation {
    Install(InstallOperation),
    Update(UpdateOperation),
    Uninstall(UninstallOperation),
    MarkAliasInstalled(MarkAliasInstalledOperation),
    MarkAliasUninstalled(MarkAliasUninstalledOperation),
}

impl AnyOperation {
    pub fn get_operation_type(&self) -> &'static str {
        match self {
            Self::Install(_) => InstallOperation::TYPE,
            Self::Update(_) => UpdateOperation::TYPE,
            Self::Uninstall(_) => UninstallOperation::TYPE,
            Self::MarkAliasInstalled(_) => MarkAliasInstalledOperation::TYPE,
            Self::MarkAliasUninstalled(_) => MarkAliasUninstalledOperation::TYPE,
        }
    }

    pub fn show(&self, lock: bool) -> String {
        match self {
            Self::Install(op) => op.show(lock),
            Self::Update(op) => op.show(lock),
            Self::Uninstall(op) => op.show(lock),
            Self::MarkAliasInstalled(op) => op.show(lock),
            Self::MarkAliasUninstalled(op) => op.show(lock),
        }
    }

    /// The package the operation results in. PHP spells this out at every call site as
    /// `$op instanceof UpdateOperation ? $op->getTargetPackage() : $op->getPackage()`.
    pub fn get_target_package(&self) -> PackageInterfaceHandle {
        match self {
            Self::Install(op) => op.get_package(),
            Self::Update(op) => op.get_target_package(),
            Self::Uninstall(op) => op.get_package(),
            Self::MarkAliasInstalled(op) => op.get_package().into(),
            Self::MarkAliasUninstalled(op) => op.get_package().into(),
        }
    }
}

impl std::fmt::Display for AnyOperation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.show(false))
    }
}

impl From<InstallOperation> for AnyOperation {
    fn from(op: InstallOperation) -> Self {
        Self::Install(op)
    }
}

impl From<UpdateOperation> for AnyOperation {
    fn from(op: UpdateOperation) -> Self {
        Self::Update(op)
    }
}

impl From<UninstallOperation> for AnyOperation {
    fn from(op: UninstallOperation) -> Self {
        Self::Uninstall(op)
    }
}

impl From<MarkAliasInstalledOperation> for AnyOperation {
    fn from(op: MarkAliasInstalledOperation) -> Self {
        Self::MarkAliasInstalled(op)
    }
}

impl From<MarkAliasUninstalledOperation> for AnyOperation {
    fn from(op: MarkAliasUninstalledOperation) -> Self {
        Self::MarkAliasUninstalled(op)
    }
}