| 1 |
use serde::Deserialize; |
| 2 |
|
| 3 |
use crate::prelude::*; |
| 4 |
|
| 5 |
pub mod about; |
| 6 |
pub mod git; |
| 7 |
pub mod repository; |
| 8 |
pub mod r#static; |
| 9 |
|
| 10 |
const PAGE_SIZE: usize = 50; |
| 11 |
|
| 12 |
#[derive(Clone, Deserialize)] |
| 13 |
pub struct PageQuery { |
| 14 |
#[serde(rename = "h")] |
| 15 |
pub head: Option<String>, |
| 16 |
|
| 17 |
#[serde(rename = "qt")] |
| 18 |
pub query_type: Option<String>, |
| 19 |
|
| 20 |
#[serde(rename = "v")] |
| 21 |
pub value: Option<String>, |
| 22 |
|
| 23 |
pub id: Option<String>, |
| 24 |
|
| 25 |
#[serde(rename = "ofs")] |
| 26 |
pub offset: Option<usize>, |
| 27 |
} |
| 28 |
|
| 29 |
impl PageQuery { |
| 30 |
pub fn id_param(&self) -> String { |
| 31 |
match &self.id { |
| 32 |
Some(id) => format!("?id={id}"), |
| 33 |
None => String::new(), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
pub fn head_param(&self) -> String { |
| 38 |
match (&self.id, &self.head) { |
| 39 |
(None, Some(head)) => format!("?h={head}"), |
| 40 |
_ => String::new(), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub fn id_or_head_param(&self) -> String { |
| 45 |
if let Some(id) = &self.id { |
| 46 |
format!("?id={id}") |
| 47 |
} else if let Some(head) = &self.head { |
| 48 |
format!("?h={head}") |
| 49 |
} else { |
| 50 |
String::new() |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#[derive(Template)] |
| 56 |
#[template(path = "repositorys.html")] |
| 57 |
struct ReposPage { |
| 58 |
repos: Vec<Repository>, |
| 59 |
} |
| 60 |
|
| 61 |
#[get("/")] |
| 62 |
pub async fn page(query: web::Query<PageQuery>, data: web::Data<App>) -> ActixResult<HttpResponse> { |
| 63 |
let rendered = { |
| 64 |
let mut repos: Vec<Repository> = { |
| 65 |
let guard = match data.repos.read() { |
| 66 |
Ok(g) => g, |
| 67 |
Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())), |
| 68 |
}; |
| 69 |
|
| 70 |
match (query.query_type.as_deref(), query.value.as_deref()) { |
| 71 |
(Some(qt), Some(value)) => { |
| 72 |
let value_lower = value.to_lowercase(); |
| 73 |
guard |
| 74 |
.iter() |
| 75 |
.filter(|r| { |
| 76 |
field_for(r, Some(qt)) |
| 77 |
.map(|f| f.to_lowercase().contains(&value_lower)) |
| 78 |
.unwrap_or(false) |
| 79 |
}) |
| 80 |
.cloned() |
| 81 |
.collect() |
| 82 |
} |
| 83 |
_ => guard.clone(), |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
if let (qt, Some(value)) = (query.query_type.as_deref(), query.value.as_deref()) { |
| 88 |
let value_lower = value.to_lowercase(); |
| 89 |
repos.sort_by_key(|a| relevance(a, qt, &value_lower)); |
| 90 |
} |
| 91 |
ReposPage { repos }.render() |
| 92 |
}; |
| 93 |
match rendered { |
| 94 |
Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)), |
| 95 |
Err(e) => Ok(HttpResponse::InternalServerError().body(e.to_string())), |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
fn field_for<'a>(repo: &'a Repository, qt: Option<&str>) -> Option<&'a str> { |
| 100 |
match qt { |
| 101 |
Some("description") => Some(&repo.description), |
| 102 |
_ => Some(&repo.name), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
fn relevance(repo: &Repository, qt: Option<&str>, value_lower: &str) -> u8 { |
| 108 |
match field_for(repo, qt) { |
| 109 |
Some(field) => { |
| 110 |
let field_lower = field.to_lowercase(); |
| 111 |
if field_lower == value_lower { |
| 112 |
0 |
| 113 |
} else if field_lower.starts_with(value_lower) { |
| 114 |
1 |
| 115 |
} else { |
| 116 |
2 |
| 117 |
} |
| 118 |
} |
| 119 |
None => 3, |
| 120 |
} |
| 121 |
} |