pre-format version strings

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-24 22:06:20 +00:00
parent 7638bbc49c
commit 3a51e18ce6
7 changed files with 32 additions and 32 deletions

View file

@ -145,7 +145,7 @@ pub(crate) async fn syncv3_client_server_json() -> Result<impl IntoResponse> {
Ok(Json(serde_json::json!({ Ok(Json(serde_json::json!({
"server": server_url, "server": server_url,
"version": conduit::version::conduwuit(), "version": conduit::version(),
}))) })))
} }
@ -155,8 +155,8 @@ pub(crate) async fn syncv3_client_server_json() -> Result<impl IntoResponse> {
/// `/_matrix/federation/v1/version` /// `/_matrix/federation/v1/version`
pub(crate) async fn conduwuit_server_version() -> Result<impl IntoResponse> { pub(crate) async fn conduwuit_server_version() -> Result<impl IntoResponse> {
Ok(Json(serde_json::json!({ Ok(Json(serde_json::json!({
"name": "conduwuit", "name": conduit::version::name(),
"version": conduit::version::conduwuit(), "version": conduit::version::version(),
}))) })))
} }

View file

@ -10,8 +10,8 @@ pub(crate) async fn get_server_version_route(
) -> Result<get_server_version::v1::Response> { ) -> Result<get_server_version::v1::Response> {
Ok(get_server_version::v1::Response { Ok(get_server_version::v1::Response {
server: Some(get_server_version::v1::Server { server: Some(get_server_version::v1::Server {
name: Some("Conduwuit".to_owned()), name: Some(conduit::version::name().into()),
version: Some(conduit::version::conduwuit()), version: Some(conduit::version::version().into()),
}), }),
}) })
} }

View file

@ -13,6 +13,7 @@ pub use config::Config;
pub use error::{Error, RumaResponse}; pub use error::{Error, RumaResponse};
pub use pducount::PduCount; pub use pducount::PduCount;
pub use server::Server; pub use server::Server;
pub use version::version;
pub type Result<T, E = Error> = std::result::Result<T, E>; pub type Result<T, E = Error> = std::result::Result<T, E>;

View file

@ -4,25 +4,28 @@
/// Set the environment variable `CONDUWUIT_VERSION_EXTRA` to any UTF-8 string /// 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 /// to include it in parenthesis after the SemVer version. A common value are
/// git commit hashes. /// git commit hashes.
use std::sync::OnceLock;
static BRANDING: &str = "Conduwuit";
static SEMANTIC: &str = env!("CARGO_PKG_VERSION");
static VERSION: OnceLock<String> = OnceLock::new();
static USER_AGENT: OnceLock<String> = OnceLock::new();
#[inline]
#[must_use] #[must_use]
pub fn conduwuit() -> String { pub fn name() -> &'static str { BRANDING }
match option_env!("CONDUWUIT_VERSION_EXTRA") {
Some(extra) => { #[inline]
if extra.is_empty() { pub fn version() -> &'static str { VERSION.get_or_init(init_version) }
env!("CARGO_PKG_VERSION").to_owned()
} else { #[inline]
format!("{} ({})", env!("CARGO_PKG_VERSION"), extra) pub fn user_agent() -> &'static str { USER_AGENT.get_or_init(init_user_agent) }
}
}, fn init_user_agent() -> String { format!("{}/{}", name(), version()) }
None => match option_env!("CONDUIT_VERSION_EXTRA") {
Some(extra) => { fn init_version() -> String {
if extra.is_empty() { option_env!("CONDUWUIT_VERSION_EXTRA")
env!("CARGO_PKG_VERSION").to_owned() .or(option_env!("CONDUIT_VERSION_EXTRA"))
} else { .map_or(SEMANTIC.to_owned(), |extra| format!("{BRANDING} ({extra})"))
format!("{} ({})", env!("CARGO_PKG_VERSION"), extra)
}
},
None => env!("CARGO_PKG_VERSION").to_owned(),
},
}
} }

View file

@ -6,7 +6,7 @@ use clap::Parser;
/// Commandline arguments /// Commandline arguments
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(version = conduit::version::conduwuit(), about, long_about = None)] #[clap(version = conduit::version(), about, long_about = None)]
pub(crate) struct Args { pub(crate) struct Args {
#[arg(short, long)] #[arg(short, long)]
/// Optional argument to the path of a conduwuit config TOML file /// Optional argument to the path of a conduwuit config TOML file

View file

@ -46,7 +46,7 @@ impl Server {
database_path = ?config.database_path, database_path = ?config.database_path,
log_levels = %config.log, log_levels = %config.log,
"{}", "{}",
conduit::version::conduwuit(), conduit::version(),
); );
Ok(Arc::new(Self { Ok(Arc::new(Self {

View file

@ -87,10 +87,6 @@ impl Client {
} }
fn base(config: &Config) -> Result<reqwest::ClientBuilder> { fn base(config: &Config) -> Result<reqwest::ClientBuilder> {
let version = conduit::version::conduwuit();
let user_agent = format!("Conduwuit/{version}");
let mut builder = reqwest::Client::builder() let mut builder = reqwest::Client::builder()
.hickory_dns(true) .hickory_dns(true)
.connect_timeout(Duration::from_secs(config.request_conn_timeout)) .connect_timeout(Duration::from_secs(config.request_conn_timeout))
@ -98,7 +94,7 @@ impl Client {
.timeout(Duration::from_secs(config.request_total_timeout)) .timeout(Duration::from_secs(config.request_total_timeout))
.pool_idle_timeout(Duration::from_secs(config.request_idle_timeout)) .pool_idle_timeout(Duration::from_secs(config.request_idle_timeout))
.pool_max_idle_per_host(config.request_idle_per_host.into()) .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)) .redirect(redirect::Policy::limited(6))
.connection_verbose(true); .connection_verbose(true);