typst_library/foundations/
version.rs1use 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#[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 pub const COMPONENTS: [&'static str; 3] = ["major", "minor", "patch"];
33
34 pub fn new() -> Self {
36 Self::default()
37 }
38
39 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 pub fn push(&mut self, component: u32) {
53 self.0.push(component);
54 }
55
56 pub fn values(&self) -> &[u32] {
58 &self.0
59 }
60}
61
62#[scope]
63impl Version {
64 #[func(constructor)]
76 pub fn construct(
77 #[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 #[func]
100 pub fn at(
101 &self,
102 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
192pub 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}