From 6934dd0b5ecb8d3d8edd50b407acdad0eb5db6c3 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Mon, 14 Jun 2021 17:07:38 +1000 Subject: [PATCH] move some commonly used constants into a single module --- src/formats.rs | 4 ++-- src/main.rs | 6 ++---- src/parameters.rs | 31 +------------------------------ src/utils.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 36 deletions(-) create mode 100644 src/utils.rs diff --git a/src/formats.rs b/src/formats.rs index 054783a..d63b076 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -10,8 +10,8 @@ use cfg_if::cfg_if; use snailquote::escape; use crate::findings::ScanError; -use crate::parameters::VERSION; -use crate::{Findings, BACKEND}; +use crate::utils::{VERSION, BACKEND}; +use crate::{Findings}; use itertools::Itertools; /// A macro for creating an array of `Writable`s without needing to pepper your code with `into()`s. diff --git a/src/main.rs b/src/main.rs index c22a8e7..a6e672a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,18 +45,15 @@ pub(crate) mod string_type; #[cfg(test)] mod tests; +mod utils; cfg_if! { if #[cfg(any(all(unix, feature = "infer-backend"), all(not(unix), not(feature = "xdg-mime-backend"))))] { /// A [OnceCell] holding an instance of [mime_db::MimeDb]. static MIMEDB: OnceCell = OnceCell::new(); - /// The backend being used; either "Infer" or "XDG-Mime". - const BACKEND: &str = "Infer"; } else { /// A [OnceCell] holding an instance of [mime_db::MimeDb]. static MIMEDB: OnceCell = OnceCell::new(); - /// The backend being used; either "Infer" or "XDG-Mime". - const BACKEND: &str = "XDG-Mime"; } } @@ -74,6 +71,7 @@ fn main() { // .target(env_logger::Target::Stdout) // log to stdout rather than stderr .init(); + // trace!("fif {}", CLAP_LONG_VERSION.get().unwrap()); trace!("Initialise mimetype database"); init_db(); diff --git a/src/parameters.rs b/src/parameters.rs index a6ad95a..37c1850 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -1,10 +1,9 @@ //! [Clap] struct used to parse command line arguments. use crate::string_type::String as StringType; -use crate::BACKEND; +use crate::utils::{clap_version, clap_long_version}; use cfg_if::cfg_if; use clap::{AppSettings, Clap}; -use once_cell::sync::OnceCell; use std::collections::BTreeSet; use std::path::PathBuf; @@ -16,34 +15,6 @@ cfg_if! { } } -/// The current version of fif, as defined in Cargo.toml. -pub const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); - -// 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 = OnceCell::new(); -static CLAP_LONG_VERSION: OnceCell = 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. -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. -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() -} - #[derive(Clap, PartialEq, Debug)] pub enum OutputFormat { /// A Bourne shell compatible script. diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..8673323 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,40 @@ +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 = OnceCell::new(); +static CLAP_LONG_VERSION: OnceCell = 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() +}