diff --git a/src/main.rs b/src/main.rs index 17b4d62..f8ac979 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,13 @@ #![warn(clippy::pedantic)] #![allow(clippy::needless_pass_by_value, clippy::no_effect_underscore_binding)] +mod trivial; + use askama::Template; use reqwest::{Client, StatusCode, Url}; -use rocket::form::{Form, FromForm}; use rocket::http::{ContentType, Header, Status}; -use rocket::response::{self, status::NoContent, Debug, Redirect, Responder}; -use rocket::{get, post, routes, uri, Request, Response, State}; +use rocket::response::{self, Debug, Responder}; +use rocket::{get, routes, Request, Response, State}; use serde::Deserialize; use std::io::Cursor; use std::str::FromStr; @@ -26,14 +27,14 @@ fn rocket() -> _ { rocket::build().manage(client).mount( "/", routes![ - code, - copy_js, - css, - favicon_ico, - index, instance, - instance_form, - robots_txt + trivial::code, + trivial::copy_js, + trivial::css, + trivial::favicon_ico, + trivial::index, + trivial::instance_form, + trivial::robots_txt, ], ) } @@ -51,31 +52,6 @@ impl Responder<'_, 'static> for Html { } } -#[derive(Template)] -#[template(path = "index.html")] -struct Index; - -#[get("/")] -fn index() -> Html { - Html(Index) -} - -#[derive(FromForm)] -struct InstanceForm<'a> { - instance: &'a str, - show_all: bool, - show_animated: bool, -} - -#[post("/", data = "
")] -fn instance_form(form: Form>) -> Redirect { - Redirect::to(uri!(instance( - form.instance, - form.show_all.then_some(true), - form.show_animated.then_some(true), - ))) -} - #[get("/?&")] async fn instance( client: &State, @@ -147,42 +123,3 @@ async fn instance( .await?; Ok((ContentType::HTML, output)) } - -#[derive(Responder)] -struct Code { - zip: &'static [u8], - content_type: ContentType, - disposition: Header<'static>, -} - -#[get("/code")] -fn code() -> Code { - Code { - zip: include_bytes!(concat!(env!("OUT_DIR"), "/source.zip")), - content_type: ContentType::ZIP, - disposition: Header::new( - "content-disposition", - r#"attachment; filename="emojos.in.zip""#, - ), - } -} - -#[get("/static/site.css")] -fn css() -> (ContentType, &'static str) { - (ContentType::CSS, include_str!("site.css")) -} - -#[get("/static/copy.js")] -fn copy_js() -> (ContentType, &'static str) { - (ContentType::JavaScript, include_str!("copy.js")) -} - -#[get("/favicon.ico")] -fn favicon_ico() -> NoContent { - NoContent -} - -#[get("/robots.txt")] -fn robots_txt() -> NoContent { - NoContent -} diff --git a/src/trivial.rs b/src/trivial.rs new file mode 100644 index 0000000..56eb28a --- /dev/null +++ b/src/trivial.rs @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +use crate::Html; +use askama::Template; +use rocket::form::{Form, FromForm}; +use rocket::http::{Header, Status}; +use rocket::response::content::{RawCss, RawJavaScript}; +use rocket::response::{status::NoContent, Redirect, Responder}; +use rocket::{get, post, uri}; + +#[derive(Template)] +#[template(path = "index.html")] +pub(crate) struct Index; + +#[get("/")] +pub(crate) fn index() -> Html { + Html(Index) +} + +#[derive(FromForm)] +pub(crate) struct InstanceForm<'a> { + instance: &'a str, + show_all: bool, + show_animated: bool, +} + +#[post("/", data = "")] +pub(crate) fn instance_form(form: Form>) -> Redirect { + Redirect::to(uri!(crate::instance( + form.instance, + form.show_all.then_some(true), + form.show_animated.then_some(true), + ))) +} + +#[derive(Responder)] +#[response(content_type = "application/zip")] +pub(crate) struct Code { + zip: &'static [u8], + disposition: Header<'static>, +} + +#[get("/code")] +pub(crate) fn code() -> Code { + Code { + zip: include_bytes!(concat!(env!("OUT_DIR"), "/source.zip")), + disposition: Header::new( + "content-disposition", + r#"attachment; filename="emojos.in.zip""#, + ), + } +} + +#[get("/static/site.css")] +pub(crate) fn css() -> RawCss<&'static [u8]> { + RawCss(include_bytes!("site.css")) +} + +#[get("/static/copy.js")] +pub(crate) fn copy_js() -> RawJavaScript<&'static [u8]> { + RawJavaScript(include_bytes!("copy.js")) +} + +#[get("/favicon.ico")] +pub(crate) fn favicon_ico() -> Status { + Status::NotFound +} + +#[get("/robots.txt")] +pub(crate) fn robots_txt() -> NoContent { + NoContent +}