aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/dependency_resolver/rule2_literals.rs
blob: 7db902486863e28263e05c16ae22432715a45a38 (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
//! ref: composer/src/Composer/DependencyResolver/Rule2Literals.php

use shirabe_php_shim::PhpMixed;

use crate::dependency_resolver::{ReasonData, Rule, RuleBase};

#[derive(Debug)]
pub struct Rule2Literals {
    inner: RuleBase,
    pub(crate) literal1: i64,
    pub(crate) literal2: i64,
}

impl Rule2Literals {
    pub fn new(literal1: i64, literal2: i64, reason: PhpMixed, reason_data: PhpMixed) -> Self {
        let (literal1, literal2) = if literal1 < literal2 {
            (literal1, literal2)
        } else {
            (literal2, literal1)
        };

        Self {
            inner: RuleBase::new(reason.as_int().unwrap_or(0), ReasonData::from(reason_data)),
            literal1,
            literal2,
        }
    }

    pub(crate) fn base(&self) -> &RuleBase {
        &self.inner
    }

    pub(crate) fn base_mut(&mut self) -> &mut RuleBase {
        &mut self.inner
    }

    pub fn get_hash(&self) -> String {
        format!("{},{}", self.literal1, self.literal2)
    }

    pub fn equals(&self, rule: &Rule) -> bool {
        // PHP: specialized fast-case when `$rule instanceof self`.
        if let Rule::TwoLiterals(other) = rule {
            if self.literal1 != other.literal1 {
                return false;
            }
            if self.literal2 != other.literal2 {
                return false;
            }
            return true;
        }

        let literals = rule.get_literals();
        if literals.len() != 2 {
            return false;
        }
        if self.literal1 != literals[0] {
            return false;
        }
        if self.literal2 != literals[1] {
            return false;
        }
        true
    }

    pub fn is_assertion(&self) -> bool {
        false
    }
}

impl std::fmt::Display for Rule2Literals {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            if self.inner.is_disabled() {
                "disabled("
            } else {
                "("
            }
        )?;

        write!(f, "{}|{})", self.literal1, self.literal2)
    }
}