git-gemini-forge 0.6.2

A simple Gemini server that serves a read-only view of public repositories from a Git forge.
use super::converters::gitlab_project_id;
use crate::handlers::templates::PercentEncoded;
use crate::network::error;
use crate::network::net::Net;
use crate::network::responses::*;
use crate::network::{ForgeApi, ForgeApiKind};
use isahc::http::HeaderName;
use secrecy::SecretString;
use url::Url;

/// Describes the GitLab API.
///
/// See https://docs.gitlab.com/ee/api/api_resources.html
#[derive(Clone, Debug)]
pub struct GitLabApi {
	_base_url: Url,
	_kind: ForgeApiKind,
	_private_token: Option<SecretString>,
}

impl GitLabApi {
	/// Constructs an API from the given forge URL.
	pub fn from_url(
		forge_url: Url,
		kind: ForgeApiKind,
		private_token: &Option<SecretString>,
	) -> Self {
		let base_url: Url = forge_url.join("/api/v4/").expect("Valid URL path");
		GitLabApi {
			_base_url: base_url,
			_kind: kind,
			_private_token: private_token.clone(),
		}
	}
}

impl ForgeApi for GitLabApi {
	fn kind(&self) -> ForgeApiKind {
		self._kind
	}

	fn access_token_header_name(&self) -> HeaderName {
		HeaderName::from_static("Private-Token")
	}

	fn access_token_value(&self) -> Option<SecretString> {
		match &self._private_token {
			None => None,
			Some(token) => Some(token.clone()),
		}
	}

	fn check_access_token(&self, net: &Net) -> Result<(), error::Error> {
		match self.access_token_value() {
			None => Ok(()),
			Some(_) => super::get_authed_user(self, net).map(|_| ()),
		}
	}

	fn base_url(&self) -> &Url {
		&self._base_url
	}

	fn get_branches(
		&self,
		net: &Net,
		username: &PercentEncoded,
		project_pathname: &PercentEncoded,
	) -> Result<Vec<Branch>, error::Error> {
		let project_id = gitlab_project_id(username, project_pathname);
		super::get_branches(self, net, &project_id)
	}

	fn get_contents(
		&self,
		net: &Net,
		username: &PercentEncoded,
		project_pathname: &PercentEncoded,
		branch_name: &PercentEncoded,
		item_path: &PercentEncoded,
	) -> Result<Contents<Item>, error::Error> {
		let project_id = gitlab_project_id(username, project_pathname);
		super::get_contents(self, net, &project_id, &branch_name, &item_path).map(Into::into)
	}

	fn get_file_tree(
		&self,
		net: &Net,
		username: &PercentEncoded,
		project_pathname: &PercentEncoded,
	) -> Result<Vec<Item>, error::Error> {
		let project_id = gitlab_project_id(username, project_pathname);
		super::get_file_tree(self, net, &project_id)
			.map(|tree| tree.iter().map(Into::into).collect())
	}

	fn get_recent_repos(&self, net: &Net) -> Result<Vec<Repo>, error::Error> {
		super::get_recent_repos(self, net).map(|projects| {
			projects
				.iter()
				.filter(|p| p.default_branch.is_some()) // ignore empty projects
				.map(Into::into)
				.collect()
		})
	}

	fn get_repo(
		&self,
		net: &Net,
		username: &PercentEncoded,
		project_pathname: &PercentEncoded,
	) -> Result<Repo, error::Error> {
		let project_id = gitlab_project_id(username, project_pathname);
		super::get_repo(self, net, &project_id).map(Into::into)
	}

	fn get_user_repos(
		&self,
		net: &Net,
		username: &PercentEncoded,
	) -> Result<Vec<Repo>, error::Error> {
		super::get_user_repos(self, net, username)
			.map(|projects| projects.iter().map(Into::into).collect())
	}

	fn get_users(&self, net: &Net) -> Result<Vec<User>, error::Error> {
		super::get_users(self, net).map(|users| users.iter().map(Into::into).collect())
	}

	fn get_version(&self, net: &Net) -> Result<ServerVersion, error::Error> {
		super::get_version(self, net).map(Into::into)
	}
}