From ba4d782687b5eb8d91fd881a7cb9d0adce7dd9f0 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 13 Feb 2023 18:00:27 +0100 Subject: licences are boring, lets show the source code instead. --- src/source.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/source.rs (limited to 'src/source.rs') diff --git a/src/source.rs b/src/source.rs new file mode 100644 index 0000000..ef76512 --- /dev/null +++ b/src/source.rs @@ -0,0 +1,81 @@ +use rocket::{ + get, + http::Header, + response::{self, Responder}, + Request, State, +}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +use crate::layout::{DynLayoutPage, LayoutPage}; + +pub struct Reload(pub T); + +#[rocket::async_trait] +impl<'r, T: Responder<'r, 'static>> Responder<'r, 'static> for Reload { + 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); + +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::>() + .join("\n") + .split("\n") + .map(String::from) + .collect(), + ) +} + +#[get("/source")] +pub async fn r_source(text: &State) -> Reload { + 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(LayoutPage { + title: "Source".to_string(), + content: markup::new! { pre { @out } }, + }) +} -- cgit v1.2.3-70-g09d2