pub fn version() -> TypstVersion {
*crate::singleton!(TypstVersion, {
let raw = env!("TYPST_VERSION");
let commit = option_env!("TYPST_COMMIT_SHA");
match semver::Version::parse(raw) {
Ok(version) => {
return TypstVersion {
major: version.major.try_into().unwrap(),
minor: version.minor.try_into().unwrap(),
patch: version.patch.try_into().unwrap(),
raw,
commit,
};
}
Err(err) => {
panic!("failed to parse {raw:?} as semantic version number: {err:?}")
}
}
})
}
#[derive(Debug, Clone, Copy)]
pub struct TypstVersion {
major: u32,
minor: u32,
patch: u32,
raw: &'static str,
commit: Option<&'static str>,
}
impl TypstVersion {
pub fn major(&self) -> u32 {
self.major
}
pub fn minor(&self) -> u32 {
self.minor
}
pub fn patch(&self) -> u32 {
self.patch
}
pub fn raw(&self) -> &'static str {
self.raw
}
pub fn commit(&self) -> Option<&'static str> {
self.commit
}
}
pub fn display_commit(commit: Option<&'static str>) -> &'static str {
const LENGTH: usize = 8;
match commit {
Some(s) => &s[..s.len().min(LENGTH)],
None => "unknown commit",
}
}