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
mod meta {
    pub const TAG_MASK: u8 = 0b11000000;
    pub const VALUE_MASK: u8 = 0b00111111;
    pub const MAX_VALUE: u8 = VALUE_MASK;

    pub const RC: u8 = 0b01000000;
    pub const ALPHA: u8 = 0b10000000;
    pub const DEV_ALPHA: u8 = 0b11000000;
}

/// The version of a Rerun crate.
///
/// Sub-set of semver supporting `major.minor.patch-{alpha,rc}.N+dev`.
///
/// The string value of build metadata is not preserved.
///
/// Examples: `1.2.3`, `1.2.3-alpha.4`, `1.2.3-alpha.1+dev`.
///
/// `-alpha.N+dev` versions are used for local or CI builds.
/// `-alpha.N` versions are used for weekly releases.
/// `-rc.N` versions are used for release candidates as we're preparing for a full release.
///
/// The version numbers (`N`) aren't allowed to be very large (current max: 63).
/// This limited subset is chosen so that we can encode the version in 32 bits
/// in our `.rrd` files and on the wire.
///
/// Here is the current binary format:
/// ```text,ignore
/// major    minor    patch    meta
/// 00000000 00000000 00000000 00NNNNNN
///                            ▲▲▲    ▲
///                            ││└─┬──┘
///                            ││  └─ N
///                            │└─ rc/dev
///                            └─ alpha
/// ```
///
/// The valid bit patterns for `meta` are:
/// - `10NNNNNN` -> `-alpha.N`
/// - `11NNNNNN` -> `-alpha.N+dev`
/// - `01NNNNNN` -> `-rc.N`
/// - `00000000` -> none of the above
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct CrateVersion {
    pub major: u8,
    pub minor: u8,
    pub patch: u8,
    pub meta: Option<Meta>,
}

impl CrateVersion {
    pub const LOCAL: Self = Self::parse(env!("CARGO_PKG_VERSION"));
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Meta {
    Rc(u8),
    Alpha(u8),
    DevAlpha(u8),
}

impl Meta {
    pub const fn to_byte(self) -> u8 {
        match self {
            Meta::Rc(value) => value | meta::RC,
            Meta::Alpha(value) => value | meta::ALPHA,
            Meta::DevAlpha(value) => value | meta::DEV_ALPHA,
        }
    }

    pub const fn from_byte(v: u8) -> Option<Self> {
        let tag = v & meta::TAG_MASK;
        let value = v & meta::VALUE_MASK;
        match tag {
            meta::RC => Some(Self::Rc(value)),
            meta::ALPHA => Some(Self::Alpha(value)),
            meta::DEV_ALPHA => Some(Self::DevAlpha(value)),
            _ => None,
        }
    }
}

/// Helper function to slice slices in a `const` context.
/// Instead of using this directly, use the `slice` macro.
///
/// This is equivalent to `v[start..end]`.
const fn const_u8_slice_util(v: &[u8], start: Option<usize>, end: Option<usize>) -> &[u8] {
    let (start, end) = match (start, end) {
        (Some(start), Some(end)) => (start, end),
        (Some(start), None) => (start, v.len()),
        (None, Some(end)) => (0, end),
        (None, None) => return v,
    };

    assert!(start <= v.len());
    assert!(end <= v.len());
    assert!(start <= end);

    {
        // The only reason we do this is to allow slicing in `const` functions.
        #![allow(unsafe_code)]

        let ptr = v.as_ptr();
        // SAFETY:
        // - the read is valid, because the following is true:
        //   - `ptr` is valid for reads of `len` elements, because it is taken from a valid slice.
        //     this means it is already guaranteed to be non-null and properly aligned, and the
        //     entire length of the slice is contained within a single allocated object.
        //   - `start <= len && end <= len && start <= end`
        // - the returned slice appears to be a shared borrow from `v`,
        //   so the borrow checker will ensure users will not mutate `v`
        //   until this slice is dropped.
        unsafe { std::slice::from_raw_parts(ptr.add(start), end - start) }
    }
}

/// Slice `s` by some `start` and `end` bounds.
///
/// This is equivalent to doing `s[start..end]`,
/// but works in a `const` context.
macro_rules! slice {
    ($s:expr, .., $end:expr) => {
        const_u8_slice_util($s, None, Some($end))
    };
    ($s:expr, $start:expr, ..) => {
        const_u8_slice_util($s, Some($start), None)
    };
    ($s:expr, $start:expr, $end:expr) => {
        const_u8_slice_util($s, Some($start), Some($end))
    };
}

const fn equals(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }

    let len = a.len();
    let mut i = 0;
    while i < len {
        if a[i] != b[i] {
            return false;
        }
        i += 1;
    }
    true
}

