summaryrefslogtreecommitdiff
path: root/src/source.rs
blob: 04ec929d5354dfe69c5f149b686c44def95760d3 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::layout::{DynScaffold, Scaffold};
use rocket::{
    get,
    http::Header,
    response::{self, Responder},
    Request, State,
};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub struct Reload<T>(pub T);

#[rocket::async_trait]
impl<'r, T: Responder<'r, 'static>> Responder<'r, 'static> for Reload<T> {
    fn respond_to(self, request: &'r Request<'_>) -> response::Result<'static> {
        let mut resp = self.0.respond_to(request);
        if let Ok(resp) = &mut resp {
            resp.set_header(Header::new("refresh", "0"));
        }
        resp
    }
}

const SOURCE_DIR: include_dir::Dir = include_dir::include_dir!("$CARGO_MANIFEST_DIR/src");

pub struct SourceWrap(Vec<String>);

pub fn prepare_source() -> SourceWrap {
    SourceWrap(
        SOURCE_DIR
            .find("**/*.rs")
            .unwrap()
            .map(|f| {
                format!(
                    "
======================================
    Contents of {:?}
======================================
{}
",
                    f.path(),
                    f.as_file().unwrap().contents_utf8().unwrap()
                )
            })
            .collect::<Vec<_>>()
            .join("\n")
            .split("\n")
            .map(String::from)
            .collect(),
    )
}

#[get("/source")]
pub async fn r_source(text: &State<SourceWrap>) -> Reload<DynScaffold> {
    let mspf = 100u128;

    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis();

    let frame = ts / mspf;
    let frame_off = ts % mspf;

    tokio::time::sleep(Duration::from_millis((mspf - frame_off) as u64)).await;

    let mut out = String::new();
    out += &format!(
        "About {} milliseconds have passed since midnight of the january the first in 1970.\n",
        ts
    );
    out += "------------------------------------------------------\n";
    for i in frame..frame + 30 {
        out += &text.0[(i % text.0.len() as u128) as usize];
        out += "\n";
    }
    Reload(Scaffold {
        title: "Source".to_string(),
        content: markup::new! { pre { @out } },
    })
}