rer-version 1.0.0-rc.3

Version and range types matching rez's alphanumeric versioning.
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
//! Faithful port of rez's `VersionRange` semantics.
//!
//! rez's solver relies on a specific contract that `version_ranges::Ranges`
//! does not express directly — chiefly that `intersection`, the `-` operator
//! and `inverse` return `None` to signal "empty / no result", and that there
//! is a distinguished "any" range. This module wraps `Ranges<RerVersion>` and
//! re-exposes rez's API (`rez/src/rez/version/_version.py`, class
//! `VersionRange`) so the rest of the solver port can mirror the Python code
//! closely.

use crate::requirement::parser::parse_version_range;
use crate::version::RerVersion;
use core::fmt;
use std::ops::Bound;
use std::rc::Rc;
use version_ranges::Ranges;

/// A set of one or more contiguous version ranges, with rez's semantics.
///
/// Construct from a rez range string with [`VersionRange::parse`], from a
/// single version with [`VersionRange::from_version`], or from an existing
/// [`Ranges`] with [`VersionRange::from_ranges`].
///
/// The inner [`Ranges`] is wrapped in `Rc` so that a `VersionRange::clone` is
/// a refcount bump rather than a `SmallVec` allocation. The solver clones
/// requirements (and the ranges inside them) constantly during a solve;
/// callgrind put `SmallVec::extend` + `Drop` at ~4 % of cycles after #66/
/// /#67/#68/#70/#71/#72, almost entirely from these clones.
///
/// `PartialEq`, `Eq` and `Hash` on `Rc<T>` defer to the inner `T`, so the
/// semantics are unchanged from the previous `Ranges<RerVersion>` field.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VersionRange(Rc<Ranges<RerVersion>>);

impl VersionRange {
    /// The "any" range — contains every version. Mirrors rez's empty-string range.
    pub fn any() -> Self {
        VersionRange(Rc::new(Ranges::full()))
    }

    /// The empty range — contains no version. rez represents "empty" as `None`
    /// at the API boundary, but an empty `VersionRange` is still useful
    /// internally (e.g. as a `from_versions` accumulator).
    pub fn empty() -> Self {
        VersionRange(Rc::new(Ranges::empty()))
    }

    /// Parse a rez range string such as `"3"`, `"1+<2"`, `"==1.0"`, `"2|6+"`.
    ///
    /// `|`-separated alternatives are unioned together. The empty string is the
    /// "any" range.
    ///
    /// # Panics
    ///
    /// Panics on a syntactically invalid range string, matching the existing
    /// `parse_version_range` behaviour.
    pub fn parse(s: &str) -> Self {
        let mut range: Option<Ranges<RerVersion>> = None;
        for part in s.split('|') {
            let part_range = parse_version_range(part);
            range = Some(match range {
                None => part_range,
                Some(acc) => acc.union(&part_range),
            });
        }
        VersionRange(Rc::new(range.unwrap_or_else(Ranges::full)))
    }

    /// Wrap a raw [`Ranges`] (already produced by the requirement parser).
    pub fn from_ranges(ranges: Ranges<RerVersion>) -> Self {
        VersionRange(Rc::new(ranges))
    }

    /// Borrow the underlying [`Ranges`].
    pub fn as_ranges(&self) -> &Ranges<RerVersion> {
        &self.0
    }

    /// Consume into the underlying [`Ranges`]. Clones the inner if the `Rc` is
    /// shared.
    pub fn into_ranges(self) -> Ranges<RerVersion> {
        Rc::unwrap_or_clone(self.0)
    }

    /// rez `VersionRange.from_version` with `op=None`: the "superset" range
    /// `[version, version.next())`, e.g. `3` contains `3`, `3.0`, `3.1.4`.
    pub fn from_version(version: &RerVersion) -> Self {
        VersionRange(Rc::new(Ranges::between(version.clone(), version.bump())))
    }

    /// rez `VersionRange.from_versions`: a range containing exactly the given
    /// versions and nothing else (e.g. `==3|==4|==5.1`).
    ///
    /// Built in a single pass via `Ranges`'s `FromIterator` rather than folding
    /// `union` over singletons (which reallocated and was O(n²)) — this is hot,
    /// it backs `_PackageScope._update`.
    pub fn from_versions<I: IntoIterator<Item = RerVersion>>(versions: I) -> Self {
        VersionRange(Rc::new(
            versions
                .into_iter()
                .map(|v| (Bound::Included(v.clone()), Bound::Included(v)))
                .collect(),
        ))
    }

    /// rez `VersionRange.is_any`: true for the range that contains all versions.
    pub fn is_any(&self) -> bool {
        *self.0 == Ranges::full()
    }

    /// True if this range contains no version.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// rez `VersionRange.intersection` (`&`): `None` if the ranges are disjoint.
    pub fn intersection(&self, other: &Self) -> Option<Self> {
        let result = self.0.intersection(&other.0);
        if result.is_empty() {
            None
        } else {
            Some(VersionRange(Rc::new(result)))
        }
    }