const fn split_at(s: &[u8], i: usize) -> (&[u8], &[u8]) {
    (slice!(s, .., i), slice!(s, i, ..))
}

impl CrateVersion {
    pub const fn new(major: u8, minor: u8, patch: u8) -> Self {
        Self {
            major,
            minor,
            patch,
            meta: None,
        }
    }

    /// True is this version has no metadata at all (rc, dev, alpha, etc).
    ///
    /// I.e. it's an actual, final release.
    pub fn is_release(&self) -> bool {
        self.meta.is_none()
    }

    /// Whether or not this build has a `+dev` suffix.
    ///
    /// This is used to identify builds which are not explicit releases,
    /// such as local builds and CI builds for every commit.
    pub fn is_dev(&self) -> bool {
        matches!(self.meta, Some(Meta::DevAlpha(..)))
    }

    /// Whether or not this is an alpha version (`-alpha.N` or `-alpha.N+dev`).
    pub fn is_alpha(&self) -> bool {
        matches!(self.meta, Some(Meta::Alpha(..) | Meta::DevAlpha(..)))
    }

    /// Whether or not this is a release candidate (`-rc.N`).
    pub fn is_rc(&self) -> bool {
        matches!(self.meta, Some(Meta::Rc(..)))
    }

    /// From a compact 32-bit representation crated with [`Self::to_bytes`].
    pub fn from_bytes([major, minor, patch, meta]: [u8; 4]) -> Self {
        Self {
            major,
            minor,
            patch,
            meta: Meta::from_byte(meta),
        }
    }

    /// A compact 32-bit representation. See also [`Self::from_bytes`].
    pub fn to_bytes(self) -> [u8; 4] {
        [
            self.major,
            self.minor,
            self.patch,
            self.meta.map(Meta::to_byte).unwrap_or_default(),
        ]
    }

    #[allow(clippy::unnested_or_patterns)]
    pub fn is_compatible_with(self, other: CrateVersion) -> bool {
        match (self.meta, other.meta) {
            // release candidates are always compatible with each other
            // and their finalized version:
            //   1.0.0-rc.1 == 1.0.0-rc.2 == 1.0.0
            (Some(Meta::Rc(..)), Some(Meta::Rc(..)))
            | (Some(Meta::Rc(..)), None)
            | (None, Some(Meta::Rc(..))) => {}
            (this, other) => {
                if this != other {
                    // Alphas can contain breaking changes
                    return false;
                }
            }
        }

        if self.major == 0 {
            // before 1.0.0 we break compatibility using the minor:
            (self.major, self.minor) == (other.major, other.minor)
        } else {
            // major version is the only breaking change:
            self.major == other.major
        }
    }

    /// Parse a version string according to our subset of semver.
    ///
    /// See [`CrateVersion`] for more information.
    pub const fn parse(version_string: &str) -> Self {
        match Self::try_parse(version_string) {
            Ok(version) => version,
            Err(_err) => {
                // We previously used const_panic to concatenate the actual version but it crashed
                // the 1.72.0 linker on mac :/
                panic!("invalid version string")
            }
        }
    }

    /// Parse a version string according to our subset of semver.
    ///
    /// See [`CrateVersion`] for more information.
    pub const fn try_parse(version_string: &str) -> Result<Self, &'static str> {
        // Note that this is a const function, which means we are extremely limited in what we can do!

        const fn maybe(s: &[u8], c: u8) -> (bool, &[u8]) {
            if !s.is_empty() && s[0] == c {
                (true, slice!(s, 1, ..))
            } else {
                (false, s)
            }
        }

