~jplatte/turbo.fish

29af5b23b82c1560cf4b3f2526999e025568ede6 — Jonas Platte 1 year, 2 months ago 425e1c2
Support reverse turbofishes
4 files changed, 116 insertions(+), 3 deletions(-)

M src/main.rs
A src/reverse_turbofish.rs
M static/turbofish.css
A templates/reverse_turbofish.tera
M src/main.rs => src/main.rs +15 -3
@@ 1,6 1,7 @@
#![feature(proc_macro_hygiene, decl_macro)]

mod random;
mod reverse_turbofish;
mod turbofish;

use std::{


@@ 15,7 16,7 @@ use rocket::{
};
use rocket_contrib::templates::Template;

use self::{random::random_type, turbofish::TurboFish};
use self::{random::random_type, reverse_turbofish::ReverseTurboFish, turbofish::TurboFish};

#[get("/")]
fn index() -> Template {


@@ 30,7 31,7 @@ fn random() -> Redirect {
    Redirect::to(uri!(turbofish: TurboFish::new(random_type())))
}

#[get("/<turbofish>")]
#[get("/<turbofish>", rank = 1)]
fn turbofish(turbofish: TurboFish) -> Template {
    let mut context = HashMap::new();
    context.insert("guts", turbofish.gut());


@@ 38,6 39,14 @@ fn turbofish(turbofish: TurboFish) -> Template {
    Template::render("turbofish", context)
}

#[get("/<reverse_turbofish>", rank = 2)]
fn reverse_turbofish(reverse_turbofish: ReverseTurboFish) -> Template {
    let mut context = HashMap::new();
    context.insert("guts", reverse_turbofish.gut());

    Template::render("reverse_turbofish", context)
}

// From https://github.com/SergioBenitez/Rocket/blob/master/examples/static_files/src/main.rs
#[get("/<file..>", rank = 100)]
fn files(file: PathBuf) -> Option<NamedFile> {


@@ 47,7 56,10 @@ fn files(file: PathBuf) -> Option<NamedFile> {
fn main() {
    // TODO: Custom 404
    rocket::ignite()
        .mount("/", routes![index, random, turbofish, files])
        .mount(
            "/",
            routes![index, random, turbofish, reverse_turbofish, files],
        )
        .attach(Template::fairing())
        .launch();
}

A src/reverse_turbofish.rs => src/reverse_turbofish.rs +46 -0
@@ 0,0 1,46 @@
use std::fmt;

use rocket::{
    http::{
        impl_from_uri_param_identity,
        uri::{Formatter, Path, UriDisplay},
        RawStr,
    },
    request::FromParam,
};

pub struct ReverseTurboFish(String);

impl ReverseTurboFish {
    pub fn new(guts: String) -> ReverseTurboFish {
        ReverseTurboFish(guts)
    }

    pub fn gut(self) -> String {
        self.0
    }
}

impl<'a> FromParam<'a> for ReverseTurboFish {
    type Error = &'a RawStr;

    fn from_param(param: &'a RawStr) -> Result<Self, Self::Error> {
        let param_cow = param.percent_decode().map_err(|_| param)?;
        let (front, rest) = param_cow.split_at(1);
        let (mid, back) = rest.split_at(rest.len() - 3);

        if front == "<" && back == ">::" {
            Ok(ReverseTurboFish(mid.replace("<", "<​")))
        } else {
            Err(param)
        }
    }
}

impl UriDisplay<Path> for ReverseTurboFish {
    fn fmt(&self, f: &mut Formatter<Path>) -> fmt::Result {
        f.write_value(&format!("<{}>::", self.0))
    }
}

impl_from_uri_param_identity!([Path] ReverseTurboFish);

M static/turbofish.css => static/turbofish.css +24 -0
@@ 120,3 120,27 @@ h::after {
    from { right: 100vw }
    to { right: -100vw }
}

.reverse t,
.reverse u,
.reverse r,
.reverse b,
.reverse o,
.reverse f,
.reverse i,
.reverse s,
.reverse h {
    animation-direction: reverse;
}

.reverse t::before, .reverse t::after,
.reverse u::before, .reverse u::after,
.reverse r::before, .reverse r::after,
.reverse b::before, .reverse b::after,
.reverse o::before, .reverse o::after,
.reverse f::before, .reverse f::after,
.reverse i::before, .reverse i::after,
.reverse s::before, .reverse s::after,
.reverse h::before, .reverse h::after {
    order: 1;
}

A templates/reverse_turbofish.tera => templates/reverse_turbofish.tera +31 -0
@@ 0,0 1,31 @@
<!doctype html>
<html lang="en" class="reverse">

<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'none'; frame-src 'none'">

<title><​{{ guts }}>::</title>

<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="turbofish.css">

<t><​{{ guts }}></t>
<u><​{{ guts }}></u>
<r><​{{ guts }}></r>
<b><​{{ guts }}></b>
<o><​{{ guts }}></o>
<f><​{{ guts }}></f>
<i><​{{ guts }}></i>
<s><​{{ guts }}></s>
<h><​{{ guts }}></h>

<footer>
    <a href="https://www.reddit.com/r/rust/comments/cpknjb/i_just_found_out_that_method_is_valid/ewq261j/">what?</a>
    <a href="https://github.com/jplatte/turbo.fish">code.</a>
    <a href="/random">random!</a>
</footer>

</html>