aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-semver/src/constraint/simple_constraint.rs
blob: 161425a3aa1f13d62d4b71ab53caba8bd2e7f86a (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! ref: composer/vendor/composer/semver/src/Constraint/Constraint.php

use anyhow::bail;
use shirabe_php_shim as php;

use crate::constraint::Bound;

/// Corresponds to PHP's `Constraint`.
#[derive(Debug, Clone)]
pub struct SimpleConstraint {
    pub(crate) operator: i64,
    pub(crate) version: String,
    pub(crate) pretty_string: Option<String>,
}

impl SimpleConstraint {
    pub const OP_EQ: i64 = 0;
    pub const OP_LT: i64 = 1;
    pub const OP_LE: i64 = 2;
    pub const OP_GT: i64 = 3;
    pub const OP_GE: i64 = 4;
    pub const OP_NE: i64 = 5;

    pub const STR_OP_EQ: &'static str = "==";
    pub const STR_OP_EQ_ALT: &'static str = "=";
    pub const STR_OP_LT: &'static str = "<";
    pub const STR_OP_LE: &'static str = "<=";
    pub const STR_OP_GT: &'static str = ">";
    pub const STR_OP_GE: &'static str = ">=";
    pub const STR_OP_NE: &'static str = "!=";
    pub const STR_OP_NE_ALT: &'static str = "<>";

    fn trans_op_str(op: &str) -> Option<i64> {
        match op {
            "=" => Some(Self::OP_EQ),
            "==" => Some(Self::OP_EQ),
            "<" => Some(Self::OP_LT),
            "<=" => Some(Self::OP_LE),
            ">" => Some(Self::OP_GT),
            ">=" => Some(Self::OP_GE),
            "<>" => Some(Self::OP_NE),
            "!=" => Some(Self::OP_NE),
            _ => None,
        }
    }

    fn trans_op_int(op: i64) -> &'static str {
        match op {
            Self::OP_EQ => "==",
            Self::OP_LT => "<",
            Self::OP_LE => "<=",
            Self::OP_GT => ">",
            Self::OP_GE => ">=",
            Self::OP_NE => "!=",
            _ => panic!("unknown operator: {}", op),
        }
    }

    pub fn new(operator: String, version: String, pretty_string: Option<String>) -> Self {
        let op_int = Self::trans_op_str(&operator).unwrap_or_else(|| {
            // PHP raises InvalidArgumentException; in the Rust port keep that as a panic
            // because invalid operators are programmer errors caught during porting.
            panic!(
                "Invalid operator \"{}\" given, expected one of: {}",
                operator,
                Self::get_supported_operators().join(", ")
            )
        });

        Self {
            operator: op_int,
            version,
            pretty_string,
        }
    }

    pub fn get_version(&self) -> &str {
        &self.version
    }

    pub fn get_operator(&self) -> &'static str {
        Self::trans_op_int(self.operator)
    }

    pub fn get_supported_operators() -> Vec<&'static str> {
        vec!["=", "==", "<", "<=", ">", ">=", "<>", "!="]
    }

    pub fn get_operator_constant(operator: &str) -> i64 {
        Self::trans_op_str(operator).expect("valid operator")
    }

    pub fn version_compare(
        &self,
        a: &str,
        b: &str,
        operator: &str,
        compare_branches: bool,
    ) -> anyhow::Result<bool> {
        if Self::trans_op_str(operator).is_none() {
            bail!(
                "Invalid operator \"{}\" given, expected one of: {}",
                operator,
                Self::get_supported_operators().join(", ")
            );
        }

        let a_is_branch = a.starts_with("dev-");
        let b_is_branch = b.starts_with("dev-");

        if operator == "!=" && (a_is_branch || b_is_branch) {
            return Ok(a != b);
        }

        if a_is_branch && b_is_branch {
            return Ok(operator == "==" && a == b);
        }

        if !compare_branches && (a_is_branch || b_is_branch) {
            return Ok(false);
        }

        Ok(php::version_compare(a, b, operator))
    }

    pub fn compile_constraint(&self, other_operator: i64) -> String {
        if self.version.starts_with("dev-") {
            if Self::OP_EQ == self.operator {
                if Self::OP_EQ == other_operator {
                    return format!("$b && $v === {}", php::var_export_str(&self.version, true));
                }
                if Self::OP_NE == other_operator {
                    return format!("!$b || $v !== {}", php::var_export_str(&self.version, true));
                }
                return "false".to_string();
            }

            if Self::OP_NE == self.operator {
                if Self::OP_EQ == other_operator {
                    return format!("!$b || $v !== {}", php::var_export_str(&self.version, true));
                }
                if Self::OP_NE == other_operator {
                    return "true".to_string();
                }
                return "!$b".to_string();
            }

            return "false".to_string();
        }

        if Self::OP_EQ == self.operator {
            if Self::OP_EQ == other_operator {
                return format!(
                    "\\version_compare($v, {}, '==')",
                    php::var_export_str(&self.version, true)
                );
            }
            if Self::OP_NE == other_operator {
                return format!(
                    "$b || \\version_compare($v, {}, '!=')",
                    php::var_export_str(&self.version, true)
                );
            }
            return format!(
                "!$b && \\version_compare({}, $v, '{}')",
                php::var_export_str(&self.version, true),
                Self::trans_op_int(other_operator)
            );
        }

        if Self::OP_NE == self.operator {
            if Self::OP_EQ == other_operator {
                return format!(
                    "$b || (!$b && \\version_compare($v, {}, '!='))",
                    php::var_export_str(&self.version, true)
                );
            }
            if Self::OP_NE == other_operator {
                return "true".to_string();
            }
            return "!$b".to_string();
        }

        if Self::OP_LT == self.operator || Self::OP_LE == self.operator {
            if Self::OP_LT == other_operator || Self::OP_LE == other_operator {
                return "!$b".to_string();
            }
        } else if Self::OP_GT == other_operator || Self::OP_GE == other_operator {
            return "!$b".to_string();
        }

        if Self::OP_NE == other_operator {
            return "true".to_string();
        }

        let code_comparison = format!(
            "\\version_compare($v, {}, '{}')",
            php::var_export_str(&self.version, true),
            Self::trans_op_int(self.operator)
        );

        if self.operator == Self::OP_LE && other_operator == Self::OP_GT {
            return format!(
                "!$b && \\version_compare($v, {}, '!=') && {}",
                php::var_export_str(&self.version, true),
                code_comparison
            );
        }

        if self.operator == Self::OP_GE && other_operator == Self::OP_LT {
            return format!(
                "!$b && \\version_compare($v, {}, '!=') && {}",
                php::var_export_str(&self.version, true),
                code_comparison
            );
        }

        format!("!$b && {}", code_comparison)
    }

    pub fn match_specific(&self, provider: &SimpleConstraint, compare_branches: bool) -> bool {
        let no_equal_op = Self::trans_op_int(self.operator).replace('=', "");
        let provider_no_equal_op = Self::trans_op_int(provider.operator).replace('=', "");

        let is_equal_op = Self::OP_EQ == self.operator;
        let is_non_equal_op = Self::OP_NE == self.operator;
        let is_provider_equal_op = Self::OP_EQ == provider.operator;
        let is_provider_non_equal_op = Self::OP_NE == provider.operator;

        if is_non_equal_op || is_provider_non_equal_op {
            if is_non_equal_op
                && !is_provider_non_equal_op
                && !is_provider_equal_op
                && provider.version.starts_with("dev-")
            {
                return false;
            }

            if is_provider_non_equal_op
                && !is_non_equal_op
                && !is_equal_op
                && self.version.starts_with("dev-")
            {
                return false;
            }

            if !is_equal_op && !is_provider_equal_op {
                return true;
            }
            return self
                .version_compare(&provider.version, &self.version, "!=", compare_branches)
                .expect("valid operator");
        }

        if self.operator != Self::OP_EQ && no_equal_op == provider_no_equal_op {
            return !(self.version.starts_with("dev-") || provider.version.starts_with("dev-"));
        }

        let (version1, version2, operator) = if is_equal_op {
            (&self.version, &provider.version, provider.operator)
        } else {
            (&provider.version, &self.version, self.operator)
        };

        if self
            .version_compare(
                version1,
                version2,
                Self::trans_op_int(operator),
                compare_branches,
            )
            .expect("valid operator")
        {
            return !(Self::trans_op_int(provider.operator) == provider_no_equal_op
                && Self::trans_op_int(self.operator) != no_equal_op
                && php::version_compare(&provider.version, &self.version, "=="));
        }

        false
    }

    /// Composer memoizes the result; this port recomputes on every call. It is not heavy
    /// calculation so caching is a premature optimization.
    fn extract_bounds(&self) -> (Bound, Bound) {
        if self.version.starts_with("dev-") {
            return (Bound::zero(), Bound::positive_infinity());
        }

        match self.operator {
            Self::OP_EQ => (
                Bound::new(self.version.clone(), true),
                Bound::new(self.version.clone(), true),
            ),
            Self::OP_LT => (Bound::zero(), Bound::new(self.version.clone(), false)),
            Self::OP_LE => (Bound::zero(), Bound::new(self.version.clone(), true)),
            Self::OP_GT => (
                Bound::new(self.version.clone(), false),
                Bound::positive_infinity(),
            ),
            Self::OP_GE => (
                Bound::new(self.version.clone(), true),
                Bound::positive_infinity(),
            ),
            Self::OP_NE => (Bound::zero(), Bound::positive_infinity()),
            _ => panic!("unknown operator: {}", self.operator),
        }
    }

    pub fn compile(&self, other_operator: i64) -> String {
        self.compile_constraint(other_operator)
    }

    pub fn get_pretty_string(&self) -> String {
        if let Some(ref s) = self.pretty_string
            && !s.is_empty()
        {
            return s.clone();
        }
        self.to_string()
    }

    pub fn get_lower_bound(&self) -> Bound {
        self.extract_bounds().0
    }

    pub fn get_upper_bound(&self) -> Bound {
        self.extract_bounds().1
    }
}

impl std::fmt::Display for SimpleConstraint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", Self::trans_op_int(self.operator), self.version)
    }
}