2022-01-22 16:41:24 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021-2022 Lynnesbian
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2021-10-05 14:24:08 +00:00
|
|
|
|
2021-09-24 14:53:35 +00:00
|
|
|
//! Various minor utilities.
|
|
|
|
|
2021-10-05 16:36:09 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-06-14 07:07:38 +00:00
|
|
|
use cfg_if::cfg_if;
|
2021-10-05 16:36:09 +00:00
|
|
|
use mime::Mime;
|
2021-09-24 14:53:35 +00:00
|
|
|
use once_cell::sync::Lazy;
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:30:13 +00:00
|
|
|
/// The version defined in Cargo.toml, prefixed with a v (e.g. "v0.4.0")
|
2021-09-24 14:53:35 +00:00
|
|
|
pub(crate) static CLAP_VERSION: Lazy<String> = Lazy::new(|| String::from("v") + VERSION.unwrap_or("???"));
|
2021-06-14 07:07:38 +00:00
|
|
|
|
2021-10-05 15:30:13 +00:00
|
|
|
/// The version defined in Cargo.toml, prefixed with a v (e.g. "v0.4.0"), followed by the chosen backend and
|
|
|
|
/// abbreviated git commit hash in parentheses - For example, "v0.4.0 (XDG-Mime backend, commit #043e097)"
|
2021-09-24 14:53:35 +00:00
|
|
|
pub static CLAP_LONG_VERSION: Lazy<String> = Lazy::new(|| {
|
2022-01-01 01:55:49 +00:00
|
|
|
// handle cases where GIT_SHA is set to an empty string
|
|
|
|
let commit = match option_env!("GIT_SHA") {
|
|
|
|
Some(hash) if !hash.trim().is_empty() => String::from("commit #") + hash,
|
|
|
|
_ => String::from("unknown commit"),
|
|
|
|
};
|
|
|
|
|
2022-01-01 03:02:27 +00:00
|
|
|
format!("v{} ({}, {} backend, {})", VERSION.unwrap_or("???"), os_name(), BACKEND, commit).into()
|
2021-09-24 14:53:35 +00:00
|
|
|
});
|
2021-06-14 07:35:47 +00:00
|
|
|
|
2021-11-24 20:29:27 +00:00
|
|
|
/// A [`Mime`] representing the "application/zip" MIME type.
|
2021-10-05 16:36:09 +00:00
|
|
|
pub static APPLICATION_ZIP: Lazy<Mime> = Lazy::new(|| Mime::from_str("application/zip").unwrap());
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|