use super::{PercentEncoded, truncate_spaces};
use crate::network::responses::*;
pub fn branch_link(repo: &Repo, branch: &Branch) -> String {
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 {
"๐๏ธ" } else {
"๐" };
format!(
"=> /{}/{}/src/branch/{} {icon} {branch_name}",
username_link.uri_component(),
repo_link.uri_component(),
name_link.uri_component()
)
}
#[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);
}
}