check-updates-core 0.3.0

Shared core library for check-updates tools
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
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt;
use std::str::FromStr;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum VersionError {
    #[error("Invalid version string: {0}")]
    InvalidVersion(String),
    #[error("Invalid version specifier: {0}")]
    InvalidSpecifier(String),
}

/// A parsed semantic version
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Version {
    pub major: u64,
    pub minor: u64,
    pub patch: u64,
    pub pre_release: Option<String>,
    /// Local version segment (Python) or build metadata (Cargo)
    pub local: Option<String>,
    /// Original string representation
    pub original: String,
}

impl PartialEq for Version {
    fn eq(&self, other: &Self) -> bool {
        self.major == other.major
            && self.minor == other.minor
            && self.patch == other.patch
            && self.pre_release == other.pre_release
    }
}

impl Eq for Version {}

impl Version {
    pub fn new(major: u64, minor: u64, patch: u64) -> Self {
        Self {
            major,
            minor,
            patch,
            pre_release: None,
            local: None,
            original: format!("{major}.{minor}.{patch}"),
        }
    }

    /// Check if this is a pre-release version
    pub fn is_prerelease(&self) -> bool {
        self.pre_release.is_some()
    }

    /// Check if this version is in the same major series as another
    pub fn same_major(&self, other: &Version) -> bool {
        self.major == other.major
    }

    /// Check if this version is in the same minor series as another
    pub fn same_minor(&self, other: &Version) -> bool {
        self.major == other.major && self.minor == other.minor
    }
}

impl FromStr for Version {
    type Err = VersionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.trim();

        // Handle local version separator (+)
        let (version_part, local) = if let Some(idx) = s.find('+') {
            (&s[..idx], Some(s[idx + 1..].to_string()))
        } else {
            (s, None)
        };

        // Handle pre-release separators (-, a, b, rc, alpha, beta, dev, post)
        let (base_part, pre_release) = parse_prerelease(version_part);

        // Parse the base version (major.minor.patch)
        let parts: Vec<&str> = base_part.split('.').collect();

        let major = parts
            .first()
            .and_then(|s| s.parse().ok())
            .ok_or_else(|| VersionError::InvalidVersion(s.to_string()))?;

        let minor = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);

        let patch = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);

        Ok(Version {
            major,
            minor,
            patch,
            pre_release,
            local,
            original: s.to_string(),
        })
    }
}

fn parse_prerelease(s: &str) -> (&str, Option<String>) {
    // Common pre-release patterns
    let patterns = [
        "dev", "post", "alpha", "beta", "rc", "a", "b", "c", "-",
    ];

    for pattern in patterns {
        if let Some(idx) = s.to_lowercase().find(pattern)
            && idx > 0 {
                return (&s[..idx], Some(s[idx..].to_string()));
            }
    }

    (s, None)
}

impl Ord for Version {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.major.cmp(&other.major) {
            Ordering::Equal => {}
            ord => return ord,
        }
        match self.minor.cmp(&other.minor) {
            Ordering::Equal => {}
            ord => return ord,
        }
        match self.patch.cmp(&other.patch) {
            Ordering::Equal => {}
            ord => return ord,
        }

        // Pre-release versions are less than release versions
        match (&self.pre_release, &other.pre_release) {
            (None, Some(_)) => Ordering::Greater,
            (Some(_), None) => Ordering::Less,
            (Some(a), Some(b)) => a.cmp(b),
            (None, None) => Ordering::Equal,
        }
    }
}

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

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.original)
    }
}

/// Version specification (constraint)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionSpec {
    /// ==1.2.3
    Pinned(Version),
    /// >=1.2.3
    Minimum(Version),
    /// <=1.2.3
    Maximum(Version),
    /// >1.2.3
    GreaterThan(Version),
    /// <1.2.3
    LessThan(Version),
    /// >=1.2.3,<2.0.0
    Range { min: Version, max: Version },
    /// ^1.2.3 (caret - same major)
    Caret(Version),
    /// ~1.2.3 (tilde - same minor)
    Tilde(Version),
    /// ~=1.2.3 (compatible release - Python)
    Compatible(Version),
    /// ==1.2.*
    Wildcard { prefix: String, pattern: String },
    /// !=1.2.3
    NotEqual(Version),
    /// Complex constraint we store as raw string
    Complex(String),
    /// Any version (no constraint or *)
    Any,
}

