git-gemini-forge 0.5.2

A simple Gemini server that serves a read-only view of public repositories from a Git forge.
use super::truncate_spaces;
use crate::network::responses::*;
use urlencoding::encode as encode_uri_component;

/// Creates an internal page link in Gemtext for the given file or directory.
pub fn file_link(repo: &Repo, branch: Option<&Branch>, file: &RepoContents) -> String {
	// Replace space with "_"
	let username_clean: String = truncate_spaces(&repo.owner.login);
	let username_link = encode_uri_component(&username_clean);

	let repo_link = encode_uri_component(&repo.name);

	let name_link = encode_uri_component(&file.name);
	let file_name = truncate_spaces(&file.name);
	let branch_name = match branch {
		Some(branch) => encode_uri_component(&branch.name),
		None => encode_uri_component(&repo.default_branch),
	};

	let icon: &str = match file.kind {
		ContentsType::Submodule => "",
		ContentsType::Symlink => "",
		ContentsType::Dir => "📁",
		ContentsType::File => "📄",
	};

	// TODO: This format matches Forgejo; we may want to support other formats for other forges.
	return format!(
		"=> /{username_link}/{repo_link}/src/branch/{branch_name}/{name_link} {icon} {file_name}"
	);
}