This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(plugin)] | |
#![plugin(rocket_codegen)] | |
extern crate rocket; | |
#[macro_use] | |
extern crate log; | |
use rocket::request::{Outcome, Request, FromRequest}; | |
use rocket::outcome::Outcome::*; | |
use rocket::response::{NamedFile, Redirect, content}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[get("/lang/<lang>")] | |
fn lang(mut cookies: Cookies, referer: Referer, lang: String) -> Result<Redirect> { | |
let lang:&'static str = Lang::from_str(&lang)?.into(); | |
info!("Setting language to: {}", lang); | |
cookies.add_private(Cookie::new("lang", lang)); | |
Ok(Redirect::to(&referer.url)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Referer { | |
url:String | |
} | |
impl<'a, 'r> FromRequest<'a, 'r> for Referer { | |
type Error = (); | |
fn from_request(request: &'a Request<'r>) -> Outcome<Referer, ()> { | |
match request.headers().get_one("Referer") { | |
Some(r) => Success(Referer { url:r.into() }), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[get("/lang/<lang>")] | |
fn lang(mut cookies: Cookies, lang: String) -> Result<Redirect> { | |
let lang:&'static str = Lang::from_str(&lang)?.into(); | |
info!("Setting language to: {}", lang); | |
cookies.add_private(Cookie::new("lang", lang)); | |
Ok(Redirect::to("/page")) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[get("/page")] | |
fn page(lang: Lang) -> content::HTML<String> { | |
let hello = if lang == Lang::Nl { | |
"Hallo daar!" | |
} else { | |
"Hello there!" | |
}; | |
content::HTML(format!( | |
"<html> | |
<body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl<'a, 'r> FromRequest<'a, 'r> for Lang { | |
type Error = (); | |
fn from_request(request: &'a Request<'r>) -> Outcome<Lang, ()> { | |
match request.cookies().get_private("lang") { | |
Some(r) => { | |
match Lang::from_str(r.value()) { | |
Ok(l) => Success(l), | |
Err(_) => Success(Lang::default()), | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Into<&'static str> for Lang { | |
fn into(self) -> &'static str { | |
match self { | |
Lang::Nl => "nl", | |
Lang::En => "en", | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl FromStr for Lang { | |
type Err = Error; | |
fn from_str(s:&str) -> Result<Self> { | |
match s { | |
"nl" => Ok(Lang::Nl), | |
"en" => Ok(Lang::En), | |
o => Err(format!("Unsupported language: {}", o).into()), | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Default for Lang { | |
fn default() -> Lang { | |
Lang::Nl | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(PartialEq)] | |
enum Lang { | |
Nl, | |
En, | |
} |
NewerOlder