M Cargo.toml => Cargo.toml +1 -0
@@ 7,6 7,7 @@ version = "0.1.0"
edition = "2018"
[dependencies]
+percent-encoding = "2.1.0"
rand = "0.7.0"
rocket = "0.4.2"
M src/main.rs => src/main.rs +18 -13
@@ 9,6 9,7 @@ use std::{
path::{Path, PathBuf},
};
+use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use rocket::{
get,
response::{NamedFile, Redirect},
@@ 18,12 19,20 @@ use rocket_contrib::templates::Template;
use self::{random::random_type, reverse_turbofish::ReverseTurboFish, turbofish::TurboFish};
-#[get("/")]
-fn index() -> Template {
+fn tpl_context(guts: &str) -> HashMap<&'static str, String> {
let mut context = HashMap::new();
- context.insert("guts", "");
+ context.insert("guts", guts.replace("<", "<"));
+ context.insert(
+ "guts_link",
+ utf8_percent_encode(guts, NON_ALPHANUMERIC).to_string(),
+ );
- Template::render("turbofish", context)
+ context
+}
+
+#[get("/")]
+fn index() -> Template {
+ Template::render("turbofish", tpl_context(""))
}
#[get("/random")]
@@ 33,23 42,19 @@ fn random() -> Redirect {
#[get("/random_reverse")]
fn random_reverse() -> Redirect {
- Redirect::to(uri!(reverse_turbofish: ReverseTurboFish::new(random_type())))
+ Redirect::to(uri!(
+ reverse_turbofish: ReverseTurboFish::new(random_type())
+ ))
}
#[get("/<turbofish>", rank = 1)]
fn turbofish(turbofish: TurboFish) -> Template {
- let mut context = HashMap::new();
- context.insert("guts", turbofish.gut());
-
- Template::render("turbofish", context)
+ Template::render("turbofish", tpl_context(&turbofish.gut()))
}
#[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)
+ Template::render("reverse_turbofish", tpl_context(&reverse_turbofish.gut()))
}
// From https://github.com/SergioBenitez/Rocket/blob/master/examples/static_files/src/main.rs
M src/reverse_turbofish.rs => src/reverse_turbofish.rs +1 -1
@@ 30,7 30,7 @@ impl<'a> FromParam<'a> for ReverseTurboFish {
let (mid, back) = rest.split_at(rest.len() - 3);
if front == "<" && back == ">::" {
- Ok(ReverseTurboFish(mid.replace("<", "<")))
+ Ok(ReverseTurboFish(mid.to_owned()))
} else {
Err(param)
}
M src/turbofish.rs => src/turbofish.rs +1 -1
@@ 30,7 30,7 @@ impl<'a> FromParam<'a> for TurboFish {
let (mid, front) = rest.split_at(rest.len() - 1);
if back == "::<" && front == ">" {
- Ok(TurboFish(mid.replace("<", "<")))
+ Ok(TurboFish(mid.to_owned()))
} else {
Err(param)
}
M templates/reverse_turbofish.tera => templates/reverse_turbofish.tera +1 -1
@@ 26,7 26,7 @@
<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_reverse">random!</a>
- <a href="/::<{{ guts }}>">reverse!</a>
+ <a href="/::<{{ guts_link }}>">reverse!</a>
</footer>
</html>
M templates/turbofish.tera => templates/turbofish.tera +1 -1
@@ 27,7 27,7 @@
<a href="https://matematikaadit.github.io/posts/rust-turbofish.html">what??</a>
<a href="https://github.com/jplatte/turbo.fish">code.</a>
<a href="/random">random!</a>
- <a href="/<{{ guts }}>::">reverse!</a>
+ <a href="/<{{ guts_link }}>::">reverse!</a>
</footer>
</html>