impl VersionSpec {
    /// Parse a version specifier string
    pub fn parse(s: &str) -> Result<Self, VersionError> {
        let s = s.trim();

        if s.is_empty() || s == "*" {
            return Ok(VersionSpec::Any);
        }

        // Handle caret notation (poetry/pdm style)
        if let Some(version_str) = s.strip_prefix('^') {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::Caret(version));
        }

        // Handle tilde notation
        if let Some(version_str) = s.strip_prefix("~=") {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::Compatible(version));
        }
        if let Some(version_str) = s.strip_prefix('~') {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::Tilde(version));
        }

        // Handle wildcard
        if s.contains('*') {
            if let Some(prefix) = s.strip_prefix("==") {
                return Ok(VersionSpec::Wildcard {
                    prefix: prefix.replace(".*", "").replace("*", ""),
                    pattern: s.to_string(),
                });
            }
            return Ok(VersionSpec::Wildcard {
                prefix: s.replace(".*", "").replace("*", ""),
                pattern: s.to_string(),
            });
        }

        // Handle range (>=X,<Y)
        if s.contains(',') {
            let parts: Vec<&str> = s.split(',').collect();
            if parts.len() == 2 {
                let min_part = parts[0].trim();
                let max_part = parts[1].trim();

                if let (Some(min_str), Some(max_str)) = (
                    min_part.strip_prefix(">="),
                    max_part.strip_prefix('<'),
                ) {
                    let min = Version::from_str(min_str)?;
                    let max = Version::from_str(max_str)?;
                    return Ok(VersionSpec::Range { min, max });
                }
            }
            // Complex constraint
            return Ok(VersionSpec::Complex(s.to_string()));
        }

        // Handle simple operators
        if let Some(version_str) = s.strip_prefix("==") {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::Pinned(version));
        }
        if let Some(version_str) = s.strip_prefix(">=") {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::Minimum(version));
        }
        if let Some(version_str) = s.strip_prefix("<=") {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::Maximum(version));
        }
        if let Some(version_str) = s.strip_prefix("!=") {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::NotEqual(version));
        }
        if let Some(version_str) = s.strip_prefix('>') {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::GreaterThan(version));
        }
        if let Some(version_str) = s.strip_prefix('<') {
            let version = Version::from_str(version_str)?;
            return Ok(VersionSpec::LessThan(version));
        }

        // No operator - treat as pinned or complex
        if let Ok(version) = Version::from_str(s) {
            return Ok(VersionSpec::Pinned(version));
        }

        Ok(VersionSpec::Complex(s.to_string()))
    }

    /// Check if a version satisfies this constraint
    pub fn satisfies(&self, version: &Version) -> bool {
        match self {
            VersionSpec::Any => true,
            VersionSpec::Pinned(v) => version == v,
            VersionSpec::Minimum(v) => version >= v,
            VersionSpec::Maximum(v) => version <= v,
            VersionSpec::GreaterThan(v) => version > v,
            VersionSpec::LessThan(v) => version < v,
            VersionSpec::Range { min, max } => version >= min && version < max,
            VersionSpec::Caret(v) => {
                // Caret: ^1.2.3 means >=1.2.3 <2.0.0
                // But for 0.x: ^0.1.2 means >=0.1.2 <0.2.0
                // And for 0.0.x: ^0.0.3 means =0.0.3
                if version < v {
                    return false;
                }
                if v.major == 0 {
                    if v.minor == 0 {
                        // ^0.0.z means =0.0.z
                        version.major == 0 && version.minor == 0 && version.patch == v.patch
                    } else {
                        // ^0.y.z means >=0.y.z <0.(y+1).0
                        version.major == 0 && version.minor == v.minor
                    }
                } else {
                    // ^x.y.z means >=x.y.z <(x+1).0.0
                    version.major == v.major
                }
            }
            VersionSpec::Tilde(v) => {
                version >= v && version.major == v.major && version.minor == v.minor
            }
            VersionSpec::Compatible(v) => {
                // PEP 440: ~=X.Y means >=X.Y, <(X+1).0.0 (lock major only)
                //          ~=X.Y.Z means >=X.Y.Z, <X.(Y+1).0 (lock major+minor)
                let dot_count = v.original.chars().filter(|c| *c == '.').count();
                if dot_count < 2 {
                    // ~=X.Y form: only lock on major
                    version >= v && version.major == v.major
                } else {
                    // ~=X.Y.Z form: lock on major+minor
                    version >= v && version.major == v.major && version.minor == v.minor
                }
            }
            VersionSpec::Wildcard { prefix, .. } => {
                // Must match prefix followed by a dot (or end), so 1.2.* doesn't match 1.20.x
                version.original.starts_with(&format!("{prefix}."))
                    || version.original == *prefix
            }
            VersionSpec::NotEqual(v) => version != v,
            VersionSpec::Complex(_) => false, // Can't evaluate complex constraints; don't claim in-range
        }
    }

    /// Get the base version from the spec (for comparison)
    pub fn base_version(&self) -> Option<&Version> {
        match self {
            VersionSpec::Pinned(v)
            | VersionSpec::Minimum(v)
            | VersionSpec::Maximum(v)
            | VersionSpec::GreaterThan(v)
            | VersionSpec::LessThan(v)
            | VersionSpec::Caret(v)
            | VersionSpec::Tilde(v)
            | VersionSpec::Compatible(v)
            | VersionSpec::NotEqual(v) => Some(v),
            VersionSpec::Range { min, .. } => Some(min),
            VersionSpec::Wildcard { .. } | VersionSpec::Complex(_) | VersionSpec::Any => None,
        }
    }

    /// Get the maximum allowed major version (for "in range" calculation)
    pub fn max_major(&self) -> Option<u64> {
        match self {
            VersionSpec::Range { max, .. } => Some(max.major),
            VersionSpec::Caret(v) => Some(v.major),
            VersionSpec::LessThan(v) | VersionSpec::Maximum(v) => Some(v.major),
            // For unbounded specs, we assume same major (semver)
            VersionSpec::Minimum(v)
            | VersionSpec::GreaterThan(v)
            | VersionSpec::Pinned(v)
            | VersionSpec::Compatible(v)
            | VersionSpec::Tilde(v) => Some(v.major),
            VersionSpec::NotEqual(v) => Some(v.major),
            VersionSpec::Wildcard { prefix, .. } => {
                prefix.split('.').next().and_then(|s| s.parse().ok())
            }
            VersionSpec::Complex(_) | VersionSpec::Any => None,
        }
    }

    /// Get the version string without operators (for Cargo.toml format)
    /// Returns just "1.0.0" instead of "==1.0.0"
    pub fn version_string(&self) -> Option<String> {
        match self {
            VersionSpec::Pinned(v)
            | VersionSpec::Minimum(v)
            | VersionSpec::Maximum(v)
            | VersionSpec::GreaterThan(v)
            | VersionSpec::LessThan(v)
            | VersionSpec::Caret(v)
            | VersionSpec::Tilde(v)
            | VersionSpec::Compatible(v)
            | VersionSpec::NotEqual(v) => Some(v.to_string()),
            VersionSpec::Range { min, .. } => Some(min.to_string()),
            VersionSpec::Wildcard { prefix, .. } => Some(format!("{prefix}.*")),
            VersionSpec::Complex(s) => Some(s.clone()),
            VersionSpec::Any => None,
        }
    }

    /// Serialize to Cargo.toml requirement syntax.
    /// Cargo conventions: bare version = caret, `=` for exact pin, `~` for tilde, etc.
    pub fn to_cargo_string(&self) -> Option<String> {
        match self {
            VersionSpec::Caret(v) => Some(v.to_string()), // bare = caret in Cargo
            VersionSpec::Tilde(v) => Some(format!("~{v}")),
            VersionSpec::Pinned(v) => Some(format!("={v}")), // Cargo uses single =
            VersionSpec::Minimum(v) => Some(format!(">={v}")),
            VersionSpec::Maximum(v) => Some(format!("<={v}")),
            VersionSpec::GreaterThan(v) => Some(format!(">{v}")),
            VersionSpec::LessThan(v) => Some(format!("<{v}")),
            VersionSpec::Range { min, max } => Some(format!(">={min}, <{max}")),
            VersionSpec::Wildcard { prefix, .. } => Some(format!("{prefix}.*")),
            VersionSpec::NotEqual(v) => Some(format!("!={v}")),
            VersionSpec::Compatible(v) => Some(v.to_string()), // not a Cargo concept, treat as bare
            VersionSpec::Complex(s) => Some(s.clone()),
            VersionSpec::Any => Some("*".to_string()),
        }
    }

    /// Returns true if this spec can be safely rewritten by an updater
    pub fn is_rewritable(&self) -> bool {
        !matches!(self, VersionSpec::Complex(_) | VersionSpec::Any)
    }

    /// Create a new version spec with updated version but same constraint type
    pub fn with_version(&self, new_version: &Version) -> VersionSpec {
        match self {
            VersionSpec::Pinned(_) => VersionSpec::Pinned(new_version.clone()),
            VersionSpec::Minimum(_) => VersionSpec::Minimum(new_version.clone()),
            VersionSpec::Maximum(_) => VersionSpec::Maximum(new_version.clone()),
            VersionSpec::GreaterThan(_) => VersionSpec::GreaterThan(new_version.clone()),
            VersionSpec::LessThan(_) => VersionSpec::LessThan(new_version.clone()),
            VersionSpec::Range { max, .. } => {
                // If new min would exceed max, update max to next major
                if new_version >= max {
                    VersionSpec::Range {
                        min: new_version.clone(),
                        max: Version::new(new_version.major + 1, 0, 0),
                    }
                } else {
                    VersionSpec::Range {
                        min: new_version.clone(),
                        max: max.clone(),
                    }
                }
            }
            VersionSpec::Caret(_) => VersionSpec::Caret(new_version.clone()),
            VersionSpec::Tilde(_) => VersionSpec::Tilde(new_version.clone()),
            VersionSpec::Compatible(_) => VersionSpec::Compatible(new_version.clone()),
            VersionSpec::Wildcard { prefix, pattern } => {
                // Preserve the original wildcard precision:
                // "1.*" (1 segment) → "2.*", "1.2.*" (2 segments) → "1.3.*"
                let segments = prefix.split('.').count();
                let new_prefix = match segments {
                    0 | 1 => format!("{}", new_version.major),
                    _ => format!("{}.{}", new_version.major, new_version.minor),
                };
                VersionSpec::Wildcard {
                    prefix: new_prefix,
                    pattern: pattern.clone(),
                }
            }
            VersionSpec::NotEqual(_) => VersionSpec::NotEqual(new_version.clone()),
            VersionSpec::Complex(s) => VersionSpec::Complex(s.clone()),
            VersionSpec::Any => VersionSpec::Any,
        }
    }
}