        const fn maybe_token<'a>(s: &'a [u8], token: &[u8]) -> (bool, &'a [u8]) {
            if s.len() < token.len() {
                return (false, s);
            }

            let (left, right) = split_at(s, token.len());
            if equals(left, token) {
                (true, right)
            } else {
                (false, s)
            }
        }

        macro_rules! eat {
            ($s:ident, $c:expr, $msg:literal) => {{
                if $s.is_empty() || $s[0] != $c {
                    return Err($msg);
                }
                slice!($s, 1, ..)
            }};
        }

        macro_rules! eat_token {
            ($s:ident, $token:expr, $msg:literal) => {{
                let token = $token;
                if token.len() > $s.len() {
                    return Err($msg);
                }
                let (left, right) = split_at($s, token.len());
                if !equals(left, token) {
                    return Err($msg);
                }
                right
            }};
        }

        macro_rules! eat_u8 {
            ($s:ident, $msg:literal) => {{
                if $s.is_empty() {
                    return Err($msg);
                }

                if $s.len() > 1 && $s[1].is_ascii_digit() {
                    if $s[0] == b'0' {
                        return Err("multi-digit number cannot start with zero");
                    }
                }

                let mut num = 0u64;
                let mut i = 0;
                while i < $s.len() && $s[i].is_ascii_digit() {
                    let digit = ($s[i] - b'0') as u64;
                    num = num * 10 + digit;
                    i += 1;
                }

                if num > u8::MAX as u64 {
                    return Err("digit cannot be larger than 255");
                }
                let num = num as u8;
                let remainder = slice!($s, i, ..);

                (num, remainder)
            }};
        }

        let mut s = version_string.as_bytes();
        let (major, minor, patch);
        let mut meta = None;

        (major, s) = eat_u8!(s, "expected major version number");
        s = eat!(s, b'.', "expected `.` after major version number");
        (minor, s) = eat_u8!(s, "expected minor version number");
        s = eat!(s, b'.', "expected `.` after minor version number");
        (patch, s) = eat_u8!(s, "expected patch version number");

        if let (true, remainder) = maybe(s, b'-') {
            s = remainder;

            let build;
            if let (true, remainder) = maybe_token(s, b"alpha") {
                s = eat!(remainder, b'.', "expected `.` after `-alpha`");
                (build, s) = eat_u8!(s, "expected digit after `-alpha.`");
                if build > meta::MAX_VALUE {
                    return Err("`-alpha` build number is larger than 63");
                }
                meta = Some(Meta::Alpha(build));
            } else if let (true, remainder) = maybe_token(s, b"rc") {
                s = eat!(remainder, b'.', "expected `.` after `-rc`");
                (build, s) = eat_u8!(s, "expected digit after `-rc.`");
                if build > meta::MAX_VALUE {
                    return Err("`-rc` build number is larger than 63");
                }
                meta = Some(Meta::Rc(build));
            } else {
                return Err("expected `alpha` or `rc` after `-`");
            }
        }

        if let (true, remainder) = maybe(s, b'+') {
            s = remainder;
            match meta {
                Some(Meta::Alpha(build)) => {
                    s = eat_token!(s, b"dev", "expected `dev` after `+`");
                    meta = Some(Meta::DevAlpha(build));
                }
                Some(..) => return Err("unexpected `-rc` with `+dev`"),
                None => return Err("unexpected `+dev` without `-alpha`"),
            }
        };

        if !s.is_empty() {
            return Err("expected end of string");
        }

        Ok(Self {
            major,
            minor,
            patch,
            meta,
        })
    }
}

impl std::fmt::Display for Meta {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Meta::Rc(build) => write!(f, "-rc.{build}"),
            Meta::Alpha(build) => write!(f, "-alpha.{build}"),
            Meta::DevAlpha(build) => write!(f, "-alpha.{build}+dev"),
        }
    }
}

impl std::fmt::Display for CrateVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self {
            major,
            minor,
            patch,
            meta,
        } = *self;

        write!(f, "{major}.{minor}.{patch}")?;
        if let Some(meta) = meta {
            write!(f, "{meta}")?;
        }
        Ok(())
    }
}

#[test]
fn test_parse_version() {
    macro_rules! assert_parse_ok {
        ($input:literal, $expected:expr) => {
            assert_eq!(CrateVersion::try_parse($input), Ok($expected))
        };
    }

    assert_parse_ok!("0.2.0", CrateVersion::new(0, 2, 0));
    assert_parse_ok!("0.2.0", CrateVersion::new(0, 2, 0));
    assert_parse_ok!("1.2.3", CrateVersion::new(1, 2, 3));
    assert_parse_ok!("12.23.24", CrateVersion::new(12, 23, 24));
    assert_parse_ok!(
        "12.23.24-rc.63",
        CrateVersion {
            major: 12,
            minor: 23,
            patch: 24,
            meta: Some(Meta::Rc(63)),
        }
    );
    assert_parse_ok!(
        "12.23.24-alpha.63",
        CrateVersion {
            major: 12,
            minor: 23,
            patch: 24,
            meta: Some(Meta::Alpha(63)),
        }
    );
    assert_parse_ok!(
        "12.23.24-alpha.63+dev",
        CrateVersion {
            major: 12,
            minor: 23,
            patch: 24,
            meta: Some(Meta::DevAlpha(63)),
        }
    );
}

#[test]
fn test_format_parse_roundtrip() {
    for version in [
        "0.2.0",
        "1.2.3",
        "12.23.24",
        "12.23.24-rc.63",
        "12.23.24-alpha.63",
        "12.23.24-alpha.63+dev",
    ] {
        assert_eq!(CrateVersion::parse(version).to_string(), version);
    }
}

