version_spec 0.11.3

A specification for working with partial, full, or aliased versions. Supports semver and calver.
Documentation
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
use crate::is_calver_like;
use crate::spec_error::SpecError;
use crate::syntax_parser::*;
use crate::syntax_traits::{FormatOptions, FormatsVersion};
use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt::{self, Display};
use std::str::FromStr;

/// The kind of version, either calendar or semantic.
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum VersionKind {
    /// A calendar version, typically in the form of `YYYY-MM-DD` or `YYYY-MM`.
    Calendar,

    /// A semantic version, typically in the form of `MAJOR.MINOR.PATCH`.
    #[default]
    Semantic,
}

/// A version in either calendar or semantic format, with support for
/// scopes, pre-releases, and build metadata.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(try_from = "String", into = "String")]
pub struct Version {
    /// The kind of version.
    pub kind: VersionKind,

    /// An optional scope prefix, for example the "vendor" in `vendor-1.2.3`.
    /// Does not include the trailing `-`.
    pub scope: Option<CompactString>,

    /// The major version number, or the year for calendar versions.
    pub major: u32,

    /// The minor version number, or the month for calendar versions.
    pub minor: u32,

    /// The patch version number, or the day for calendar versions,
    /// in which a day of 0 means it was not defined.
    pub patch: u32,

    /// Optional pre-release identifier, for example the "alpha.1"
    /// in `1.2.3-alpha.1`. Does not include the leading `-`.
    pub prerelease: Option<CompactString>,

    /// Optional build metadata, for example the "build.5" in `1.2.3+build.5`.
    /// Does not include the leading `+`.
    pub build: Option<CompactString>,
}

impl Version {
    /// Creates a semantic version from the provided major, minor,
    /// and patch numbers.
    pub fn new(major: u32, minor: u32, patch: u32) -> Self {
        Self::semantic(major, minor, patch)
    }

    /// Creates a calendar version from the provided year, month, and day.
    /// Short years are expanded from the year 2000, while months and days
    /// are clamped to valid ranges.
    pub fn calendar(year: u32, month: u32, day: u32) -> Self {
        Self {
            kind: VersionKind::Calendar,
            major: calendar_year(year),
            minor: month.clamp(1, 12),
            patch: day.clamp(1, 31),
            ..Default::default()
        }
    }

    /// Creates a semantic version from the provided major, minor,
    /// and patch numbers.
    pub fn semantic(major: u32, minor: u32, patch: u32) -> Self {
        Self {
            kind: VersionKind::Semantic,
            major,
            minor,
            patch,
            ..Default::default()
        }
    }

    /// Parses the provided value into a version.
    pub fn parse<T: AsRef<str>>(value: T) -> Result<Self, SpecError> {
        let value = value.as_ref();

        // The calendar check may false-positive on inner version parts,
        // like the "20.3" in "10.20.30", so fall back to semantic
        if is_calver_like(value) {
            parse_calver(value).or_else(|_| parse_semver(value))
        } else {
            parse_semver(value)
        }
        .map_err(|error| SpecError::FailedVersionParse {
            error: Box::new(error),
        })
    }

    /// Return true if the version is a calendar version.
    pub fn is_calendar(&self) -> bool {
        self.kind == VersionKind::Calendar
    }

    /// Return true if the version is a semantic version.
    pub fn is_semantic(&self) -> bool {
        self.kind == VersionKind::Semantic
    }

    /// Converts this version into a requirement with the provided operator.
    pub fn to_requirement(&self, op: Op) -> Requirement {
        Requirement {
            kind: self.kind,
            op,
            scope: self.scope.clone(),
            major: Some(self.major),
            minor: Some(self.minor),
            patch: if self.kind == VersionKind::Calendar && self.patch == 0 {
                None
            } else {
                Some(self.patch)
            },
            prerelease: self.prerelease.clone(),
        }
    }
}

impl Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            self.to_formatted_string(&match self.kind {
                VersionKind::Calendar => FormatOptions {
                    include_patch: self.patch > 0,
                    ..FormatOptions::calendar()
                },
                VersionKind::Semantic => FormatOptions::semantic(),
            })
        )
    }
}