impl fmt::Display for VersionSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VersionSpec::Any => write!(f, "*"),
            VersionSpec::Pinned(v) => write!(f, "=={v}"),
            VersionSpec::Minimum(v) => write!(f, ">={v}"),
            VersionSpec::Maximum(v) => write!(f, "<={v}"),
            VersionSpec::GreaterThan(v) => write!(f, ">{v}"),
            VersionSpec::LessThan(v) => write!(f, "<{v}"),
            VersionSpec::Range { min, max } => write!(f, ">={min},<{max}"),
            VersionSpec::Caret(v) => write!(f, "^{v}"),
            VersionSpec::Tilde(v) => write!(f, "~{v}"),
            VersionSpec::Compatible(v) => write!(f, "~={v}"),
            VersionSpec::Wildcard { prefix, .. } => write!(f, "=={prefix}.*"),
            VersionSpec::NotEqual(v) => write!(f, "!={v}"),
            VersionSpec::Complex(s) => write!(f, "{s}"),
        }
    }
}

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

    #[test]
    fn test_parse_version() {
        let v = Version::from_str("1.2.3").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);

        let v = Version::from_str("2.0").unwrap();
        assert_eq!(v.major, 2);
        assert_eq!(v.minor, 0);
        assert_eq!(v.patch, 0);
    }

    #[test]
    fn test_version_comparison() {
        let v1 = Version::from_str("1.2.3").unwrap();
        let v2 = Version::from_str("1.2.4").unwrap();
        let v3 = Version::from_str("2.0.0").unwrap();

        assert!(v1 < v2);
        assert!(v2 < v3);
        assert!(v1 < v3);
    }

    #[test]
    fn test_parse_version_spec() {
        assert!(matches!(
            VersionSpec::parse("==1.2.3").unwrap(),
            VersionSpec::Pinned(_)
        ));
        assert!(matches!(
            VersionSpec::parse(">=1.2.3").unwrap(),
            VersionSpec::Minimum(_)
        ));
        assert!(matches!(
            VersionSpec::parse("^1.2.3").unwrap(),
            VersionSpec::Caret(_)
        ));
        assert!(matches!(
            VersionSpec::parse(">=1.0.0,<2.0.0").unwrap(),
            VersionSpec::Range { .. }
        ));
    }

    #[test]
    fn test_satisfies() {
        let spec = VersionSpec::parse(">=1.0.0,<2.0.0").unwrap();
        assert!(spec.satisfies(&Version::from_str("1.5.0").unwrap()));
        assert!(!spec.satisfies(&Version::from_str("2.0.0").unwrap()));
        assert!(!spec.satisfies(&Version::from_str("0.9.0").unwrap()));
    }
}