aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-semver/src/version_parser.rs
blob: ba2b59d3fd64c928bb6d64091d1f8e4a51fcc748 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! ref: composer/vendor/composer/semver/src/VersionParser.php

use crate::constraint::Constraint;
use crate::constraint::ConstraintInterface;
use crate::constraint::MatchAllConstraint;
use crate::constraint::MultiConstraint;
use shirabe_php_shim as php;

// Regex to match pre-release data (sort of).
//
// Due to backwards compatibility:
//   - Instead of enforcing hyphen, an underscore, dot or nothing at all are also accepted.
//   - Only stabilities as recognized by Composer are allowed to precede a numerical identifier.
//   - Numerical-only pre-release identifiers are not supported, see tests.
//
//                        |--------------|
// [major].[minor].[patch] -[pre-release] +[build-metadata]
const MODIFIER_REGEX: &str =
    "[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)((?:[.-]?\\d+)*+)?)?([.-]?dev)?";

const STABILITIES_REGEX: &str = "stable|RC|beta|alpha|dev";

#[derive(Debug, Clone)]
pub struct VersionParser;

impl VersionParser {
    pub fn parse_stability(version: &str) -> String {
        let version = php::preg_replace("{#.+$}", "", version).unwrap_or_default();

        if version.starts_with("dev-") || version.ends_with("-dev") {
            return "dev".to_string();
        }

        let pattern = format!("{{{}(?:\\+.*)?$}}i", MODIFIER_REGEX);
        let lower = php::strtolower(&version);
        let mut match_: Vec<Option<String>> = Vec::new();
        php::preg_match(&pattern, &lower, &mut match_);

        // match_[3] = the ([.-]?dev)? capture
        if match_
            .get(3)
            .and_then(|o| o.as_deref())
            .is_some_and(|s| !s.is_empty())
        {
            return "dev".to_string();
        }

        // match_[1] = the (stable|beta|b|RC|alpha|a|patch|pl|p) capture
        let m1 = match_.get(1).and_then(|o| o.as_deref()).unwrap_or("");
        if !m1.is_empty() {
            if m1 == "beta" || m1 == "b" {
                return "beta".to_string();
            }
            if m1 == "alpha" || m1 == "a" {
                return "alpha".to_string();
            }
            if m1 == "rc" {
                return "RC".to_string();
            }
        }

        "stable".to_string()
    }

    pub fn normalize_stability(stability: &str) -> anyhow::Result<String> {
        let stability = php::strtolower(stability);

        if !["stable", "rc", "beta", "alpha", "dev"].contains(&stability.as_str()) {
            anyhow::bail!(
                "Invalid stability string \"{}\", expected one of stable, RC, beta, alpha or dev",
                stability
            );
        }

        Ok(if stability == "rc" {
            "RC".to_string()
        } else {
            stability
        })
    }