impl Ord for Version {
    fn cmp(&self, other: &Self) -> Ordering {
        self.kind
            .cmp(&other.kind)
            .then_with(|| self.scope.cmp(&other.scope))
            .then_with(|| self.major.cmp(&other.major))
            .then_with(|| self.minor.cmp(&other.minor))
            .then_with(|| self.patch.cmp(&other.patch))
            .then_with(|| {
                compare_prerelease(self.prerelease.as_deref(), other.prerelease.as_deref())
            })
            .then_with(|| compare_build(self.build.as_deref(), other.build.as_deref()))
    }
}

impl PartialOrd for Version {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl From<Version> for String {
    fn from(value: Version) -> Self {
        value.to_string()
    }
}

impl TryFrom<String> for Version {
    type Error = SpecError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::parse(&value)
    }
}

impl FromStr for Version {
    type Err = SpecError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::parse(value)
    }
}

#[cfg(feature = "schematic")]
impl schematic::Schematic for Version {
    fn schema_name() -> Option<String> {
        Some("Version".into())
    }

    fn build_schema(mut schema: schematic::SchemaBuilder) -> schematic::Schema {
        schema.string_default()
    }
}

/// The comparison operator of a requirement.
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum Op {
    /// An exact match (`=` or `==`).
    Exact,

    /// A greater than match (`>`).
    Greater,

    /// A greater than or equal match (`>=`).
    GreaterEq,

    /// A less than match (`<`).
    Less,

    /// A less than or equal match (`<=`).
    LessEq,

    /// A patch-level match (`~`).
    /// This is the default operator when one is not defined.
    #[default]
    Tilde,

    /// A compatible, up to the next major version, match (`^`).
    Caret,

    /// Matches any version (`*`, `x`, or `X`).
    Wildcard,
}

impl Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match self {
            Op::Exact => "=",
            Op::Greater => ">",
            Op::GreaterEq => ">=",
            Op::Less => "<",
            Op::LessEq => "<=",
            Op::Tilde => "~",
            Op::Caret => "^",
            Op::Wildcard => "",
        })
    }
}

/// A version requirement composed of a comparison operator and a full
/// or partial version to match against. Build metadata is accepted
/// when parsing, but is otherwise ignored.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(try_from = "String", into = "String")]
pub struct Requirement {
    /// The kind of version.
    pub kind: VersionKind,

    /// The comparison operator.
    pub op: Op,

    /// An optional scope prefix, for example the "vendor" in `vendor-1.2`.
    pub scope: Option<CompactString>,

    /// The major version number, or the year for calendar versions.
    /// A `None` is either an omitted part or a wildcard.
    pub major: Option<u32>,

    /// The minor version number, or the month for calendar versions.
    /// A `None` is either an omitted part or a wildcard.
    pub minor: Option<u32>,

    /// The patch version number, or the day for calendar versions.
    /// A `None` is either an omitted part or a wildcard.
    pub patch: Option<u32>,

    /// Optional pre-release identifier, for example the "alpha.1"
    /// in `>=1.2.3-alpha.1`.
    pub prerelease: Option<CompactString>,
}

impl Requirement {
    /// Parses the provided value into a requirement.
    pub fn parse<T: AsRef<str>>(value: T) -> Result<Self, SpecError> {
        let value = value.as_ref();

        // The calendar check may false-positive on inner version parts,
        // like the "16-1" in "node-16-1.2", so fall back to semantic
        if is_calver_like(value) {
            parse_calver_req(value).or_else(|_| parse_semver_req(value))
        } else {
            parse_semver_req(value)
        }
        .map_err(|error| SpecError::FailedVersionRequirementParse {
            error: Box::new(error),
        })
    }

    /// Returns true if the provided version satisfies the requirement's
    /// operator, without checking pre-release compatibility. A scoped
    /// requirement only matches versions with the same scope, while an
    /// unscoped requirement matches any scope.
    pub fn matches_op(&self, version: &Version) -> bool {
        if self.scope.is_some() && self.scope != version.scope {
            return false;
        }

        match self.op {
            Op::Exact | Op::Wildcard => self.matches_exact(version),
            Op::Greater => self.matches_greater(version),
            Op::GreaterEq => self.matches_exact(version) || self.matches_greater(version),
            Op::Less => self.matches_less(version),
            Op::LessEq => self.matches_exact(version) || self.matches_less(version),
            Op::Tilde => self.matches_tilde(version),
            Op::Caret => self.matches_caret(version),
        }
    }

