aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/dependency_resolver/generic_rule.rs
blob: c0381ef6199ebc9df87841a1aee9a300d4c9d430 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! ref: composer/src/Composer/DependencyResolver/GenericRule.php

use crate::dependency_resolver::rule::{Rule, RuleBase};
use anyhow::Result;
use shirabe_php_shim::{PHP_VERSION_ID, PhpMixed, RuntimeException, hash_raw, implode, unpack};

use super::{request::Request, rule::ReasonData};

#[derive(Debug)]
pub struct GenericRule {
    inner: RuleBase,
    pub(crate) literals: Vec<i64>,
}

impl GenericRule {
    pub fn new(mut literals: Vec<i64>, reason: PhpMixed, reason_data: PhpMixed) -> Self {
        let inner = RuleBase::new(reason.as_int().unwrap_or(0), ReasonData::from(reason_data));
        literals.sort();
        Self { inner, literals }
    }

    pub fn get_literals(&self) -> &Vec<i64> {
        &self.literals
    }

    pub fn get_hash(&self) -> Result<i64> {
        let joined = self
            .literals
            .iter()
            .map(|l| l.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let algo = if PHP_VERSION_ID > 80100 {
            "xxh3"
        } else {
            "sha1"
        };
        let binary = hash_raw(algo, &joined);
        let data = unpack("ihash", &binary);
        match data {
            Some(map) => {
                if let Some(val) = map.get("hash") {
                    Ok(val.as_int().unwrap_or(0))
                } else {
                    Err(RuntimeException {
                        message: format!("Failed unpacking: {}", joined),
                        code: 0,
                    }
                    .into())
                }
            }
            None => Err(RuntimeException {
                message: format!("Failed unpacking: {}", joined),
                code: 0,
            }
            .into()),
        }
    }

    pub fn equals(&self, rule: &dyn RuleLiterals) -> bool {
        self.literals == *rule.get_literals()
    }

    pub fn is_assertion(&self) -> bool {
        self.literals.len() == 1
    }
}

pub trait RuleLiterals {
    fn get_literals(&self) -> &Vec<i64>;
    fn is_multi_conflict_rule(&self) -> bool {
        false
    }
    fn is_assertion(&self) -> bool {
        false
    }
    fn is_disabled(&self) -> bool {
        false
    }
    fn as_any(&self) -> &dyn std::any::Any {
        todo!()
    }
    /// Clone this rule into an owned `Box<dyn Rule>` so callers like
    /// `RuleWatchGraph::propagate_literal` can hand it to `Decisions::decide`.
    fn clone_rule_box(&self) -> Box<dyn Rule> {
        todo!()
    }
}

impl RuleLiterals for GenericRule {
    fn get_literals(&self) -> &Vec<i64> {
        &self.literals
    }
}

impl Rule for GenericRule {
    fn bitfield(&self) -> i64 {
        todo!()
    }

    fn bitfield_mut(&mut self) -> &mut i64 {
        todo!()
    }

    fn request(&self) -> Option<&Request> {
        todo!()
    }

    fn request_mut(&mut self) -> Option<&mut Request> {
        todo!()
    }

    fn reason_data(&self) -> Option<&ReasonData> {
        todo!()
    }

    fn reason_data_mut(&mut self) -> Option<&mut ReasonData> {
        todo!()
    }

    fn get_literals(&self) -> Vec<i64> {
        todo!()
    }

    fn get_hash(&self) -> PhpMixed {
        todo!()
    }

    fn equals(&self, rule: &dyn Rule) -> bool {
        todo!()
    }

    fn is_assertion(&self) -> bool {
        todo!()
    }
}

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

        for (i, literal) in self.literals.iter().enumerate() {
            if i != 0 {
                write!(f, "|")?;
            }
            write!(f, "{}", literal)?;
        }
        write!(f, ")")
    }
}