Skip to main content

typst_utils/
version.rs

1//! Typst version information.
2
3/// Returns the version of Typst.
4///
5/// The information is read from the following sources:
6///
7/// - For the version number: The `TYPST_VERSION` environment variable
8/// - For the commit hash: The `TYPST_COMMIT_SHA` environment variable
9///
10/// Build tooling can set these environment variables to configure the exposed
11/// information. If the environment variables are left unset, the values are
12/// populated via `build.rs` from the Cargo package manifest version and the git
13/// hash in the current repository (if any).
14///
15/// # Panics
16/// If the `TYPST_VERSION` environment variable holds a version string that
17/// doesn't conform to SemVer.
18pub fn version() -> TypstVersion {
19    *crate::singleton!(TypstVersion, {
20        let raw = env!("TYPST_VERSION");
21        let commit = option_env!("TYPST_COMMIT_SHA");
22        match semver::Version::parse(raw) {
23            Ok(version) => {
24                return TypstVersion {
25                    major: version.major.try_into().unwrap(),
26                    minor: version.minor.try_into().unwrap(),
27                    patch: version.patch.try_into().unwrap(),
28                    raw,
29                    commit,
30                };
31            }
32            Err(err) => {
33                panic!("failed to parse {raw:?} as semantic version number: {err:?}")
34            }
35        }
36    })
37}
38
39/// Typst version definition.
40///
41/// This structure contains the current Typst version. To query the precise
42/// version number, refer to the [`TypstVersion::major()`],
43/// [`TypstVersion::minor()`] and [`TypstVersion::patch()`] functions. You can
44/// read the underlying, raw version string (e.g., for CLI output) with
45/// [`TypstVersion::raw`].
46///
47/// Optionally, this may also contain the hash value of the Git commit from
48/// which Typst was built. However, this field may be unpopulated.
49#[derive(Debug, Clone, Copy)]
50pub struct TypstVersion {
51    /// Typst major version number.
52    major: u32,
53    /// Typst minor version number.
54    minor: u32,
55    /// Typst patch version number.
56    patch: u32,
57    /// Raw, unmodified version string.
58    raw: &'static str,
59    /// The raw commit hash.
60    commit: Option<&'static str>,
61}
62
63impl TypstVersion {
64    /// Returns the Typst major version.
65    pub fn major(&self) -> u32 {
66        self.major
67    }
68
69    /// Returns the Typst minor version.
70    pub fn minor(&self) -> u32 {
71        self.minor
72    }
73
74    /// Returns the Typst patch version.
75    pub fn patch(&self) -> u32 {
76        self.patch
77    }
78
79    /// Returns the raw, unparsed version string.
80    ///
81    /// Guaranteed to conform to SemVer.
82    pub fn raw(&self) -> &'static str {
83        self.raw
84    }
85
86    /// Returns the commit Typst was built from, if known.
87    pub fn commit(&self) -> Option<&'static str> {
88        self.commit
89    }
90}
91
92/// Displays the commit Typst was built from human-readably.
93pub fn display_commit(commit: Option<&'static str>) -> &'static str {
94    const LENGTH: usize = 8;
95    match commit {
96        Some(s) => &s[..s.len().min(LENGTH)],
97        None => "unknown commit",
98    }
99}