    /// Returns true if the provided version exactly matches all defined
    /// parts of this requirement, including the pre-release. Omitted
    /// parts match any value.
    pub fn matches_exact(&self, version: &Version) -> bool {
        if let Some(major) = self.major {
            if version.major != major {
                return false;
            }
        }

        if let Some(minor) = self.minor {
            if version.minor != minor {
                return false;
            }
        }

        if let Some(micro) = self.patch {
            if version.patch != micro {
                return false;
            }
        }

        self.prerelease == version.prerelease
    }

    /// Returns true if the provided version is greater (`>`) than this
    /// requirement. A partial requirement only matches versions beyond
    /// the defined parts, for example `>1` does not match `1.5.0`.
    pub fn matches_greater(&self, version: &Version) -> bool {
        let Some(major) = self.major else {
            return false;
        };

        if version.major != major {
            return version.major > major;
        }

        let Some(minor) = self.minor else {
            return false;
        };

        if version.minor != minor {
            return version.minor > minor;
        }

        let Some(micro) = self.patch else {
            return false;
        };

        if version.patch != micro {
            return version.patch > micro;
        }

        compare_prerelease(version.prerelease.as_deref(), self.prerelease.as_deref())
            == Ordering::Greater
    }

    /// Returns true if the provided version is less (`<`) than this requirement.
    /// A partial requirement only matches versions below the defined parts.
    pub fn matches_less(&self, version: &Version) -> bool {
        let Some(major) = self.major else {
            return false;
        };

        if version.major != major {
            return version.major < major;
        }

        let Some(minor) = self.minor else {
            return false;
        };

        if version.minor != minor {
            return version.minor < minor;
        }

        let Some(micro) = self.patch else {
            return false;
        };

        if version.patch != micro {
            return version.patch < micro;
        }

        compare_prerelease(version.prerelease.as_deref(), self.prerelease.as_deref())
            == Ordering::Less
    }

    /// Returns true for a patch-level (`~`) match: the defined major and
    /// minor parts must be equal, while the remaining parts may drift higher.
    pub fn matches_tilde(&self, version: &Version) -> bool {
        let Some(major) = self.major else {
            return true;
        };

        if version.major != major {
            return false;
        }

        if let Some(minor) = self.minor {
            if version.minor != minor {
                return false;
            }
        }

        if let Some(micro) = self.patch {
            if version.patch != micro {
                return version.patch > micro;
            }
        }

        compare_prerelease(version.prerelease.as_deref(), self.prerelease.as_deref())
            != Ordering::Less
    }

    /// Returns true for a compatible (`^`) match: parts may drift up to
    /// the next major version, or the next minor or patch version when
    /// the major or minor is 0.
    pub fn matches_caret(&self, version: &Version) -> bool {
        let Some(major) = self.major else {
            return true;
        };

        if version.major != major {
            return false;
        }

        let Some(minor) = self.minor else {
            return true;
        };

        let Some(micro) = self.patch else {
            return if major > 0 {
                version.minor >= minor
            } else {
                version.minor == minor
            };
        };

        if major > 0 {
            if version.minor != minor {
                return version.minor > minor;
            } else if version.patch != micro {
                return version.patch > micro;
            }
        } else if minor > 0 {
            if version.minor != minor {
                return false;
            } else if version.patch != micro {
                return version.patch > micro;
            }
        } else if version.minor != minor || version.patch != micro {
            return false;
        }

        compare_prerelease(version.prerelease.as_deref(), self.prerelease.as_deref())
            != Ordering::Less
    }

    /// Returns true if this requirement has a pre-release on the same
    /// version numbers as the provided version, allowing a pre-release
    /// version to be matched.
    pub fn matches_pre(&self, version: &Version) -> bool {
        self.prerelease.is_some()
            && self.major == Some(version.major)
            && self.minor == Some(version.minor)
            && self.patch == Some(version.patch)
    }
}

impl Display for Requirement {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            self.to_formatted_string(&match self.kind {
                VersionKind::Calendar => FormatOptions::calendar(),
                VersionKind::Semantic => FormatOptions::semantic(),
            })
        )
    }
}

impl Ord for Requirement {
    fn cmp(&self, other: &Self) -> Ordering {
        self.kind
            .cmp(&other.kind)
            .then_with(|| self.scope.cmp(&other.scope))
            .then_with(|| self.major.cmp(&other.major))
            .then_with(|| self.minor.cmp(&other.minor))
            .then_with(|| self.patch.cmp(&other.patch))
            .then_with(|| {
                compare_prerelease(self.prerelease.as_deref(), other.prerelease.as_deref())
            })
            .then_with(|| self.op.cmp(&other.op))
    }
}

