~/selfhut

selfhut/src/index.rs -rw-r--r-- 1.44 kB
ef55c0da — arthurmelton summary 2 years ago

            
0545f2a7 Arthur Melton
ad9e5164 arthurmelton
94e2262d arthurmelton
ef55c0da arthurmelton
0545f2a7 Arthur Melton
ad9e5164 arthurmelton
0545f2a7 Arthur Melton
ad9e5164 arthurmelton
94e2262d arthurmelton
ad9e5164 arthurmelton
0016c2cc arthurmelton
ad9e5164 arthurmelton
94e2262d arthurmelton
0016c2cc arthurmelton
ad9e5164 arthurmelton
94e2262d arthurmelton
ad9e5164 arthurmelton
0545f2a7 Arthur Melton
ad9e5164 arthurmelton
ef55c0da arthurmelton
ad9e5164 arthurmelton
0016c2cc arthurmelton
ad9e5164 arthurmelton
0545f2a7 Arthur Melton
























1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::config::CONFIG;
use crate::git::repos::get_repos;
use crate::git::repos::Repo;
use crate::utils::markdown::md_to_html;

use rocket_dyn_templates::{context, Template};

const PAGE_REPO_COUNT: usize = 10;

#[get("/?<page>&<search>")]
pub fn index(page: Option<usize>, search: Option<String>) -> Option<Template> {
    let mut repos = get_repos()?;
    let page = page.unwrap_or(1);
    if search.is_some() {
        repos = repos
            .iter()
            .filter(|repo| {
                repo.name.contains(&*search.as_ref().unwrap())
                    || repo.description.contains(&*search.as_ref().unwrap())
            })
            .map(|repo| repo.clone())
            .collect::<Vec<Repo>>();
    }
    let total_page = if repos.len() > 0 {
        (repos.len() - 1) / PAGE_REPO_COUNT + 1
    } else {
        1
    };
    let last_item = if repos.len() > page * PAGE_REPO_COUNT {
        page * PAGE_REPO_COUNT
    } else {
        repos.len()
    };
    Some(Template::render(
        "index",
        context! {
            title: "Me",
            user: CONFIG.name.clone(),
            description: md_to_html(&CONFIG.description.clone()),
            repos: repos[(page-1)*PAGE_REPO_COUNT..last_item].to_vec(),
            search: search.unwrap_or("".to_string()),
            page,
            total_page,
            page_inc: page+1,
            page_dec: if page > 1 {page-1} else {page}
        },
    ))
}