fif/src/utils.rs

41 lines
1.8 KiB
Rust

use once_cell::sync::OnceCell;
use cfg_if::cfg_if;
/// The current version of fif, as defined in Cargo.toml.
pub const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
cfg_if! {
if #[cfg(any(all(unix, feature = "infer-backend"), all(not(unix), not(feature = "xdg-mime-backend"))))] {
/// The backend being used; either "Infer" or "XDG-Mime".
pub const BACKEND: &str = "Infer";
} else {
/// The backend being used; either "Infer" or "XDG-Mime".
pub const BACKEND: &str = "XDG-Mime";
}
}
// the version and long_version given to clap need to be a &str, but we want to use format!, which returns a String.
// we can't just do something like `version = format!(...).as_str()`, because clap needs to know that the version will
// live for a given lifetime, which we need to satisfy by making our String static. of course, you can't use format!
// statically, so we need to use a OnceCell or similar to get around this.
static CLAP_VERSION: OnceCell<String> = OnceCell::new();
static CLAP_LONG_VERSION: OnceCell<String> = OnceCell::new();
/// Sets [`CLAP_VERSION`] to be the version defined in Cargo.toml, prefixed with a v (e.g. "v0.3.1"), then returns it as
/// a String.
pub(crate) fn clap_version() -> &'static str {
CLAP_VERSION
.set(format!("v{}", VERSION.unwrap_or("???")))
.unwrap_or_default(); // it doesn't matter if CLAP_VERSION has already been set
CLAP_VERSION.get().unwrap().as_str()
}
/// Sets [`CLAP_LONG_VERSION`] to be similar to [`CLAP_VERSION`], followed by the chosen backend in parentheses (e.g.
/// "v0.3.1 (XDG-Mime backend)"), then returns it as a String.
pub(crate) fn clap_long_version() -> &'static str {
CLAP_LONG_VERSION
.set(format!("v{} ({} backend)", VERSION.unwrap_or("???"), BACKEND))
.unwrap_or_default();
CLAP_LONG_VERSION.get().unwrap().as_str()
}