git-gemini-forge 0.6.6

A simple Gemini server that serves a read-only view of public repositories from a Git forge.
use const_str::concat;
use const_str::replace;
use regex_static::lazy_regex;

type Lazy<T> = regex_static::once_cell::sync::Lazy<T>;
pub type Regex = Lazy<regex::Regex>;

/// Regex that matches at least one whitespace character (`\s+`)
pub static WHITESPACE: Regex = lazy_regex!(r"\s+");

/// The name of the project.
pub const PROJECT_NAME: &str = env!("CARGO_PKG_NAME");

/// The version of the project.
pub const PROJECT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// The HTTP URL of the project's git forge.
pub const REPO_HTTP: &str = env!("CARGO_PKG_REPOSITORY");

/// The Gemini URL of the project's git forge.
pub const REPO_GMI: &str = replace!(REPO_HTTP, "https", "gemini");

/// A user-agent string to send to upstream services.
pub static USER_AGENT: &str = const {
	// "x.x.x"
	const REQWEST_VERSION: &str =
		include_str!(core::concat!(env!("OUT_DIR"), "/reqwest_version.txt"));

	// Note whether we're in debug mode
	#[cfg(debug_assertions)]
	const DEBUG: &str = "-debug";
	#[cfg(not(debug_assertions))]
	const DEBUG: &str = "";

	// e.g. "foo/0.0.0 (reqwest/0.0.0; +https://...)"
	// TODO: Add a way to use the production capsule instead, e.g. "+gemini://example.com)"
	concat!(
		PROJECT_NAME,
		"/",
		PROJECT_VERSION,
		DEBUG,
		" (reqwest/",
		REQWEST_VERSION,
		"; +",
		REPO_HTTP,
		")"
	)
};

// MARK: - Tests

#[cfg(test)]
mod tests {
	use super::*;

	// e.g. git-gemini-forge/0.6.0 (reqwest/1.7.2)
	static UA_REGEX: Regex = lazy_regex!(
		r"^git-gemini-forge/\d+\.\d+\.\d+(\-debug)? \(reqwest/\d+\.\d+\.\d+; \+https://[\w\./\-]+\)$"
	);

	#[test]
	fn test_user_agent_string_is_reasonable() {
		let actual = USER_AGENT;
		assert!(UA_REGEX.is_match(actual), "Unexpected UA: {actual:?}");
		assert!(actual.contains(REPO_HTTP));
	}
}