use super::templates::PercentEncoded;
use crate::config::*;
use crate::network;
use crate::network::net::Net;
use crate::network::responses::Contents;
use windmark::response::Response;
pub fn handler(
cfg: &Config,
net: &Net,
username: PercentEncoded,
repo_name: PercentEncoded,
branch_name: PercentEncoded,
item_path: PercentEncoded,
) -> Result<Response, network::error::Error> {
let repo = &cfg.forge_api().get_repo(net, &username, &repo_name)?;
let kind = cfg.forge_kind.to_string();
let contents =
cfg.forge_api()
.get_contents(net, &username, &repo_name, &branch_name, &item_path)?;
match contents {
Contents::Multiple(contents) => {
let contents_list: Vec<String> = contents
.iter()
.map(|f| super::templates::file_link(&repo, Some(&branch_name), &f))
.collect();
let gemtext_tree_list: String = contents_list
.iter()
.map(|f| format!("{f}\n"))
.collect::<Vec<String>>()
.concat();
let segmented = item_path.uri_component_retaining_sep();
let is_at_branch = segmented.len() <= 1;
let mut segments: Vec<&str> = segmented.split("/").collect();
segments.pop();
let parent_segmented = segments.join("/");
let back = if is_at_branch {
String::new()
} else if segments.is_empty() {
format!(
"=> /{}/{}/src/branch/{} ⮢ ..\n",
username.uri_component(),
repo_name.uri_component(),
branch_name.uri_component()
)
} else {
format!(
"=> /{}/{}/src/branch/{}/{} ⮢ ..\n",
username.uri_component(),
repo_name.uri_component(),
branch_name.uri_component(),
parent_segmented
)
};
let contents_http_url = {
let mut repo_http_url = repo.html_url.clone();
if !repo_http_url.path().ends_with("/") {
repo_http_url.set_path(&format!("{}/", repo_http_url.path()));
}
repo_http_url
.join(&format!(
"src/branch/{}/{}",
branch_name.uri_component(),
segmented
))
.unwrap_or_else(|_| repo.html_url.clone())
};
let contents_http = format!("\n\n=> {contents_http_url} View on {kind}");
let gemtext = format!(
"=> /{}/{} Back to {}/{}
## Tree for branch {}
/{}
{back}{gemtext_tree_list}{contents_http}",
username.uri_component(),
repo_name.uri_component(),
username.display(),
repo_name.display(),
branch_name.display(),
item_path.display(),
);
Ok(Response::success(gemtext))
}
Contents::Single(_file) => {
Ok(Response::temporary_failure(
"This route is not implemented yet",
))
}
}
}