reliakit-primitives 0.4.3

Reusable type-safe primitives for constrained and reliability-oriented Rust values.
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
use crate::{PrimitiveError, PrimitiveResult};
use alloc::string::{String, ToString};
use core::{fmt, str::FromStr};

/// Semantic version in the form `MAJOR.MINOR.PATCH` with optional pre-release
/// and build metadata identifiers.
///
/// Parses `1.2.3`, `1.2.3-beta.1`, `1.2.3+build.456`, and
/// `1.2.3-alpha.1+build.456`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SemVer {
    major: u64,
    minor: u64,
    patch: u64,
    pre: Option<String>,
    build: Option<String>,
}

impl SemVer {
    /// Creates a `SemVer` with no pre-release or build metadata.
    pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
        Self {
            major,
            minor,
            patch,
            pre: None,
            build: None,
        }
    }

    /// Parses a semver string.
    pub fn parse(s: &str) -> PrimitiveResult<Self> {
        if s.is_empty() {
            return Err(PrimitiveError::Empty);
        }

        let (s, build) = if let Some(idx) = s.find('+') {
            let b = s[idx + 1..].to_string();
            if b.contains('+') {
                return Err(PrimitiveError::Invalid {
                    message: "build metadata must not contain '+'",
                });
            }
            validate_identifier_set(&b, IdentifierKind::Build)?;
            (&s[..idx], Some(b))
        } else {
            (s, None)
        };

        let (s, pre) = if let Some(idx) = s.find('-') {
            let p = s[idx + 1..].to_string();
            validate_identifier_set(&p, IdentifierKind::PreRelease)?;
            (&s[..idx], Some(p))
        } else {
            (s, None)
        };

        let mut parts = s.splitn(4, '.');
        let major = parse_version_component(parts.next().unwrap_or(""))?;
        let minor = parse_version_component(parts.next().unwrap_or(""))?;
        let patch = parse_version_component(parts.next().unwrap_or(""))?;

        if parts.next().is_some() {
            return Err(PrimitiveError::Invalid {
                message: "semver must have exactly three dot-separated components",
            });
        }

        Ok(Self {
            major,
            minor,
            patch,
            pre,
            build,
        })
    }

    /// Returns the major version component.
    pub fn major(&self) -> u64 {
        self.major
    }
    /// Returns the minor version component.
    pub fn minor(&self) -> u64 {
        self.minor
    }
    /// Returns the patch version component.
    pub fn patch(&self) -> u64 {
        self.patch
    }

    /// Returns the pre-release identifier if present.
    pub fn pre(&self) -> Option<&str> {
        self.pre.as_deref()
    }

    /// Returns the build metadata if present.
    pub fn build(&self) -> Option<&str> {
        self.build.as_deref()
    }

    /// Returns `true` if this is a pre-release version.
    pub fn is_pre_release(&self) -> bool {
        self.pre.is_some()
    }

    /// Compares semantic version precedence according to the SemVer rules.
    ///
    /// Build metadata is ignored for SemVer precedence. This differs from
    /// [`Ord`], which uses build metadata as a final tie-breaker so that Rust's
    /// total ordering remains consistent with [`Eq`].
    pub fn cmp_precedence(&self, other: &Self) -> core::cmp::Ordering {
        let core =
            (self.major, self.minor, self.patch).cmp(&(other.major, other.minor, other.patch));
        if core != core::cmp::Ordering::Equal {
            return core;
        }

        // Per SemVer spec §11: pre-release < release when core version is equal.
        match (&self.pre, &other.pre) {
            (None, None) => core::cmp::Ordering::Equal,
            (Some(_), None) => core::cmp::Ordering::Less,
            (None, Some(_)) => core::cmp::Ordering::Greater,
            (Some(a), Some(b)) => compare_pre_release(a, b),
        }
    }
}

fn parse_version_component(s: &str) -> PrimitiveResult<u64> {
    if s.is_empty() {
        return Err(PrimitiveError::Invalid {
            message: "semver component must not be empty",
        });
    }
    if s.len() > 1 && s.starts_with('0') {
        return Err(PrimitiveError::Invalid {
            message: "semver component must not have leading zeros",
        });
    }
    parse_u64(s).ok_or(PrimitiveError::Invalid {
        message: "semver component must be a non-negative integer",
    })
}

fn parse_u64(s: &str) -> Option<u64> {
    if s.is_empty() {
        return None;
    }
    let mut result: u64 = 0;
    for b in s.bytes() {
        if !b.is_ascii_digit() {
            return None;
        }
        let digit = (b - b'0') as u64;
        result = result.checked_mul(10)?.checked_add(digit)?;
    }
    Some(result)
}

#[derive(Copy, Clone)]
enum IdentifierKind {
    PreRelease,
    Build,
}

