2021-06-14 07:07:38 +00:00
|
|
|
use cfg_if::cfg_if;
|
2021-06-14 07:08:52 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
2021-06-14 07:07:38 +00:00
|
|
|
|
2021-09-22 15:33:10 +00:00
|
|
|
use crate::String;
|
|
|
|
|
2021-06-14 07:07:38 +00:00
|
|
|
/// 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
|
2021-09-22 16:09:57 +00:00
|
|
|
/// an str.
|
2021-07-31 13:33:18 +00:00
|
|
|
pub fn clap_version() -> &'static str { CLAP_VERSION.get_or_init(|| String::from("v") + VERSION.unwrap_or("???")) }
|
2021-06-14 07:07:38 +00:00
|
|
|
|
2021-09-22 16:09:57 +00:00
|
|
|
/// Sets [`CLAP_LONG_VERSION`] to be similar to [`CLAP_VERSION`], followed by the chosen backend and abbreviated git
|
|
|
|
/// commit hash in parentheses (e.g. "v0.3.6 (XDG-Mime backend, commit #043e097)"), then returns it as an str.
|
2021-06-14 07:08:52 +00:00
|
|
|
pub fn clap_long_version() -> &'static str {
|
2021-09-22 16:05:50 +00:00
|
|
|
CLAP_LONG_VERSION.get_or_init(|| {
|
|
|
|
format!(
|
|
|
|
"v{} ({} backend, commit #{})",
|
|
|
|
VERSION.unwrap_or("???"),
|
|
|
|
BACKEND,
|
|
|
|
option_env!("GIT_SHA").unwrap_or("???")
|
|
|
|
)
|
|
|
|
.into()
|
|
|
|
})
|
2021-06-14 07:07:38 +00:00
|
|
|
}
|
2021-06-14 07:35:47 +00:00
|
|
|
|
2021-07-01 08:52:53 +00:00
|
|
|
/// Returns the name of the target operating system with proper casing, like "Windows" or "macOS".
|
2021-06-14 07:54:11 +00:00
|
|
|
#[allow(clippy::option_map_unit_fn)]
|
2021-06-14 07:35:47 +00:00
|
|
|
pub fn os_name() -> String {
|
2021-09-22 15:33:10 +00:00
|
|
|
match std::env::consts::OS {
|
|
|
|
// special cases: "ios" should not be capitalised into "Ios", for example
|
2021-06-14 07:35:47 +00:00
|
|
|
"ios" => "iOS".into(),
|
|
|
|
"macos" => "macOS".into(),
|
|
|
|
"freebsd" => "FreeBSD".into(),
|
|
|
|
"openbsd" => "OpenBSD".into(),
|
|
|
|
"netbsd" => "NetBSD".into(),
|
|
|
|
"vxworks" => "VxWorks".into(),
|
2021-09-22 15:33:10 +00:00
|
|
|
os => {
|
2021-06-14 07:35:47 +00:00
|
|
|
// generic case: return consts::OS with the first letter in uppercase ("linux" -> "Linux")
|
2021-09-22 15:33:10 +00:00
|
|
|
let mut os_upper = String::from(os);
|
2021-06-14 07:35:47 +00:00
|
|
|
os_upper.get_mut(0..1).map(|first| first.make_ascii_uppercase());
|
|
|
|
os_upper
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|