    pub fn normalize(&self, version: &str, full_version: Option<&str>) -> anyhow::Result<String> {
        let version = php::trim(version, None);
        let orig_version = version.clone();
        let full_version = full_version.unwrap_or(&version).to_string();
        let mut version = version;

        // strip off aliasing
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match("{^([^,\\s]++) ++as ++([^,\\s]++)$}", &version, &mut match_) > 0 {
            version = match_[1].clone().unwrap_or_default();
        }

        // strip off stability flag
        let stab_pattern = format!("{{@(?:{})$}}i", STABILITIES_REGEX);
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match(&stab_pattern, &version, &mut match_) > 0 {
            let match0_len = match_[0].as_deref().unwrap_or("").len();
            version = version[..version.len() - match0_len].to_string();
        }

        // normalize master/trunk/default branches to dev-name for BC with 1.x as these used to
        // be valid constraints
        if ["master", "trunk", "default"].contains(&version.as_str()) {
            version = format!("dev-{}", version);
        }

        // if requirement is branch-like, use full name
        if version.to_ascii_lowercase().starts_with("dev-") {
            return Ok(format!("dev-{}", &version[4..]));
        }

        // strip off build metadata
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match("{^([^,\\s+]++)\\+[^\\s]++$}", &version, &mut match_) > 0 {
            version = match_[1].clone().unwrap_or_default();
        }

        let mut index: Option<usize> = None;
        let mut matches: Vec<Option<String>> = Vec::new();

        // match classical versioning
        let classical_pattern = format!(
            "{{^v?(\\d{{1,5}}+)(\\.\\d++)?(\\.\\d++)?(\\.\\d++)?{}$}}i",
            MODIFIER_REGEX
        );
        if php::preg_match(&classical_pattern, &version, &mut matches) > 0 {
            let m2 = matches[2].as_deref().unwrap_or("");
            let m3 = matches[3].as_deref().unwrap_or("");
            let m4 = matches[4].as_deref().unwrap_or("");
            version = format!(
                "{}{}{}{}",
                matches[1].as_deref().unwrap_or(""),
                if m2.is_empty() { ".0" } else { m2 },
                if m3.is_empty() { ".0" } else { m3 },
                if m4.is_empty() { ".0" } else { m4 },
            );
            index = Some(5);
        } else {
            // match date(time) based versioning
            let datetime_pattern = format!(
                "{{^v?(\\d{{4}}(?:[.:-]?\\d{{2}}){{1,6}}(?:[.:-]?\\d{{1,3}}){{0,2}}){}$}}i",
                MODIFIER_REGEX
            );
            if php::preg_match(&datetime_pattern, &version, &mut matches) > 0 {
                version = php::preg_replace("{\\D}", ".", matches[1].as_deref().unwrap_or(""))
                    .unwrap_or_default();
                index = Some(2);
            }
        }

        // add version modifiers if a version was matched
        if let Some(idx) = index {
            let mi = matches.get(idx).and_then(|o| o.as_deref()).unwrap_or("");
            if !mi.is_empty() {
                if mi == "stable" {
                    return Ok(version);
                }
                let mi1 = matches
                    .get(idx + 1)
                    .and_then(|o| o.as_deref())
                    .unwrap_or("");
                version = format!(
                    "{}-{}{}",
                    version,
                    self.expand_stability(mi),
                    if !mi1.is_empty() {
                        mi1.trim_start_matches(['.', '-'])
                    } else {
                        ""
                    }
                );
            }

            if !matches
                .get(idx + 2)
                .and_then(|o| o.as_deref())
                .unwrap_or("")
                .is_empty()
            {
                version = format!("{}-dev", version);
            }

            return Ok(version);
        }

        // match dev branches
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match("{(.*?)[.-]?dev$}i", &version, &mut match_) > 0 {
            let branch_name = match_[1].clone().unwrap_or_default();
            // a branch ending with -dev is only valid if it is numeric
            // if it gets prefixed with dev- it means the branch name should
            // have had a dev- prefix already when passed to normalize
            if let Ok(normalized) = self.normalize_branch(&branch_name)
                && !normalized.starts_with("dev-")
            {
                return Ok(normalized);
            }
        }

        let extra_message = if php::preg_match(
            &format!(
                "{{ +as +{}(?:@(?:{}))?$}}",
                php::preg_quote(&version, None),
                STABILITIES_REGEX
            ),
            &full_version,
            &mut Vec::new(),
        ) > 0
        {
            format!(
                " in \"{}\", the alias must be an exact version",
                full_version
            )
        } else if php::preg_match(
            &format!(
                "{{^{}(?:@(?:{}))?  +as +}}",
                php::preg_quote(&version, None),
                STABILITIES_REGEX
            ),
            &full_version,
            &mut Vec::new(),
        ) > 0
        {
            format!(
                " in \"{}\", the alias source must be an exact version, if it is a branch name \
                you should prefix it with dev-",
                full_version
            )
        } else {
            String::new()
        };

        anyhow::bail!(
            "Invalid version string \"{}\"{}",
            orig_version,
            extra_message
        )
    }

    pub fn parse_numeric_alias_prefix(&self, branch: &str) -> Option<String> {
        let mut matches: Vec<Option<String>> = Vec::new();
        // matches['version'] == matches[1] ((?P<version>...) is group 1)
        if php::preg_match(
            "{^(?P<version>(\\d++\\.)*\\d++)(?:\\.x)?-dev$}i",
            branch,
            &mut matches,
        ) > 0
        {
            let version = matches[1].clone().unwrap_or_default();
            return Some(format!("{}.", version));
        }

        None
    }