#[test]
fn test_format_parse_roundtrip_bytes() {
    for version in [
        "0.2.0",
        "1.2.3",
        "12.23.24",
        "12.23.24-rc.63",
        "12.23.24-alpha.63",
        "12.23.24-alpha.63+dev",
    ] {
        let version = CrateVersion::parse(version);
        let bytes = version.to_bytes();
        assert_eq!(CrateVersion::from_bytes(bytes), version);
    }
}

#[test]
fn test_compatibility() {
    fn are_compatible(a: &str, b: &str) -> bool {
        CrateVersion::parse(a).is_compatible_with(CrateVersion::parse(b))
    }

    assert!(are_compatible("0.2.0", "0.2.0"));
    assert!(are_compatible("0.2.0", "0.2.1"));
    assert!(are_compatible("1.2.0", "1.3.0"));
    assert!(
        !are_compatible("0.2.0", "1.2.0"),
        "Different major versions are incompatible"
    );
    assert!(
        !are_compatible("0.2.0", "0.3.0"),
        "Different minor versions are incompatible"
    );
    assert!(are_compatible("0.2.0-alpha.0", "0.2.0-alpha.0"));
    assert!(are_compatible("0.2.0-rc.0", "0.2.0-rc.0"));
    assert!(
        !are_compatible("0.2.0-rc.0", "0.2.0-alpha.0"),
        "Rc and Alpha are incompatible"
    );
    assert!(
        !are_compatible("0.2.0-rc.0", "0.2.0-alpha.0+dev"),
        "Rc and Dev are incompatible"
    );
    assert!(
        !are_compatible("0.2.0-alpha.0", "0.2.0-alpha.0+dev"),
        "Alpha and Dev are incompatible"
    );
    assert!(
        !are_compatible("0.2.0-alpha.0", "0.2.0-alpha.1"),
        "Different alpha builds are always incompatible"
    );
    assert!(
        are_compatible("0.2.0-rc.0", "0.2.0-rc.1"),
        "Different rc builds are always compatible"
    );
    assert!(
        are_compatible("0.2.0-rc.0", "0.2.0"),
        "rc build is compatible with the finalized version"
    );
    assert!(
        are_compatible("0.2.0", "0.2.1-rc.0"),
        "rc build is compatible by patch version"
    );
}

#[test]
fn test_bad_parse() {
    macro_rules! assert_parse_err {
        ($input:literal, $expected:literal) => {
            assert_eq!(CrateVersion::try_parse($input), Err($expected))
        };
    }

    assert_parse_err!("10", "expected `.` after major version number");
    assert_parse_err!("10.", "expected minor version number");
    assert_parse_err!("10.0", "expected `.` after minor version number");
    assert_parse_err!("10.0.", "expected patch version number");
    assert_parse_err!("10.0.2-", "expected `alpha` or `rc` after `-`");
    assert_parse_err!("10.0.2-alpha", "expected `.` after `-alpha`");
    assert_parse_err!("10.0.2-alpha.", "expected digit after `-alpha.`");
    assert_parse_err!(
        "10.0.2-alpha.255",
        "`-alpha` build number is larger than 63"
    );
    assert_parse_err!("10.0.2-rc", "expected `.` after `-rc`");
    assert_parse_err!("10.0.2-rc.", "expected digit after `-rc.`");
    assert_parse_err!("10.0.2-rc.255", "`-rc` build number is larger than 63");
    assert_parse_err!("10.0.2-alpha.1+", "expected `dev` after `+`");
    assert_parse_err!("10.0.2-rc.1+dev", "unexpected `-rc` with `+dev`");
    assert_parse_err!("10.0.2+dev", "unexpected `+dev` without `-alpha`");
    assert_parse_err!(
        "10.0.2-alpha.1+dev extra_characters",
        "expected end of string"
    );
    assert_parse_err!("256.0.2-alpha.1+dev", "digit cannot be larger than 255");
    assert_parse_err!("10.256.2-alpha.1+dev", "digit cannot be larger than 255");
    assert_parse_err!("10.0.256-alpha.1+dev", "digit cannot be larger than 255");
    assert_parse_err!("10.0.2-alpha.256+dev", "digit cannot be larger than 255");
    assert_parse_err!(
        "01.0.2-alpha.256+dev",
        "multi-digit number cannot start with zero"
    );
    assert_parse_err!(
        "10.01.2-alpha.256+dev",
        "multi-digit number cannot start with zero"
    );
    assert_parse_err!(
        "10.0.01-alpha.256+dev",
        "multi-digit number cannot start with zero"
    );
    assert_parse_err!(
        "10.0.2-alpha.01+dev",
        "multi-digit number cannot start with zero"
    );
}