impl PartialOrd for Requirement {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl From<Requirement> for String {
    fn from(value: Requirement) -> Self {
        value.to_string()
    }
}

impl TryFrom<String> for Requirement {
    type Error = SpecError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::parse(&value)
    }
}

impl FromStr for Requirement {
    type Err = SpecError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::parse(value)
    }
}

#[cfg(feature = "schematic")]
impl schematic::Schematic for Requirement {
    fn schema_name() -> Option<String> {
        Some("Requirement".into())
    }

    fn build_schema(mut schema: schematic::SchemaBuilder) -> schematic::Schema {
        schema.string_default()
    }
}

/// A single clause within a version range.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Clause {
    /// A list of requirements that must all match, for example `>=1.2 && <2`.
    All(Vec<Requirement>),

    /// A bounded range between two fully qualified versions, inclusive
    /// on both ends, for example `1.2.3 - 2.3.4`. The versions are boxed
    /// to keep the size of this enum down.
    Between(Box<Version>, Box<Version>),

    /// A single requirement.
    Only(Requirement),
}

impl Clause {
    /// Returns the version scope if available. A clause with multiple requirements
    /// only has a scope if all scoped requirements share the same scope, while
    /// requirements without a scope are ignored.
    pub fn get_scope(&self) -> Option<&str> {
        match self {
            Clause::All(reqs) => {
                let mut scope = None;

                for req in reqs {
                    if let Some(req_scope) = req.scope.as_deref() {
                        if scope.is_none() {
                            scope = Some(req_scope);
                        } else if scope != Some(req_scope) {
                            return None;
                        }
                    }
                }

                scope
            }
            Clause::Between(ver1, ver2) => {
                if ver1.scope == ver2.scope {
                    ver1.scope.as_deref()
                } else {
                    None
                }
            }
            Clause::Only(req) => req.scope.as_deref(),
        }
    }

    /// Set the scope on either the current requirement(s) or version(s).
    pub fn set_scope(&mut self, scope: impl AsRef<str>) {
        let scope = Some(scope.as_ref().into());

        match self {
            Clause::All(reqs) => {
                for req in reqs {
                    req.scope = scope.clone();
                }
            }
            Clause::Between(ver1, ver2) => {
                ver1.scope = scope.clone();
                ver2.scope = scope;
            }
            Clause::Only(req) => {
                req.scope = scope;
            }
        }
    }
}

impl Display for Clause {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Clause::All(reqs) => {
                for (i, req) in reqs.iter().enumerate() {
                    if i > 0 {
                        f.write_str(" && ")?;
                    }

                    write!(f, "{req}")?;
                }

                Ok(())
            }
            Clause::Between(ver1, ver2) => write!(f, "{ver1} - {ver2}"),
            Clause::Only(req) => write!(f, "{req}"),
        }
    }
}

/// A version range composed of clauses, in which any clause may match,
/// for example `^1 || 2.3.4 - 3.0.0 || >=4, <5`.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(try_from = "String", into = "String")]
pub struct Range {
    /// The list of clauses. An empty list is a wildcard match.
    pub clauses: Vec<Clause>,
}

impl Range {
    /// Parses the provided value into a range, attempting the semantic
    /// format first, and the calendar format second. A leading `v` or `V`
    /// is ignored.
    pub fn parse<T: AsRef<str>>(value: T) -> Result<Self, SpecError> {
        let value = value.as_ref();

        // The calendar check may false-positive on inner version parts,
        // like the "20.3" in "10.20.30", so fall back to semantic
        if is_calver_like(value) {
            parse_calver_range(value).or_else(|_| parse_semver_range(value))
        } else {
            parse_semver_range(value)
        }
        .map_err(|error| SpecError::FailedVersionRangeParse {
            error: Box::new(error),
        })
    }

    /// Returns the version scope if available. A range with multiple clauses
    /// only has a scope if all scoped clauses share the same scope, while
    /// clauses without a scope are ignored.
    pub fn get_scope(&self) -> Option<&str> {
        let mut scope = None;

        for clause in &self.clauses {
            if let Some(clause_scope) = clause.get_scope() {
                if scope.is_none() {
                    scope = Some(clause_scope);
                } else if scope != Some(clause_scope) {
                    return None;
                }
            }
        }

        scope
    }