    pub fn normalize_branch(&self, name: &str) -> anyhow::Result<String> {
        let name = php::trim(name, None);

        let mut matches: Vec<Option<String>> = Vec::new();
        // Groups: 1=major, 2=".minor"(outer), 3=minor(inner), 4=".patch"(outer),
        // 5=patch(inner), 6=".fourth"(outer), 7=fourth(inner).
        // We use the outer groups [1,2,4,6] to replicate PHP's groups [1,2,3,4].
        if php::preg_match(
            "{^v?(\\d++)(\\.(\\d++|[xX*]))?(\\.(\\d++|[xX*]))?(\\.(\\d++|[xX*]))?$}i",
            &name,
            &mut matches,
        ) > 0
        {
            let mut version = String::new();
            for i in [1usize, 2, 4, 6] {
                if let Some(Some(m)) = matches.get(i) {
                    version.push_str(&m.replace(['*', 'X'], "x"));
                } else {
                    version.push_str(".x");
                }
            }

            return Ok(format!("{}-dev", version.replace('x', "9999999")));
        }

        Ok(format!("dev-{}", name))
    }

    #[deprecated(
        note = "No need to use this anymore in theory, Composer 2 does not normalize any \
            branch names to 9999999-dev anymore"
    )]
    pub fn normalize_default_branch(&self, name: &str) -> String {
        if name == "dev-master" || name == "dev-default" || name == "dev-trunk" {
            return "9999999-dev".to_string();
        }

        name.to_string()
    }

    pub fn parse_constraints(
        &self,
        constraints: &str,
    ) -> anyhow::Result<Box<dyn ConstraintInterface>> {
        let pretty_constraint = constraints.to_string();

        let or_constraints = php::preg_split("{\\s*\\|\\|?\\s*}", &php::trim(constraints, None))
            .ok_or_else(|| anyhow::anyhow!("Failed to preg_split string: {}", constraints))?;

        let mut or_groups: Vec<Box<dyn ConstraintInterface>> = Vec::new();

        for or_constraint in &or_constraints {
            let and_constraints = php::preg_split(
                "{(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)}",
                or_constraint,
            )
            .ok_or_else(|| anyhow::anyhow!("Failed to preg_split string: {}", or_constraint))?;

            let constraint_objects: Vec<Box<dyn ConstraintInterface>> = if and_constraints.len() > 1
            {
                let mut objs: Vec<Box<dyn ConstraintInterface>> = Vec::new();
                for and_constraint in &and_constraints {
                    for parsed in self.parse_constraint(and_constraint)? {
                        objs.push(parsed);
                    }
                }
                objs
            } else {
                self.parse_constraint(&and_constraints[0])?
            };

            let constraint: Box<dyn ConstraintInterface> = if constraint_objects.len() == 1 {
                constraint_objects.into_iter().next().unwrap()
            } else {
                Box::new(MultiConstraint::new(constraint_objects, true))
            };

            or_groups.push(constraint);
        }

        let mut parsed_constraint = MultiConstraint::create(or_groups, false)?;

        parsed_constraint.set_pretty_string(Some(pretty_constraint));

        Ok(parsed_constraint)
    }

    fn parse_constraint(
        &self,
        constraint: &str,
    ) -> anyhow::Result<Vec<Box<dyn ConstraintInterface>>> {
        let mut constraint = constraint.to_string();

        // strip off aliasing
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match(
            "{^([^,\\s]++) ++as ++([^,\\s]++)$}",
            &constraint,
            &mut match_,
        ) > 0
        {
            constraint = match_[1].clone().unwrap_or_default();
        }

        // strip @stability flags, and keep it for later use
        let mut stability_modifier: Option<String> = None;
        let mut match_: Vec<Option<String>> = Vec::new();
        let stab_pattern = format!("{{^([^,\\s]*?)@({})$}}i", STABILITIES_REGEX);
        if php::preg_match(&stab_pattern, &constraint, &mut match_) > 0 {
            let m1 = match_[1].as_deref().unwrap_or("");
            constraint = if !m1.is_empty() {
                m1.to_string()
            } else {
                "*".to_string()
            };
            let m2 = match_[2].as_deref().unwrap_or("");
            if m2 != "stable" {
                stability_modifier = Some(m2.to_string());
            }
        }

        // get rid of #refs as those are used by composer only
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match(
            "{^(dev-[^,\\s@]+?|[^,\\s@]+?\\.x-dev)#.+$}i",
            &constraint,
            &mut match_,
        ) > 0
        {
            constraint = match_[1].clone().unwrap_or_default();
        }

        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match("{^(v)?[xX*](\\.[xX*])*$}i", &constraint, &mut match_) > 0 {
            let m1_nonempty = !match_
                .get(1)
                .and_then(|o| o.as_deref())
                .unwrap_or("")
                .is_empty();
            let m2_nonempty = !match_
                .get(2)
                .and_then(|o| o.as_deref())
                .unwrap_or("")
                .is_empty();
            if m1_nonempty || m2_nonempty {
                return Ok(vec![Box::new(Constraint::new(
                    ">=".to_string(),
                    "0.0.0.0-dev".to_string(),
                ))]);
            }

            return Ok(vec![Box::new(MatchAllConstraint {
                pretty_string: None,
            })]);
        }

        let version_regex = format!(
            "v?(\\d++)(?:\\.(\\d++))?(?:\\.(\\d++))?(?:\\.(\\d++))?(?:{}|\\.([xX*][.-]?dev))(?:\\+[^\\s]+)?",
            MODIFIER_REGEX
        );

        // Tilde Range
        //
        // Like wildcard constraints, unsuffixed tilde constraints say that they must be greater
        // than the previous version, to ensure that unstable instances of the current version are
        // allowed. However, if a stability suffix is added to the constraint, then a >= match on
        // the current version is used instead.
        let mut matches: Vec<Option<String>> = Vec::new();
        let tilde_pattern = format!("{{^~>?{}$}}i", version_regex);
        if php::preg_match(&tilde_pattern, &constraint, &mut matches) > 0 {
            if constraint.starts_with("~>") {
                anyhow::bail!(
                    "Could not parse version constraint {}: Invalid operator \"~>\", you probably \
                    meant to use the \"~\" operator",
                    constraint
                );
            }

            // Work out which position in the version we are operating at
            let mut position = if !matches[4].as_deref().unwrap_or("").is_empty() {
                4
            } else if !matches[3].as_deref().unwrap_or("").is_empty() {
                3
            } else if !matches[2].as_deref().unwrap_or("").is_empty() {
                2
            } else {
                1
            };

            // when matching 2.x-dev or 3.0.x-dev we have to shift the second or third number,
            // despite no second/third number matching above
            if !matches[8].as_deref().unwrap_or("").is_empty() {
                position += 1;
            }

            // Calculate the stability suffix
            let stability_suffix = if matches[5].as_deref().unwrap_or("").is_empty()
                && matches[7].as_deref().unwrap_or("").is_empty()
                && matches[8].as_deref().unwrap_or("").is_empty()
            {
                "-dev"
            } else {
                ""
            };

            let low_version =
                self.normalize(&format!("{}{}", &constraint[1..], stability_suffix), None)?;
            let lower_bound = Constraint::new(">=".to_string(), low_version);

            // For upper bound, we increment the position of one more significance,
            // but highPosition = 0 would be illegal
            let high_position = std::cmp::max(1, position - 1);
            let high_version = format!(
                "{}-dev",
                self.manipulate_version_string(&matches, high_position, 1, "0")
                    .unwrap_or_default()
            );
            let upper_bound = Constraint::new("<".to_string(), high_version);

            return Ok(vec![Box::new(lower_bound), Box::new(upper_bound)]);
        }

        // Caret Range
        //
        // Allows changes that do not modify the left-most non-zero digit in the [major, minor,
        // patch] tuple. In other words, this allows patch and minor updates for versions 1.0.0
        // and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X
        let mut matches: Vec<Option<String>> = Vec::new();
        let caret_pattern = format!("{{^\\^{}($)}}i", version_regex);
        if php::preg_match(&caret_pattern, &constraint, &mut matches) > 0 {
            // Work out which position in the version we are operating at
            let m1 = matches[1].as_deref().unwrap_or("");
            let m2 = matches[2].as_deref().unwrap_or("");
            let m3 = matches[3].as_deref().unwrap_or("");
            let position = if m1 != "0" || m2.is_empty() {
                1
            } else if m2 != "0" || m3.is_empty() {
                2
            } else {
                3
            };

            // Calculate the stability suffix
            let stability_suffix = if matches[5].as_deref().unwrap_or("").is_empty()
                && matches[7].as_deref().unwrap_or("").is_empty()
                && matches[8].as_deref().unwrap_or("").is_empty()
            {
                "-dev"
            } else {
                ""
            };

            let low_version =
                self.normalize(&format!("{}{}", &constraint[1..], stability_suffix), None)?;
            let lower_bound = Constraint::new(">=".to_string(), low_version);

            // For upper bound, we increment the position of one more significance,
            // but highPosition = 0 would be illegal
            let high_version = format!(
                "{}-dev",
                self.manipulate_version_string(&matches, position, 1, "0")
                    .unwrap_or_default()
            );
            let upper_bound = Constraint::new("<".to_string(), high_version);

            return Ok(vec![Box::new(lower_bound), Box::new(upper_bound)]);
        }

        // X Range
        //
        // Any of X, x, or * may be used to "stand in" for one of the numeric values in the
        // [major, minor, patch] tuple. A partial version range is treated as an X-Range, so the
        // special character is in fact optional.
        let mut matches: Vec<Option<String>> = Vec::new();
        if php::preg_match(
            "{^v?(\\d++)(?:\\.(\\d++))?(?:\\.(\\d++))?(?:\\.[xX*])++$}",
            &constraint,
            &mut matches,
        ) > 0
        {
            let position = if !matches[3].as_deref().unwrap_or("").is_empty() {
                3
            } else if !matches[2].as_deref().unwrap_or("").is_empty() {
                2
            } else {
                1
            };

            let low_version = format!(
                "{}-dev",
                self.manipulate_version_string(&matches, position, 0, "0")
                    .unwrap_or_default()
            );
            let high_version = format!(
                "{}-dev",
                self.manipulate_version_string(&matches, position, 1, "0")
                    .unwrap_or_default()
            );

            if low_version == "0.0.0.0-dev" {
                return Ok(vec![Box::new(Constraint::new(
                    "<".to_string(),
                    high_version,
                ))]);
            }

            return Ok(vec![
                Box::new(Constraint::new(">=".to_string(), low_version)),
                Box::new(Constraint::new("<".to_string(), high_version)),
            ]);
        }

        // Hyphen Range
        //
        // Specifies an inclusive set. If a partial version is provided as the first version in
        // the inclusive range, then the missing pieces are replaced with zeroes. If a partial
        // version is provided as the second version in the inclusive range, then all versions
        // that start with the supplied parts of the tuple are accepted, but nothing that would
        // be greater than the provided tuple parts.
        let mut matches: Vec<Option<String>> = Vec::new();
        let hyphen_pattern = format!(
            "{{^(?P<from>{}) +- +(?P<to>{})($)}}i",
            version_regex, version_regex
        );
        if php::preg_match(&hyphen_pattern, &constraint, &mut matches) > 0 {
            // matches[1]='from' string, matches[2..9]=from captures, matches[10]='to' string,
            // matches[11..18]=to captures, matches[19]='($)'
            // matches[6]=from stability, matches[8]=from dev, matches[9]=from wildcard-dev
            let low_stability_suffix = if matches[6].as_deref().unwrap_or("").is_empty()
                && matches[8].as_deref().unwrap_or("").is_empty()
                && matches[9].as_deref().unwrap_or("").is_empty()
            {
                "-dev"
            } else {
                ""
            };

            let from_str = matches[1].clone().unwrap_or_default(); // matches['from']
            let low_version = self.normalize(&from_str, None)?;
            let lower_bound = Constraint::new(
                ">=".to_string(),
                format!("{}{}", low_version, low_stability_suffix),
            );

            // PHP's empty() on "0" returns true, but here we only check for truly empty/missing
            let empty = |x: &Option<String>| -> bool { x.as_deref().is_none_or(|s| s.is_empty()) };

            // matches[12]=to minor, matches[13]=to patch, matches[15]=to stability,
            // matches[17]=to dev, matches[18]=to wildcard-dev
            let upper_bound: Constraint = if (!empty(&matches[12]) && !empty(&matches[13]))
                || !matches[15].as_deref().unwrap_or("").is_empty()
                || !matches[17].as_deref().unwrap_or("").is_empty()
                || !matches[18].as_deref().unwrap_or("").is_empty()
            {
                let to_str = matches[10].clone().unwrap_or_default(); // matches['to']
                let hv = self.normalize(&to_str, None)?;
                Constraint::new("<=".to_string(), hv)
            } else {
                // matches[11]=to major, matches[12]=to minor, matches[13]=to patch,
                // matches[14]=to fourth
                let high_match = vec![
                    Some(String::new()),
                    matches[11].clone(),
                    matches[12].clone(),
                    matches[13].clone(),
                    matches[14].clone(),
                ];

                // validate to version
                let to_str = matches[10].clone().unwrap_or_default(); // matches['to']
                self.normalize(&to_str, None)?;

                let position = if empty(&matches[12]) { 1 } else { 2 };
                let hv = format!(
                    "{}-dev",
                    self.manipulate_version_string(&high_match, position, 1, "0")
                        .unwrap_or_default()
                );
                Constraint::new("<".to_string(), hv)
            };

            return Ok(vec![Box::new(lower_bound), Box::new(upper_bound)]);
        }

        // Basic Comparators
        let mut match_: Vec<Option<String>> = Vec::new();
        if php::preg_match("{^(<>|!=|>=?|<=?|==?)?\\s*(.*)}", &constraint, &mut match_) > 0 {
            let version_str = match_[2].clone().unwrap_or_default();
            let op_str = match_[1].clone().unwrap_or_default();

            let version_result: anyhow::Result<String> = match self.normalize(&version_str, None) {
                Ok(v) => Ok(v),
                Err(e) => {
                    // recover from an invalid constraint like foobar-dev which should be
                    // dev-foobar except if the constraint uses a known operator, in which
                    // case it must be a parse error
                    if version_str.ends_with("-dev")
                        && php::preg_match("{^[0-9a-zA-Z-./]+$}", &version_str, &mut Vec::new()) > 0
                    {
                        self.normalize(
                            &format!("dev-{}", &version_str[..version_str.len() - 4]),
                            None,
                        )
                    } else {
                        Err(e)
                    }
                }
            };

            if let Ok(mut version) = version_result {
                let op = if op_str.is_empty() { "=" } else { &op_str };

                if op != "=="
                    && op != "="
                    && let Some(ref stab_mod) = stability_modifier
                    && Self::parse_stability(&version) == "stable"
                {
                    version = format!("{}-{}", version, stab_mod);
                }
                if op == "<" || op == ">=" {
                    let modifier_pattern = format!("{{-{}$}}", MODIFIER_REGEX);
                    if php::preg_match(
                        &modifier_pattern,
                        &php::strtolower(&version_str),
                        &mut Vec::new(),
                    ) == 0
                        && !version_str.starts_with("dev-")
                    {
                        version = format!("{}-dev", version);
                    }
                }

                let final_op = if op_str.is_empty() {
                    "=".to_string()
                } else {
                    op_str
                };
                return Ok(vec![Box::new(Constraint::new(final_op, version))]);
            }
        }

        anyhow::bail!("Could not parse version constraint {}", constraint)
    }

    fn manipulate_version_string(
        &self,
        matches: &[Option<String>],
        position: usize,
        increment: i64,
        pad: &str,
    ) -> Option<String> {
        let mut parts: [i64; 5] = [
            0,
            matches
                .get(1)
                .and_then(|o| o.as_deref())
                .unwrap_or("0")
                .parse()
                .unwrap_or(0),
            matches
                .get(2)
                .and_then(|o| o.as_deref())
                .unwrap_or("0")
                .parse()
                .unwrap_or(0),
            matches
                .get(3)
                .and_then(|o| o.as_deref())
                .unwrap_or("0")
                .parse()
                .unwrap_or(0),
            matches
                .get(4)
                .and_then(|o| o.as_deref())
                .unwrap_or("0")
                .parse()
                .unwrap_or(0),
        ];
        let pad_val: i64 = pad.parse().unwrap_or(0);
        let mut position = position;

        for i in (1..=4).rev() {
            if i > position {
                parts[i] = pad_val;
            } else if i == position && increment != 0 {
                parts[i] += increment;
                // If $matches[$i] was 0, carry the decrement
                if parts[i] < 0 {
                    parts[i] = pad_val;
                    // Return null on a carry overflow
                    if i == 1 {
                        return None;
                    }
                    position -= 1;
                }
            }
        }

        Some(format!(
            "{}.{}.{}.{}",
            parts[1], parts[2], parts[3], parts[4]
        ))
    }

    fn expand_stability(&self, stability: &str) -> String {
        let stability = stability.to_ascii_lowercase();

        match stability.as_str() {
            "a" => "alpha".to_string(),
            "b" => "beta".to_string(),
            "p" | "pl" => "patch".to_string(),
            "rc" => "RC".to_string(),
            _ => stability,
        }
    }
}