From 3a51e18ce68d39a541977c62447654f99d681bf6 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 24 Jun 2024 22:06:20 +0000 Subject: [PATCH] pre-format version strings Signed-off-by: Jason Volk --- src/api/client/unversioned.rs | 6 ++--- src/api/server/version.rs | 4 ++-- src/core/mod.rs | 1 + src/core/version.rs | 43 +++++++++++++++++++---------------- src/main/clap.rs | 2 +- src/main/server.rs | 2 +- src/service/globals/client.rs | 6 +---- 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/api/client/unversioned.rs b/src/api/client/unversioned.rs index 56ab9a90..2b703f94 100644 --- a/src/api/client/unversioned.rs +++ b/src/api/client/unversioned.rs @@ -145,7 +145,7 @@ pub(crate) async fn syncv3_client_server_json() -> Result { Ok(Json(serde_json::json!({ "server": server_url, - "version": conduit::version::conduwuit(), + "version": conduit::version(), }))) } @@ -155,8 +155,8 @@ pub(crate) async fn syncv3_client_server_json() -> Result { /// `/_matrix/federation/v1/version` pub(crate) async fn conduwuit_server_version() -> Result { Ok(Json(serde_json::json!({ - "name": "conduwuit", - "version": conduit::version::conduwuit(), + "name": conduit::version::name(), + "version": conduit::version::version(), }))) } diff --git a/src/api/server/version.rs b/src/api/server/version.rs index 557b10dc..92837571 100644 --- a/src/api/server/version.rs +++ b/src/api/server/version.rs @@ -10,8 +10,8 @@ pub(crate) async fn get_server_version_route( ) -> Result { Ok(get_server_version::v1::Response { server: Some(get_server_version::v1::Server { - name: Some("Conduwuit".to_owned()), - version: Some(conduit::version::conduwuit()), + name: Some(conduit::version::name().into()), + version: Some(conduit::version::version().into()), }), }) } diff --git a/src/core/mod.rs b/src/core/mod.rs index 383b45ee..5ffe4cb9 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -13,6 +13,7 @@ pub use config::Config; pub use error::{Error, RumaResponse}; pub use pducount::PduCount; pub use server::Server; +pub use version::version; pub type Result = std::result::Result; diff --git a/src/core/version.rs b/src/core/version.rs index f65bac99..4a4e8426 100644 --- a/src/core/version.rs +++ b/src/core/version.rs @@ -4,25 +4,28 @@ /// Set the environment variable `CONDUWUIT_VERSION_EXTRA` to any UTF-8 string /// to include it in parenthesis after the SemVer version. A common value are /// git commit hashes. +use std::sync::OnceLock; + +static BRANDING: &str = "Conduwuit"; +static SEMANTIC: &str = env!("CARGO_PKG_VERSION"); + +static VERSION: OnceLock = OnceLock::new(); +static USER_AGENT: OnceLock = OnceLock::new(); + +#[inline] #[must_use] -pub fn conduwuit() -> String { - match option_env!("CONDUWUIT_VERSION_EXTRA") { - Some(extra) => { - if extra.is_empty() { - env!("CARGO_PKG_VERSION").to_owned() - } else { - format!("{} ({})", env!("CARGO_PKG_VERSION"), extra) - } - }, - None => match option_env!("CONDUIT_VERSION_EXTRA") { - Some(extra) => { - if extra.is_empty() { - env!("CARGO_PKG_VERSION").to_owned() - } else { - format!("{} ({})", env!("CARGO_PKG_VERSION"), extra) - } - }, - None => env!("CARGO_PKG_VERSION").to_owned(), - }, - } +pub fn name() -> &'static str { BRANDING } + +#[inline] +pub fn version() -> &'static str { VERSION.get_or_init(init_version) } + +#[inline] +pub fn user_agent() -> &'static str { USER_AGENT.get_or_init(init_user_agent) } + +fn init_user_agent() -> String { format!("{}/{}", name(), version()) } + +fn init_version() -> String { + option_env!("CONDUWUIT_VERSION_EXTRA") + .or(option_env!("CONDUIT_VERSION_EXTRA")) + .map_or(SEMANTIC.to_owned(), |extra| format!("{BRANDING} ({extra})")) } diff --git a/src/main/clap.rs b/src/main/clap.rs index a2fb588e..6ce164f5 100644 --- a/src/main/clap.rs +++ b/src/main/clap.rs @@ -6,7 +6,7 @@ use clap::Parser; /// Commandline arguments #[derive(Parser, Debug)] -#[clap(version = conduit::version::conduwuit(), about, long_about = None)] +#[clap(version = conduit::version(), about, long_about = None)] pub(crate) struct Args { #[arg(short, long)] /// Optional argument to the path of a conduwuit config TOML file diff --git a/src/main/server.rs b/src/main/server.rs index ac93da17..456a8756 100644 --- a/src/main/server.rs +++ b/src/main/server.rs @@ -46,7 +46,7 @@ impl Server { database_path = ?config.database_path, log_levels = %config.log, "{}", - conduit::version::conduwuit(), + conduit::version(), ); Ok(Arc::new(Self { diff --git a/src/service/globals/client.rs b/src/service/globals/client.rs index 7c63618e..1986d7d6 100644 --- a/src/service/globals/client.rs +++ b/src/service/globals/client.rs @@ -87,10 +87,6 @@ impl Client { } fn base(config: &Config) -> Result { - let version = conduit::version::conduwuit(); - - let user_agent = format!("Conduwuit/{version}"); - let mut builder = reqwest::Client::builder() .hickory_dns(true) .connect_timeout(Duration::from_secs(config.request_conn_timeout)) @@ -98,7 +94,7 @@ impl Client { .timeout(Duration::from_secs(config.request_total_timeout)) .pool_idle_timeout(Duration::from_secs(config.request_idle_timeout)) .pool_max_idle_per_host(config.request_idle_per_host.into()) - .user_agent(user_agent) + .user_agent(conduit::version::user_agent()) .redirect(redirect::Policy::limited(6)) .connection_verbose(true);