    /// Set the scope on all clauses within the range.
    pub fn set_scope(&mut self, scope: impl AsRef<str>) {
        for clause in &mut self.clauses {
            clause.set_scope(scope.as_ref());
        }
    }
}

impl Display for Range {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.clauses.is_empty() {
            return f.write_str("*");
        }

        for (i, clause) in self.clauses.iter().enumerate() {
            if i > 0 {
                f.write_str(" || ")?;
            }

            write!(f, "{clause}")?;
        }

        Ok(())
    }
}

impl From<Range> for String {
    fn from(value: Range) -> Self {
        value.to_string()
    }
}

impl TryFrom<String> for Range {
    type Error = SpecError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::parse(&value)
    }
}

impl FromStr for Range {
    type Err = SpecError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::parse(value)
    }
}

#[cfg(feature = "schematic")]
impl schematic::Schematic for Range {
    fn schema_name() -> Option<String> {
        Some("Range".into())
    }

    fn build_schema(mut schema: schematic::SchemaBuilder) -> schematic::Schema {
        schema.string_default()
    }
}

// A version without a pre-release compares greater than one with a
// pre-release. Identifiers are compared per the semver spec: numerically
// for digit-only identifiers, lexically otherwise, with numeric identifiers
// having lower precedence, and a larger set having a higher precedence
fn compare_prerelease(lhs: Option<&str>, rhs: Option<&str>) -> Ordering {
    let (lhs, rhs) = match (lhs, rhs) {
        (None, None) => return Ordering::Equal,
        (None, Some(_)) => return Ordering::Greater,
        (Some(_), None) => return Ordering::Less,
        (Some(lhs), Some(rhs)) => (lhs, rhs),
    };

    let mut rhs_parts = rhs.split('.');

    for lhs_part in lhs.split('.') {
        let Some(rhs_part) = rhs_parts.next() else {
            return Ordering::Greater;
        };

        let is_digits = |value: &str| value.bytes().all(|byte| byte.is_ascii_digit());

        let ordering = match (is_digits(lhs_part), is_digits(rhs_part)) {
            // Respect numeric ordering, for example 99 < 100
            (true, true) => lhs_part
                .len()
                .cmp(&rhs_part.len())
                .then_with(|| lhs_part.cmp(rhs_part)),
            (true, false) => return Ordering::Less,
            (false, true) => return Ordering::Greater,
            (false, false) => lhs_part.cmp(rhs_part),
        };

        if ordering != Ordering::Equal {
            return ordering;
        }
    }

    if rhs_parts.next().is_none() {
        Ordering::Equal
    } else {
        Ordering::Less
    }
}

// No build metadata compares less than any build metadata. Identifiers
// are compared like pre-releases, except leading zeros on digit-only
// identifiers are also ordered, for example "0" < "00" < "1" < "01" < "2"
fn compare_build(lhs: Option<&str>, rhs: Option<&str>) -> Ordering {
    let (lhs, rhs) = match (lhs, rhs) {
        (None, None) => return Ordering::Equal,
        (None, Some(_)) => return Ordering::Less,
        (Some(_), None) => return Ordering::Greater,
        (Some(lhs), Some(rhs)) => (lhs, rhs),
    };

    let mut rhs_parts = rhs.split('.');

    for lhs_part in lhs.split('.') {
        let Some(rhs_part) = rhs_parts.next() else {
            return Ordering::Greater;
        };

        let is_digits = |value: &str| value.bytes().all(|byte| byte.is_ascii_digit());

        let ordering = match (is_digits(lhs_part), is_digits(rhs_part)) {
            (true, true) => {
                let lhs_trimmed = lhs_part.trim_start_matches('0');
                let rhs_trimmed = rhs_part.trim_start_matches('0');

                lhs_trimmed
                    .len()
                    .cmp(&rhs_trimmed.len())
                    .then_with(|| lhs_trimmed.cmp(rhs_trimmed))
                    .then_with(|| lhs_part.len().cmp(&rhs_part.len()))
            }
            (true, false) => return Ordering::Less,
            (false, true) => return Ordering::Greater,
            (false, false) => lhs_part.cmp(rhs_part),
        };

        if ordering != Ordering::Equal {
            return ordering;
        }
    }

    if rhs_parts.next().is_none() {
        Ordering::Equal
    } else {
        Ordering::Less
    }
}