Skip to main content

pubhubs/servers/
version.rs

1/// PubHubs servers version  (e.g. `"v2.2.0"`), extracted from `PH_VERSION` compile
2/// time environmental variable if set, and otherwise from the git repository
3/// if available.  Set to `"n/a"` when not available.
4pub const VERSION: &str = match std::option_env!("PH_VERSION") {
5    Some(version) => version,
6    None => git_version::git_version!(args = ["--tags"], fallback = "n/a"),
7}; // Note:  Option::unwrap_or_(else) is not const
8
9/// Returns the PubHubs servers version when available.
10pub fn version() -> Option<&'static str> {
11    if VERSION == "n/a" {
12        return None;
13    }
14
15    Some(VERSION)
16}
17
18/// Attempts to parse `version` as `semver::Version` after stripping off the leading `v`.
19pub fn to_semver(version: impl AsRef<str>) -> anyhow::Result<semver::Version> {
20    let version = version.as_ref();
21
22    if version.is_empty() || version[0..1] != *"v" {
23        anyhow::bail!("missing leading 'v'");
24    }
25
26    Ok(semver::Version::parse(&version[1..])?)
27}