Skip to main content

qt_version/
lib.rs

1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use semver::Version;
5use std::fmt;
6
7#[derive(Debug, PartialEq)]
8pub enum VersionError {
9    MinExceedsExact { min: Version, exact: Version },
10    MaxPrecedesExact { max: Version, exact: Version },
11    MinExceedsMax { min: Version, max: Version },
12}
13
14impl fmt::Display for VersionError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            VersionError::MinExceedsExact { min, exact } => {
18                write!(f, "min version {min} is greater than exact version {exact}")
19            }
20            VersionError::MaxPrecedesExact { max, exact } => {
21                write!(f, "max version {max} is earlier than exact version {exact}")
22            }
23            VersionError::MinExceedsMax { min, max } => {
24                write!(f, "min version {min} is greater than max version {max}")
25            }
26        }
27    }
28}
29
30fn supported_qt_versions() -> Vec<Version> {
31    vec![
32        Version::new(6, 2, 0),
33        Version::new(6, 3, 0),
34        Version::new(6, 4, 0),
35        Version::new(6, 5, 0),
36        Version::new(6, 6, 0),
37        Version::new(6, 7, 0),
38        Version::new(6, 8, 0),
39        Version::new(6, 9, 0),
40        Version::new(6, 10, 0),
41        Version::new(6, 11, 0),
42        Version::new(6, 12, 0),
43    ]
44}
45
46fn default_version() -> Option<Version> {
47    if cfg!(feature = "qt_version_default") {
48        Some(Version::new(6, 10, 2))
49    } else {
50        None
51    }
52}
53
54fn exact_version() -> Option<Version> {
55    #[cfg(feature = "qt_version_exact_6_12_0")]
56    {
57        return Some(Version::new(6, 12, 0));
58    }
59    #[cfg(feature = "qt_version_exact_6_11_0")]
60    {
61        return Some(Version::new(6, 11, 0));
62    }
63    #[cfg(feature = "qt_version_exact_6_10_2")]
64    {
65        return Some(Version::new(6, 10, 2));
66    }
67    #[cfg(feature = "qt_version_exact_6_10_1")]
68    {
69        return Some(Version::new(6, 10, 1));
70    }
71    #[cfg(feature = "qt_version_exact_6_10_0")]
72    {
73        return Some(Version::new(6, 10, 0));
74    }
75    #[cfg(feature = "qt_version_exact_6_9_3")]
76    {
77        return Some(Version::new(6, 9, 3));
78    }
79    #[cfg(feature = "qt_version_exact_6_9_2")]
80    {
81        return Some(Version::new(6, 9, 2));
82    }
83    #[cfg(feature = "qt_version_exact_6_9_1")]
84    {
85        return Some(Version::new(6, 9, 1));
86    }
87    #[cfg(feature = "qt_version_exact_6_9_0")]
88    {
89        return Some(Version::new(6, 9, 0));
90    }
91    #[cfg(feature = "qt_version_exact_6_8_6")]
92    {
93        return Some(Version::new(6, 8, 6));
94    }
95    #[cfg(feature = "qt_version_exact_6_8_5")]
96    {
97        return Some(Version::new(6, 8, 5));
98    }
99    #[cfg(feature = "qt_version_exact_6_8_4")]
100    {
101        return Some(Version::new(6, 8, 4));
102    }
103    #[cfg(feature = "qt_version_exact_6_8_3")]
104    {
105        return Some(Version::new(6, 8, 3));
106    }
107    #[cfg(feature = "qt_version_exact_6_8_2")]
108    {
109        return Some(Version::new(6, 8, 2));
110    }
111    #[cfg(feature = "qt_version_exact_6_8_1")]
112    {
113        return Some(Version::new(6, 8, 1));
114    }
115    #[cfg(feature = "qt_version_exact_6_8_0")]
116    {
117        return Some(Version::new(6, 8, 0));
118    }
119    #[cfg(feature = "qt_version_exact_6_7_0")]
120    {
121        return Some(Version::new(6, 7, 0));
122    }
123    #[cfg(feature = "qt_version_exact_6_6_0")]
124    {
125        return Some(Version::new(6, 6, 0));
126    }
127    #[cfg(feature = "qt_version_exact_6_5_0")]
128    {
129        return Some(Version::new(6, 5, 0));
130    }
131    #[cfg(feature = "qt_version_exact_6_4_0")]
132    {
133        return Some(Version::new(6, 4, 0));
134    }
135    #[cfg(feature = "qt_version_exact_6_3_0")]
136    {
137        return Some(Version::new(6, 3, 0));
138    }
139    #[cfg(feature = "qt_version_exact_6_2_0")]
140    {
141        return Some(Version::new(6, 2, 0));
142    }
143    None
144}
145
146fn min_version() -> Option<Version> {
147    #[cfg(feature = "qt_version_at_least_6_12")]
148    {
149        return Some(Version::new(6, 12, 0));
150    }
151    #[cfg(feature = "qt_version_at_least_6_11")]
152    {
153        return Some(Version::new(6, 11, 0));
154    }
155    #[cfg(feature = "qt_version_at_least_6_10")]
156    {
157        return Some(Version::new(6, 10, 0));
158    }
159
160    #[cfg(feature = "qt_version_at_least_6_9")]
161    {
162        return Some(Version::new(6, 9, 0));
163    }
164
165    #[cfg(feature = "qt_version_at_least_6_8")]
166    {
167        return Some(Version::new(6, 8, 0));
168    }
169
170    #[cfg(feature = "qt_version_at_least_6_7")]
171    {
172        return Some(Version::new(6, 7, 0));
173    }
174
175    #[cfg(feature = "qt_version_at_least_6_6")]
176    {
177        return Some(Version::new(6, 6, 0));
178    }
179
180    #[cfg(feature = "qt_version_at_least_6_5")]
181    {
182        return Some(Version::new(6, 5, 0));
183    }
184
185    #[cfg(feature = "qt_version_at_least_6_4")]
186    {
187        return Some(Version::new(6, 4, 0));
188    }
189
190    #[cfg(feature = "qt_version_at_least_6_3")]
191    {
192        return Some(Version::new(6, 3, 0));
193    }
194
195    #[cfg(feature = "qt_version_at_least_6_2")]
196    {
197        return Some(Version::new(6, 2, 0));
198    }
199
200    None
201}
202
203fn max_version() -> Option<Version> {
204    #[cfg(feature = "qt_version_at_most_6_2")]
205    {
206        return Some(Version::new(6, 2, 0));
207    }
208    #[cfg(feature = "qt_version_at_most_6_3")]
209    {
210        return Some(Version::new(6, 3, 0));
211    }
212    #[cfg(feature = "qt_version_at_most_6_4")]
213    {
214        return Some(Version::new(6, 4, 0));
215    }
216    #[cfg(feature = "qt_version_at_most_6_5")]
217    {
218        return Some(Version::new(6, 5, 0));
219    }
220    #[cfg(feature = "qt_version_at_most_6_6")]
221    {
222        return Some(Version::new(6, 6, 0));
223    }
224    #[cfg(feature = "qt_version_at_most_6_7")]
225    {
226        return Some(Version::new(6, 7, 0));
227    }
228    #[cfg(feature = "qt_version_at_most_6_8")]
229    {
230        return Some(Version::new(6, 8, 0));
231    }
232    #[cfg(feature = "qt_version_at_most_6_9")]
233    {
234        return Some(Version::new(6, 9, 0));
235    }
236    #[cfg(feature = "qt_version_at_most_6_10")]
237    {
238        return Some(Version::new(6, 10, 0));
239    }
240    #[cfg(feature = "qt_version_at_most_6_11")]
241    {
242        return Some(Version::new(6, 11, 0));
243    }
244    #[cfg(feature = "qt_version_at_most_6_12")]
245    {
246        return Some(Version::new(6, 12, 0));
247    }
248
249    None
250}
251
252fn resolve_versions(
253    supported: &[Version],
254    exact: Option<Version>,
255    min: Option<Version>,
256    max: Option<Version>,
257) -> Result<Vec<Version>, VersionError> {
258    if let Some(exact_v) = exact {
259        if let Some(min_v) = min {
260            if min_v > exact_v {
261                return Err(VersionError::MinExceedsExact {
262                    min: min_v,
263                    exact: exact_v,
264                });
265            }
266        }
267        if let Some(max_v) = max {
268            if max_v < exact_v {
269                return Err(VersionError::MaxPrecedesExact {
270                    max: max_v,
271                    exact: exact_v,
272                });
273            }
274        }
275        return Ok(vec![exact_v]);
276    }
277
278    if let (Some(min_v), Some(max_v)) = (min.as_ref(), max.as_ref()) {
279        if min_v > max_v {
280            return Err(VersionError::MinExceedsMax {
281                min: min_v.clone(),
282                max: max_v.clone(),
283            });
284        }
285    }
286    Ok(supported
287        .iter()
288        .filter(|&v| {
289            let ok_min = min.as_ref().is_none_or(|m| *v >= *m);
290            let ok_max = max.as_ref().is_none_or(|m| *v <= *m);
291            ok_min && ok_max
292        })
293        .cloned()
294        .collect())
295}
296
297pub fn qt_versions() -> Vec<Version> {
298    let supported = supported_qt_versions();
299    let exact = exact_version().or(default_version());
300    let min = min_version();
301    let max = max_version();
302
303    resolve_versions(&supported, exact, min, max)
304        .expect("invalid Qt version feature flag combination")
305}
306
307#[cfg(test)]
308mod tests {
309    use super::*;
310
311    #[test]
312    fn test_range_filter() {
313        let supported = vec![
314            Version::new(6, 5, 0),
315            Version::new(6, 6, 0),
316            Version::new(6, 7, 0),
317            Version::new(6, 8, 0),
318            Version::new(6, 9, 0),
319            Version::new(6, 10, 0),
320        ];
321
322        let result = resolve_versions(
323            &supported,
324            None,
325            Some(Version::new(6, 7, 0)),
326            Some(Version::new(6, 9, 0)),
327        );
328
329        assert_eq!(
330            result,
331            Ok(vec![
332                Version::new(6, 7, 0),
333                Version::new(6, 8, 0),
334                Version::new(6, 9, 0),
335            ])
336        );
337    }
338
339    #[test]
340    fn test_exact() {
341        let supported = vec![
342            Version::new(6, 5, 0),
343            Version::new(6, 6, 0),
344            Version::new(6, 7, 0),
345            Version::new(6, 8, 0),
346            Version::new(6, 9, 0),
347            Version::new(6, 10, 0),
348        ];
349
350        let result = resolve_versions(
351            &supported,
352            Some(Version::new(6, 6, 0)),
353            Some(Version::new(6, 5, 0)),
354            Some(Version::new(6, 7, 0)),
355        );
356
357        assert_eq!(result, Ok(vec![Version::new(6, 6, 0)]));
358    }
359
360    #[test]
361    fn test_exact_smaller_than_range() {
362        let supported = vec![
363            Version::new(6, 5, 0),
364            Version::new(6, 6, 0),
365            Version::new(6, 7, 0),
366            Version::new(6, 8, 0),
367            Version::new(6, 9, 0),
368            Version::new(6, 10, 0),
369        ];
370
371        let result = resolve_versions(
372            &supported,
373            Some(Version::new(6, 3, 0)),
374            Some(Version::new(6, 5, 0)),
375            Some(Version::new(6, 7, 0)),
376        );
377
378        assert_eq!(
379            result,
380            Err(VersionError::MinExceedsExact {
381                min: Version::new(6, 5, 0),
382                exact: Version::new(6, 3, 0),
383            })
384        );
385    }
386
387    #[test]
388    fn test_exact_bigger_than_range() {
389        let supported = vec![
390            Version::new(6, 5, 0),
391            Version::new(6, 6, 0),
392            Version::new(6, 7, 0),
393            Version::new(6, 8, 0),
394            Version::new(6, 9, 0),
395            Version::new(6, 10, 0),
396        ];
397
398        let result = resolve_versions(
399            &supported,
400            Some(Version::new(6, 9, 0)),
401            Some(Version::new(6, 5, 0)),
402            Some(Version::new(6, 7, 0)),
403        );
404
405        assert_eq!(
406            result,
407            Err(VersionError::MaxPrecedesExact {
408                max: Version::new(6, 7, 0),
409                exact: Version::new(6, 9, 0),
410            })
411        );
412    }
413
414    #[test]
415    fn test_empty_range() {
416        let supported = vec![
417            Version::new(6, 5, 0),
418            Version::new(6, 6, 0),
419            Version::new(6, 7, 0),
420            Version::new(6, 8, 0),
421            Version::new(6, 9, 0),
422            Version::new(6, 10, 0),
423        ];
424
425        let result = resolve_versions(
426            &supported,
427            None,
428            Some(Version::new(6, 7, 0)),
429            Some(Version::new(6, 6, 0)),
430        );
431
432        assert_eq!(
433            result,
434            Err(VersionError::MinExceedsMax {
435                min: Version::new(6, 7, 0),
436                max: Version::new(6, 6, 0),
437            })
438        );
439    }
440
441    #[test]
442    fn test_no_minimum() {
443        let supported = vec![
444            Version::new(6, 5, 0),
445            Version::new(6, 6, 0),
446            Version::new(6, 7, 0),
447            Version::new(6, 8, 0),
448            Version::new(6, 9, 0),
449            Version::new(6, 10, 0),
450        ];
451
452        let result = resolve_versions(&supported, None, None, Some(Version::new(6, 9, 0)));
453
454        assert_eq!(
455            result,
456            Ok(vec![
457                Version::new(6, 5, 0),
458                Version::new(6, 6, 0),
459                Version::new(6, 7, 0),
460                Version::new(6, 8, 0),
461                Version::new(6, 9, 0),
462            ])
463        );
464    }
465
466    #[test]
467    fn test_no_maximum() {
468        let supported = vec![
469            Version::new(6, 5, 0),
470            Version::new(6, 6, 0),
471            Version::new(6, 7, 0),
472            Version::new(6, 8, 0),
473            Version::new(6, 9, 0),
474            Version::new(6, 10, 0),
475        ];
476
477        let result = resolve_versions(&supported, None, Some(Version::new(6, 7, 0)), None);
478
479        assert_eq!(
480            result,
481            Ok(vec![
482                Version::new(6, 7, 0),
483                Version::new(6, 8, 0),
484                Version::new(6, 9, 0),
485                Version::new(6, 10, 0),
486            ])
487        );
488    }
489}