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
|
//! ref: composer/src/Composer/DependencyResolver/GenericRule.php
use anyhow::Result;
use shirabe_php_shim::{hash_raw, implode, unpack, RuntimeException, PHP_VERSION_ID};
use crate::dependency_resolver::rule::Rule;
pub struct GenericRule {
inner: Rule,
pub(crate) literals: Vec<i64>,
}
impl GenericRule {
pub fn new(mut literals: Vec<i64>, reason: shirabe_php_shim::PhpMixed, reason_data: shirabe_php_shim::PhpMixed) -> Self {
let inner = Rule::new(reason, 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 fn to_string(&self) -> String {
let prefix = if self.inner.is_disabled() { "disabled(" } else { "(" };
let mut result = prefix.to_string();
for (i, literal) in self.literals.iter().enumerate() {
if i != 0 {
result.push('|');
}
result.push_str(&literal.to_string());
}
result.push(')');
result
}
}
pub trait RuleLiterals {
fn get_literals(&self) -> &Vec<i64>;
}
impl RuleLiterals for GenericRule {
fn get_literals(&self) -> &Vec<i64> {
&self.literals
}
}
|