fn validate_identifier_set(s: &str, kind: IdentifierKind) -> PrimitiveResult<()> {
    if s.is_empty() {
        return Err(PrimitiveError::Invalid {
            message: match kind {
                IdentifierKind::PreRelease => "pre-release identifier must not be empty after '-'",
                IdentifierKind::Build => "build metadata must not be empty after '+'",
            },
        });
    }

    for identifier in s.split('.') {
        if identifier.is_empty() {
            return Err(PrimitiveError::Invalid {
                message: "semver identifiers must not be empty",
            });
        }

        if !identifier
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'-')
        {
            return Err(PrimitiveError::Invalid {
                message: "semver identifiers must contain only ASCII alphanumerics and hyphens",
            });
        }

        if matches!(kind, IdentifierKind::PreRelease)
            && is_numeric_identifier(identifier)
            && identifier.len() > 1
            && identifier.starts_with('0')
        {
            return Err(PrimitiveError::Invalid {
                message: "numeric pre-release identifiers must not have leading zeros",
            });
        }
    }

    Ok(())
}

fn is_numeric_identifier(s: &str) -> bool {
    s.bytes().all(|b| b.is_ascii_digit())
}

fn compare_numeric_identifier(a: &str, b: &str) -> core::cmp::Ordering {
    a.len().cmp(&b.len()).then_with(|| a.cmp(b))
}

fn compare_pre_release(a: &str, b: &str) -> core::cmp::Ordering {
    for (left, right) in a.split('.').zip(b.split('.')) {
        let left_numeric = is_numeric_identifier(left);
        let right_numeric = is_numeric_identifier(right);

        let ordering = match (left_numeric, right_numeric) {
            (true, true) => compare_numeric_identifier(left, right),
            (true, false) => core::cmp::Ordering::Less,
            (false, true) => core::cmp::Ordering::Greater,
            (false, false) => left.cmp(right),
        };

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

    a.split('.').count().cmp(&b.split('.').count())
}

impl fmt::Display for SemVer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)?;
        if let Some(pre) = &self.pre {
            write!(f, "-{pre}")?;
        }
        if let Some(build) = &self.build {
            write!(f, "+{build}")?;
        }
        Ok(())
    }
}

impl FromStr for SemVer {
    type Err = PrimitiveError;

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

impl PartialEq<str> for SemVer {
    fn eq(&self, other: &str) -> bool {
        Self::parse(other).is_ok_and(|other| self == &other)
    }
}

impl PartialEq<&str> for SemVer {
    fn eq(&self, other: &&str) -> bool {
        self.eq(*other)
    }
}

impl PartialEq<String> for SemVer {
    fn eq(&self, other: &String) -> bool {
        self.eq(other.as_str())
    }
}

impl PartialEq<&String> for SemVer {
    fn eq(&self, other: &&String) -> bool {
        self.eq(other.as_str())
    }
}

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

impl Ord for SemVer {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.cmp_precedence(other)
            .then_with(|| self.build.cmp(&other.build))
    }
}

#[cfg(test)]
mod tests {
    use super::SemVer;
    use crate::PrimitiveError;
    use alloc::collections::BTreeSet;
    use alloc::string::ToString;

    #[test]
    fn parses_simple() {
        let v = SemVer::parse("1.2.3").unwrap();
        assert_eq!(v.major(), 1);
        assert_eq!(v.minor(), 2);
        assert_eq!(v.patch(), 3);
        assert!(v.pre().is_none());
        assert!(v.build().is_none());
    }

    #[test]
    fn parses_with_pre_release() {
        let v = SemVer::parse("2.0.0-beta.1").unwrap();
        assert_eq!(v.pre(), Some("beta.1"));
        assert!(v.is_pre_release());
    }

    #[test]
    fn parses_with_build() {
        let v = SemVer::parse("1.0.0+build.456").unwrap();
        assert_eq!(v.build(), Some("build.456"));
    }

    #[test]
    fn parses_pre_and_build() {
        let v = SemVer::parse("1.0.0-alpha.1+build.001").unwrap();
        assert_eq!(v.pre(), Some("alpha.1"));
        assert_eq!(v.build(), Some("build.001"));
    }

    #[test]
    fn rejects_empty() {
        assert_eq!(SemVer::parse("").unwrap_err(), PrimitiveError::Empty);
    }

    #[test]
    fn rejects_missing_components() {
        assert!(SemVer::parse("1.2").is_err());
    }

    #[test]
    fn rejects_too_many_components() {
        assert!(SemVer::parse("1.2.3.4").is_err());
    }

    #[test]
    fn rejects_leading_zeros() {
        assert!(SemVer::parse("1.02.3").is_err());
    }