    /// rez `VersionRange.union` (`|`): always succeeds.
    pub fn union(&self, other: &Self) -> Self {
        VersionRange(Rc::new(self.0.union(&other.0)))
    }

    /// rez `VersionRange.inverse` (`~`): `None` if and only if this is the
    /// "any" range (whose inverse would be empty).
    pub fn inverse(&self) -> Option<Self> {
        if self.is_any() {
            None
        } else {
            Some(VersionRange(Rc::new(self.0.complement())))
        }
    }

    /// rez `VersionRange.__sub__` (`-`): `self & ~other`. `None` if `other` is
    /// the "any" range, or if the result is empty.
    pub fn difference(&self, other: &Self) -> Option<Self> {
        match other.inverse() {
            None => None,
            Some(inverse) => self.intersection(&inverse),
        }
    }

    /// rez `VersionRange.issuperset`: true if `other` is fully contained here.
    pub fn issuperset(&self, other: &Self) -> bool {
        other.0.subset_of(&self.0)
    }

    /// rez `VersionRange.issubset`: true if `self` is fully contained in `other`.
    pub fn issubset(&self, other: &Self) -> bool {
        other.issuperset(self)
    }

    /// rez `VersionRange.intersects`: true if the ranges share any version.
    pub fn intersects(&self, other: &Self) -> bool {
        !self.0.intersection(&other.0).is_empty()
    }

    /// rez `VersionRange.contains_version` / `version in range`.
    pub fn contains(&self, version: &RerVersion) -> bool {
        self.0.contains(version)
    }

    /// rez `VersionRange.to_versions`: the exact (single-version) bounds present
    /// in this range, or `None` if there are none. Non-exact bounds are ignored,
    /// matching rez.
    pub fn to_versions(&self) -> Option<Vec<RerVersion>> {
        let mut versions = Vec::new();
        for (lower, upper) in self.0.iter() {
            if let (Bound::Included(low), Bound::Included(high)) = (lower, upper) {
                if low == high {
                    versions.push(low.clone());
                }
            }
        }
        if versions.is_empty() {
            None
        } else {
            Some(versions)
        }
    }

    /// rez `VersionRange.span`: the smallest contiguous range that is a superset
    /// of this range (e.g. the span of `2+<4|6+<8` is `2+<8`).
    pub fn span(&self) -> Self {
        match self.0.bounding_range() {
            None => VersionRange::empty(),
            Some((lower, upper)) => VersionRange(Rc::new(Ranges::from_range_bounds((
                clone_bound(lower),
                clone_bound(upper),
            )))),
        }
    }

    /// rez `VersionRange.split`: one [`VersionRange`] per contiguous sub-range
    /// (e.g. `3|5+` splits into `["3", "5+"]`).
    pub fn split(&self) -> Vec<Self> {
        self.0
            .iter()
            .map(|(lower, upper)| {
                VersionRange(Rc::new(Ranges::from_range_bounds((
                    lower.clone(),
                    upper.clone(),
                ))))
            })
            .collect()
    }
}

/// Clone a `Bound<&V>` into an owned `Bound<V>`.
fn clone_bound(bound: Bound<&RerVersion>) -> Bound<RerVersion> {
    match bound {
        Bound::Included(v) => Bound::Included(v.clone()),
        Bound::Excluded(v) => Bound::Excluded(v.clone()),
        Bound::Unbounded => Bound::Unbounded,
    }
}

/// Format one contiguous segment in rez's range syntax, mirroring rez's
/// `_Bound.__str__` (`rez/src/rez/version/_version.py:506`).
fn format_segment(lower: &Bound<RerVersion>, upper: &Bound<RerVersion>) -> String {
    // 1. Infinite upper bound -> just the lower bound string.
    if matches!(upper, Bound::Unbounded) {
        return match lower {
            Bound::Unbounded => String::new(),
            Bound::Included(v) => format!("{v}+"),
            Bound::Excluded(v) => format!(">{v}"),
        };
    }
    let (upper_version, upper_inclusive) = match upper {
        Bound::Included(v) => (v, true),
        Bound::Excluded(v) => (v, false),
        Bound::Unbounded => unreachable!(),
    };
    let lower_version = match lower {
        Bound::Included(v) | Bound::Excluded(v) => Some(v),
        Bound::Unbounded => None,
    };
    let lower_inclusive = !matches!(lower, Bound::Excluded(_));

    // 2. Single exact version -> "==v".
    if lower_version == Some(upper_version) {
        return format!("=={upper_version}");
    }
    // 3. Both bounds inclusive.
    if lower_inclusive && upper_inclusive {
        return match lower_version {
            Some(lv) => format!("{lv}..{upper_version}"),
            None => format!("<={upper_version}"),
        };
    }
    // 4. The "superset" form: [v, v.next()) prints as just "v".
    if lower_inclusive && !upper_inclusive {
        if let Some(lv) = lower_version {
            if &lv.bump() == upper_version {
                return lv.to_string();
            }
        }
    }
    // 5. General case: lower string followed by upper string.
    let lower_str = match lower {
        Bound::Unbounded => String::new(),
        Bound::Included(v) => format!("{v}+"),
        Bound::Excluded(v) => format!(">{v}"),
    };
    let upper_str = if upper_inclusive {
        format!("<={upper_version}")
    } else {
        format!("<{upper_version}")
    };
    format!("{lower_str}{upper_str}")
}

