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::{PercentEncoded, truncate_spaces};
use crate::network::responses::*;

/// Creates an internal page link in Gemtext for the given branch.
pub fn branch_link(repo: &Repo, branch: &Branch) -> String {
	// Replace space with "_"
	let username_clean: String = truncate_spaces(&repo.owner.login);
	let username_link = PercentEncoded::new(&username_clean);

	let repo_link = PercentEncoded::new(&repo.name);

	let name_link = PercentEncoded::new(&branch.name);
	let branch_name = truncate_spaces(&branch.name);

	let icon: &str;
	if branch.name == repo.default_branch {
		icon = "๐Ÿ•Š๏ธ"; // dove
	} else {
		icon = "๐ŸŽ‹"; // tanabata tree
	}

	return format!(
		"=> /{}/{}/src/branch/{} {icon} {branch_name}",
		username_link.uri_component(),
		repo_link.uri_component(),
		name_link.uri_component()
	);
}

// MARK: - Tests

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

	fn test_repo() -> Repo {
		let owner = User::new("User");
		Repo {
			default_branch: "main".into(),
			html_url: Url::parse("http://localhost").expect("Valid URL"),
			name: "CamelCaseName".into(),
			description: String::new(),
			website: String::new(),
			owner,
			updated_at: "2025-04-21T04:41:59Z".into(),
			parent: None,
		}
	}

	#[test]
	fn test_creates_a_link_on_main() {
		let repo = test_repo();
		let branch = Branch::new("main");
		let expected = "=> /User/CamelCaseName/src/branch/main ๐Ÿ•Š๏ธ main";
		let link = branch_link(&repo, &branch);
		assert_eq!(link, expected);
	}

	#[test]
	fn test_creates_a_link_on_another_branch() {
		let repo = test_repo();
		let branch = Branch::new("feature");
		let expected = "=> /User/CamelCaseName/src/branch/feature ๐ŸŽ‹ feature";
		let link = branch_link(&repo, &branch);
		assert_eq!(link, expected);
	}

	#[test]
	fn test_creates_a_link_on_main_with_weird_values() {
		let owner = User::new("User");
		let repo = Repo {
			default_branch: "main lmao".into(),
			html_url: Url::parse("http://localhost").expect("Valid URL"),
			name: "this$$$name   is\n=> gemini://mysite.com ลตeird stuff".into(),
			description: String::new(),
			website: String::new(),
			owner,
			updated_at: "2025-04-21T04:41:59Z".into(),
			parent: None,
		};
		let branch = Branch::new("main lmao");
		let expected = "=> /User/this%24%24%24name%20%20%20is%0A%3D%3E%20gemini%3A%2F%2Fmysite.com%20%C5%B5eird%20stuff/src/branch/main%20lmao ๐Ÿ•Š๏ธ main lmao";
		let link = branch_link(&repo, &branch);
		assert_eq!(link, expected);
	}

	#[test]
	fn test_creates_a_link_on_another_branch_with_weird_values() {
		let owner = User::new("User");
		let repo = Repo {
			default_branch: "main lmao".into(),
			html_url: Url::parse("http://localhost").expect("Valid URL"),
			name: "this$$$name   is\n=> gemini://mysite.com ลตeird stuff".into(),
			description: String::new(),
			website: String::new(),
			owner,
			updated_at: "2025-04-21T04:41:59Z".into(),
			parent: None,
		};
		let branch = Branch::new("unmain lmao");
		let expected = "=> /User/this%24%24%24name%20%20%20is%0A%3D%3E%20gemini%3A%2F%2Fmysite.com%20%C5%B5eird%20stuff/src/branch/unmain%20lmao ๐ŸŽ‹ unmain lmao";
		let link = branch_link(&repo, &branch);
		assert_eq!(link, expected);
	}
}