    #[test]
    fn rejects_non_numeric() {
        assert!(SemVer::parse("a.b.c").is_err());
    }

    #[test]
    fn rejects_empty_pre_release() {
        assert!(SemVer::parse("1.0.0-").is_err());
    }

    #[test]
    fn rejects_empty_build() {
        assert!(SemVer::parse("1.0.0+").is_err());
    }

    #[test]
    fn rejects_build_with_plus() {
        assert!(SemVer::parse("1.0.0+a+b").is_err());
    }

    #[test]
    fn rejects_invalid_pre_release_identifiers() {
        assert!(SemVer::parse("1.0.0-alpha..1").is_err());
        assert!(SemVer::parse("1.0.0-alpha_1").is_err());
        assert!(SemVer::parse("1.0.0-01").is_err());
    }

    #[test]
    fn rejects_invalid_build_identifiers() {
        assert!(SemVer::parse("1.0.0+build..1").is_err());
        assert!(SemVer::parse("1.0.0+build_1").is_err());
    }

    #[test]
    fn display() {
        assert_eq!(SemVer::parse("1.2.3").unwrap().to_string(), "1.2.3");
        assert_eq!(
            SemVer::parse("2.0.0-beta.1").unwrap().to_string(),
            "2.0.0-beta.1"
        );
        assert_eq!(
            SemVer::parse("1.0.0+build").unwrap().to_string(),
            "1.0.0+build"
        );
        assert_eq!(
            SemVer::parse("1.0.0-alpha+build").unwrap().to_string(),
            "1.0.0-alpha+build"
        );
    }

    #[test]
    fn new_constructor() {
        let v = SemVer::new(1, 0, 0);
        assert_eq!(v.to_string(), "1.0.0");
    }

    #[test]
    fn ordering() {
        let v1 = SemVer::parse("1.0.0").unwrap();
        let v2 = SemVer::parse("2.0.0").unwrap();
        let v3 = SemVer::parse("1.1.0").unwrap();
        assert!(v1 < v2);
        assert!(v1 < v3);
        assert!(v3 < v2);
    }

    #[test]
    fn pre_release_sorts_below_release() {
        let release = SemVer::parse("1.0.0").unwrap();
        let pre = SemVer::parse("1.0.0-alpha").unwrap();
        assert!(pre < release);
        assert!(release > pre);
    }

    #[test]
    fn pre_release_compared_lexicographically() {
        let alpha = SemVer::parse("1.0.0-alpha").unwrap();
        let beta = SemVer::parse("1.0.0-beta").unwrap();
        assert!(alpha < beta);
    }

    #[test]
    fn pre_release_numeric_identifiers_compare_numerically() {
        let two = SemVer::parse("1.0.0-alpha.2").unwrap();
        let ten = SemVer::parse("1.0.0-alpha.10").unwrap();
        assert!(two < ten);
    }

    #[test]
    fn pre_release_numeric_identifier_comparison_does_not_overflow() {
        let smaller = SemVer::parse("1.0.0-alpha.999999999999999999999999999999").unwrap();
        let larger = SemVer::parse("1.0.0-alpha.1000000000000000000000000000000").unwrap();
        assert!(smaller < larger);
    }

    #[test]
    fn pre_release_numeric_identifiers_sort_before_non_numeric() {
        let numeric = SemVer::parse("1.0.0-1").unwrap();
        let alpha = SemVer::parse("1.0.0-alpha").unwrap();
        assert!(numeric < alpha);
    }

    #[test]
    fn precedence_ignores_build_metadata() {
        let first = SemVer::parse("1.0.0+build.1").unwrap();
        let second = SemVer::parse("1.0.0+build.2").unwrap();

        assert_eq!(first.cmp_precedence(&second), core::cmp::Ordering::Equal);
    }

    #[test]
    fn ord_is_consistent_with_eq_for_build_metadata() {
        let first = SemVer::parse("1.0.0+build.1").unwrap();
        let second = SemVer::parse("1.0.0+build.2").unwrap();

        assert_ne!(first, second);
        assert_ne!(first.cmp(&second), core::cmp::Ordering::Equal);
    }

    #[test]
    fn btree_set_keeps_distinct_build_metadata() {
        let mut versions = BTreeSet::new();

        versions.insert(SemVer::parse("1.0.0+build.1").unwrap());
        versions.insert(SemVer::parse("1.0.0+build.2").unwrap());

        assert_eq!(versions.len(), 2);
    }

    #[test]
    fn from_str_and_string_comparisons() {
        let version = "1.2.3-beta.1".parse::<SemVer>().unwrap();
        let owned = "1.2.3-beta.1".to_string();
        assert_eq!(version, "1.2.3-beta.1");
        assert_eq!(version, owned);
        assert!("1.2".parse::<SemVer>().is_err());
    }
}