Skip to main content

xila_version/
lib.rs

1//! The running Xila version and the rule that decides which applications it runs.
2//!
3//! Applications declare the Xila version they were built for as `major.minor`
4//! instead of a separate ABI number (xila-project/core#276). [`RUNNING`] is the
5//! one place that states the running version: it is derived at build time from
6//! Core's workspace version, so bumping `[workspace.package] version` updates
7//! the native ABI note, the native API table and every loader check together.
8#![no_std]
9
10use core::fmt;
11
12/// A Xila version as `major.minor`. The patch version never affects compatibility.
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub struct Version {
15    pub major: u16,
16    pub minor: u16,
17}
18
19impl Version {
20    pub const fn new(major: u16, minor: u16) -> Self {
21        Self { major, minor }
22    }
23
24    /// The 32-bit form stored in the native ELF ABI note and in `XilaApi.version`:
25    /// the major version in the upper 16 bits and the minor version in the lower 16 bits.
26    pub const fn packed(self) -> u32 {
27        ((self.major as u32) << 16) | self.minor as u32
28    }
29
30    /// The inverse of [`Version::packed`]. Every 32-bit value names exactly one version.
31    pub const fn from_packed(packed: u32) -> Self {
32        Self::new((packed >> 16) as u16, packed as u16)
33    }
34}
35
36impl fmt::Display for Version {
37    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(formatter, "{}.{}", self.major, self.minor)
39    }
40}
41
42/// Check whether the `running` Xila version can run an application built for `declared`.
43///
44/// Before 1.0, both versions must have major version zero and the same minor
45/// version. From 1.0, the major versions must be equal and the running minor
46/// version must be at least the declared one.
47///
48/// This mirrors `xila_package_manifest::compatible` in package_manager, which
49/// Core cannot depend on. Keep both, and their boundary tests, identical.
50pub const fn compatible(declared: Version, running: Version) -> bool {
51    declared.major == running.major
52        && if declared.major == 0 {
53            declared.minor == running.minor
54        } else {
55            declared.minor <= running.minor
56        }
57}
58
59/// The Xila version this build of Core is, from `[workspace.package] version`.
60pub const RUNNING: Version = Version::new(
61    parse(env!("CARGO_PKG_VERSION_MAJOR")),
62    parse(env!("CARGO_PKG_VERSION_MINOR")),
63);
64
65/// Parse a decimal version component at compile time.
66const fn parse(text: &str) -> u16 {
67    let bytes = text.as_bytes();
68    assert!(!bytes.is_empty(), "empty version component");
69    let mut value: u32 = 0;
70    let mut index = 0;
71    while index < bytes.len() {
72        let digit = bytes[index];
73        assert!(digit.is_ascii_digit(), "non-decimal version component");
74        value = value * 10 + (digit - b'0') as u32;
75        assert!(value <= u16::MAX as u32, "version component above 65535");
76        index += 1;
77    }
78    value as u16
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    fn version(text: &str) -> Version {
86        let (major, minor) = text.split_once('.').unwrap();
87        Version::new(major.parse().unwrap(), minor.parse().unwrap())
88    }
89
90    // The same cases as `xila_package_manifest`'s compatibility test.
91    #[test]
92    fn checks_compatibility_at_major_and_minor_boundaries() {
93        for (declared, running, expected) in [
94            ("0.1", "0.1", true),
95            ("0.1", "0.2", false),
96            ("0.2", "0.1", false),
97            ("1.3", "1.2", false),
98            ("1.3", "1.3", true),
99            ("1.3", "1.9", true),
100            ("1.3", "2.0", false),
101            ("0.9", "1.0", false),
102            ("0.0", "0.0", true),
103            ("0.0", "0.1", false),
104            ("0.1", "0.0", false),
105        ] {
106            assert_eq!(
107                compatible(version(declared), version(running)),
108                expected,
109                "{declared} on {running}"
110            );
111        }
112    }
113
114    #[test]
115    fn running_version_follows_the_workspace_version() {
116        let mut parts = env!("CARGO_PKG_VERSION").split('.');
117        assert_eq!(RUNNING.major, parts.next().unwrap().parse().unwrap());
118        assert_eq!(RUNNING.minor, parts.next().unwrap().parse().unwrap());
119        assert!(compatible(RUNNING, RUNNING));
120    }
121
122    #[test]
123    fn packs_major_above_minor() {
124        assert_eq!(Version::new(0, 1).packed(), 0x0000_0001);
125        assert_eq!(Version::new(1, 3).packed(), 0x0001_0003);
126        assert_eq!(Version::new(u16::MAX, 2).packed(), 0xffff_0002);
127        for packed in [0, 1, 0x0001_0003, 0x8000_ffff, u32::MAX] {
128            assert_eq!(Version::from_packed(packed).packed(), packed);
129        }
130    }
131
132    #[test]
133    fn parses_decimal_components() {
134        assert_eq!(parse("0"), 0);
135        assert_eq!(parse("12"), 12);
136        assert_eq!(parse("65535"), u16::MAX);
137    }
138
139    #[test]
140    fn displays_major_and_minor() {
141        extern crate std;
142        use std::string::ToString;
143        assert_eq!(Version::new(1, 3).to_string(), "1.3");
144    }
145}