git-gemini-forge 0.1.1

A simple Gemini server that serves a read-only view of public repositories from a Git forge.
pub mod error;
use error::Error;
use url::Url;
use std::env;

const CERTS_DIR_KEY: &'static str = "CERTS_DIR";
const FORGE_TYPE_KEY: &'static str = "PLATFORM_TYPE";
const FORGE_URL_KEY: &'static str = "FORGE_URL";

const CRATE_VERSION: &'static str = env!("CARGO_PKG_VERSION", "Package version unknown");

#[derive(Clone)]
pub struct Config {
	/// The version of this software.
	pub crate_version: &'static str,

	/// The API we should use to communicate with the forge.
	pub forge_type: String,

	/// The URL of the forge.
	pub forge_url: Url,

	/// The directory to look for certificate files.
	pub certs_dir: String,
}

/// Retrieves configuration values from environment variables.
pub fn config() -> Result<Config, Error> {
	// Get the certificates directory
	let default_certs_dir: &str = ".certs";
	let certs_dir: String = env::var(CERTS_DIR_KEY)
		.unwrap_or(String::from(default_certs_dir));

	// Get the API shape identifier we should use
	let default_type: &str = "forgejo";
	let forge_type: String = env::var(FORGE_TYPE_KEY)
		.unwrap_or(String::from(default_type))
		.to_lowercase();

	// Make sure it's one we support
	if forge_type != default_type {
		return Err(Error::BadPlatformType(forge_type));
	}

	// Get the URL of the git forge
	let forge_url_string: String = env::var(FORGE_URL_KEY)
		.unwrap_or(String::from(""));
	if forge_url_string.len() == 0 {
		return Err(Error::MissingUrl);
	}

	match Url::parse(forge_url_string.as_str()) {
		Err(parse_error) => Err(Error::BadUrl(parse_error)),
		Ok(forge_url) => Ok(Config {
			crate_version: CRATE_VERSION,
			forge_type,
			forge_url,
			certs_dir
		}),
	}
}