typst_library/foundations/
version.rs

1use std::cmp::Ordering;
2use std::fmt::{self, Display, Formatter, Write};
3use std::hash::Hash;
4use std::iter::repeat;
5
6use ecow::{EcoString, EcoVec, eco_format};
7
8use crate::diag::{StrResult, bail};
9use crate::foundations::{Repr, cast, func, repr, scope, ty};
10
11/// A version with an arbitrary number of components.
12///
13/// The first three components have names that can be used as fields: `major`,
14/// `minor`, `patch`. All following components do not have names.
15///
16/// The list of components is semantically extended by an infinite list of
17/// zeros. This means that, for example, `0.8` is the same as `0.8.0`. As a
18/// special case, the empty version (that has no components at all) is the same
19/// as `0`, `0.0`, `0.0.0`, and so on.
20///
21/// The current version of the Typst compiler is available as `sys.version`.
22///
23/// You can convert a version to an array of explicitly given components using
24/// the [`array`] constructor.
25#[ty(scope, cast)]
26#[derive(Debug, Default, Clone, Hash)]
27#[allow(clippy::derived_hash_with_manual_eq)]
28pub struct Version(EcoVec<u32>);
29
30impl Version {
31    /// The names for the first components of a version.
32    pub const COMPONENTS: [&'static str; 3] = ["major", "minor", "patch"];
33
34    /// Create a new (empty) version.
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Get a named component of a version.
40    ///
41    /// Always non-negative. Returns `0` if the version isn't specified to the
42    /// necessary length.
43    pub fn component(&self, name: &str) -> StrResult<i64> {
44        self.0
45            .iter()
46            .zip(Self::COMPONENTS)
47            .find_map(|(&i, s)| (s == name).then_some(i as i64))
48            .ok_or_else(|| "unknown version component".into())
49    }
50
51    /// Push a component to the end of this version.
52    pub fn push(&mut self, component: u32) {
53        self.0.push(component);
54    }
55
56    /// The values of the version
57    pub fn values(&self) -> &[u32] {
58        &self.0
59    }
60}
61
62#[scope]
63impl Version {
64    /// Creates a new version.
65    ///
66    /// It can have any number of components (even zero).
67    ///
68    /// ```example
69    /// #version() \
70    /// #version(1) \
71    /// #version(1, 2, 3, 4) \
72    /// #version((1, 2, 3, 4)) \
73    /// #version((1, 2), 3)
74    /// ```
75    #[func(constructor)]
76    pub fn construct(
77        /// The components of the version (array arguments are flattened)
78        #[variadic]
79        components: Vec<VersionComponents>,
80    ) -> Version {
81        let mut version = Version::new();
82        for c in components {
83            match c {
84                VersionComponents::Single(v) => version.push(v),
85                VersionComponents::Multiple(values) => {
86                    for v in values {
87                        version.push(v);
88                    }
89                }
90            }
91        }
92        version
93    }
94
95    /// Retrieves a component of a version.
96    ///
97    /// The returned integer is always non-negative. Returns `0` if the version
98    /// isn't specified to the necessary length.
99    #[func]
100    pub fn at(
101        &self,
102        /// The index at which to retrieve the component. If negative, indexes
103        /// from the back of the explicitly given components.
104        index: i64,
105    ) -> StrResult<i64> {
106        let mut index = index;
107        if index < 0 {
108            match (self.0.len() as i64).checked_add(index) {
109                Some(pos_index) if pos_index >= 0 => index = pos_index,
110                _ => bail!(
111                    "component index out of bounds (index: {index}, len: {})",
112                    self.0.len()
113                ),
114            }
115        }
116        Ok(usize::try_from(index)
117            .ok()
118            .and_then(|i| self.0.get(i).copied())
119            .unwrap_or_default() as i64)
120    }
121}
122
123impl FromIterator<u32> for Version {
124    fn from_iter<T: IntoIterator<Item = u32>>(iter: T) -> Self {
125        Self(EcoVec::from_iter(iter))
126    }
127}
128
129impl IntoIterator for Version {
130    type Item = u32;
131    type IntoIter = ecow::vec::IntoIter<u32>;
132
133    fn into_iter(self) -> Self::IntoIter {
134        self.0.into_iter()
135    }
136}
137
138impl Ord for Version {
139    fn cmp(&self, other: &Self) -> Ordering {
140        let max_len = self.0.len().max(other.0.len());
141        let tail = repeat(&0);
142
143        let self_iter = self.0.iter().chain(tail.clone());
144        let other_iter = other.0.iter().chain(tail);
145
146        for (l, r) in self_iter.zip(other_iter).take(max_len) {
147            match l.cmp(r) {
148                Ordering::Equal => (),
149                ord => return ord,
150            }
151        }
152
153        Ordering::Equal
154    }
155}
156
157impl PartialOrd for Version {
158    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
159        Some(self.cmp(other))
160    }
161}
162
163impl Eq for Version {}
164
165impl PartialEq for Version {
166    fn eq(&self, other: &Self) -> bool {
167        matches!(self.cmp(other), Ordering::Equal)
168    }
169}
170
171impl Display for Version {
172    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
173        let mut first = true;
174        for &v in &self.0 {
175            if !first {
176                f.write_char('.')?;
177            }
178            write!(f, "{v}")?;
179            first = false;
180        }
181        Ok(())
182    }
183}
184
185impl Repr for Version {
186    fn repr(&self) -> EcoString {
187        let parts: Vec<_> = self.0.iter().map(|v| eco_format!("{v}")).collect();
188        eco_format!("version{}", &repr::pretty_array_like(&parts, false))
189    }
190}
191
192/// One or multiple version components.
193pub enum VersionComponents {
194    Single(u32),
195    Multiple(Vec<u32>),
196}
197
198cast! {
199    VersionComponents,
200    v: u32 => Self::Single(v),
201    v: Vec<u32> => Self::Multiple(v)
202}