aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/pages/node.rs
blob: c3f77912436c6bf257d448d17a26c5caf941d1ed (plain)
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
use super::layout::Layout;
use crate::{
    frontend::pages::{HtmlTemplate, MyError, MyResult},
    library::{LibDirectory, LibItem, LibNode},
    AppState,
};
use actix_web::{get, web, Responder};
use actix_web_lab::respond::Html;
use log::debug;
use markup::Render;
use std::{ops::Deref, sync::Arc};

#[get("/library/{path:.*}")]
pub async fn page_library_node(
    state: web::Data<AppState>,
    params: web::Path<(String,)>,
) -> MyResult<impl Responder> {
    debug!("request: {:?}", params.0);
    let node = state.library.nested(&params.0)?;
    let mut out = String::new();
    match node.deref() {
        LibNode::Directory(dir) => Layout {
            title: format!(
                "{} - Library",
                dir.path.file_name().unwrap().to_str().unwrap()
            ),
            main: Directory { dir: dir.clone() },
        }
        .render(&mut out)?,
        LibNode::Item(item) => Layout {
            title: "".to_string(),
            main: Item { item: item.clone() },
        }
        .render(&mut out)?,
    };
    Ok(Html(out))
}

markup::define! {
    Directory(dir: Arc<LibDirectory>) {
        h1 { @dir.data.name }
    }
    Item(item: Arc<LibItem>) {
        h1 { "thats an item" }
    }
}