impl fmt::Display for VersionRange {
    /// Renders in rez's compact range syntax (`3`, `3+<5`, `==1.0`, `2|6+`).
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut first = true;
        for (lower, upper) in self.0.iter() {
            if !first {
                write!(f, "|")?;
            }
            first = false;
            write!(f, "{}", format_segment(lower, upper))?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn v(s: &str) -> RerVersion {
        RerVersion::try_from(s).unwrap()
    }

    #[test]
    fn test_any_and_empty() {
        assert!(VersionRange::any().is_any());
        assert!(!VersionRange::any().is_empty());
        assert!(VersionRange::empty().is_empty());
        assert!(!VersionRange::empty().is_any());
        // The parsed empty-string range is the "any" range, per rez.
        assert!(VersionRange::parse("").is_any());
    }

    #[test]
    fn test_intersection_none_when_disjoint() {
        let a = VersionRange::parse("1+<2");
        let b = VersionRange::parse("3+<4");
        assert!(a.intersection(&b).is_none());
        let c = VersionRange::parse("1.5+<3");
        let hit = a.intersection(&c).unwrap();
        assert!(hit.contains(&v("1.6")));
        assert!(!hit.contains(&v("2.0")));
    }

    #[test]
    fn test_inverse_none_only_for_any() {
        assert!(VersionRange::any().inverse().is_none());
        let inv = VersionRange::parse("2+<3").inverse().unwrap();
        assert!(inv.contains(&v("1.0")));
        assert!(inv.contains(&v("4.0")));
        assert!(!inv.contains(&v("2.5")));
    }

    #[test]
    fn test_difference() {
        // a - b = a & ~b
        let a = VersionRange::parse("1+<5");
        let b = VersionRange::parse("2+<3");
        let diff = a.difference(&b).unwrap();
        assert!(diff.contains(&v("1.5")));
        assert!(!diff.contains(&v("2.5")));
        assert!(diff.contains(&v("3.0")));
        // subtracting "any" yields None
        assert!(a.difference(&VersionRange::any()).is_none());
        // subtracting a superset empties the result -> None
        assert!(b.difference(&a).is_none());
    }

    #[test]
    fn test_superset_subset_intersects() {
        let wide = VersionRange::parse("1+<10");
        let narrow = VersionRange::parse("2+<3");
        assert!(wide.issuperset(&narrow));
        assert!(!narrow.issuperset(&wide));
        assert!(narrow.issubset(&wide));
        assert!(wide.intersects(&narrow));
        assert!(!narrow.intersects(&VersionRange::parse("5+<6")));
    }

    #[test]
    fn test_from_version_superset() {
        // rez: the range `3` is the superset of any `3[.X.X...]`.
        let r = VersionRange::from_version(&v("3"));
        assert!(r.contains(&v("3")));
        assert!(r.contains(&v("3.0")));
        assert!(r.contains(&v("3.1.4")));
        assert!(!r.contains(&v("4")));
    }

    #[test]
    fn test_from_versions_and_to_versions() {
        let r = VersionRange::from_versions([v("3"), v("5.1"), v("4")]);
        let mut versions = r.to_versions().unwrap();
        versions.sort();
        assert_eq!(versions, vec![v("3"), v("4"), v("5.1")]);
        // a non-exact range has no exact versions
        assert!(VersionRange::parse("1+<2").to_versions().is_none());
    }

    #[test]
    fn test_display_rez_format() {
        // Compact rez range syntax, not the underlying Ranges debug format.
        assert_eq!(VersionRange::parse("4").to_string(), "4");
        assert_eq!(VersionRange::parse("1.0").to_string(), "1.0");
        assert_eq!(VersionRange::parse("3+<5").to_string(), "3+<5");
        assert_eq!(VersionRange::parse("3+").to_string(), "3+");
        assert_eq!(VersionRange::parse("<3").to_string(), "<3");
        assert_eq!(VersionRange::parse("==1.0.1").to_string(), "==1.0.1");
        assert_eq!(VersionRange::parse("2|5").to_string(), "2|5");
        assert_eq!(VersionRange::any().to_string(), "");
    }

    #[test]
    fn test_span_and_split() {
        let r = VersionRange::parse("2+<4|6+<8");
        let span = r.span();
        assert!(span.contains(&v("5")));
        assert!(span.contains(&v("3")));
        assert!(!span.contains(&v("8")));
        let parts = r.split();
        assert_eq!(parts.len(), 2);
        assert!(parts[0].contains(&v("3")));
        assert!(!parts[0].contains(&v("6")));
        assert!(parts[1].contains(&v("6")));
    }
}