Skip to content

Commit

Permalink
Migrate all examples to Rust 2018.
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrosen authored and SergioBenitez committed Jun 25, 2019
1 parent 2315171 commit d9f989a
Show file tree
Hide file tree
Showing 70 changed files with 103 additions and 95 deletions.
1 change: 1 addition & 0 deletions examples/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "config"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
2 changes: 0 additions & 2 deletions examples/config/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate rocket;

// This example's illustration is the Rocket.toml file.
fn main() {
rocket::ignite().launch();
Expand Down
6 changes: 3 additions & 3 deletions examples/config/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use rocket::local::Client;
struct LocalConfig(Config);

#[get("/check_config")]
fn check_config(config: State<LocalConfig>) -> Option<()> {
let environment = match ::std::env::var("ROCKET_ENV") {
fn check_config(config: State<'_, LocalConfig>) -> Option<()> {
let environment = match std::env::var("ROCKET_ENV") {
Ok(name) => name,
Err(_) => return None
};
Expand Down Expand Up @@ -52,7 +52,7 @@ fn check_config(config: State<LocalConfig>) -> Option<()> {
pub fn test_config(environment: Environment) {
// Manually set the config environment variable. Rocket will initialize the
// environment in `ignite()`. We'll read this back in the handler to config.
::std::env::set_var("ROCKET_ENV", environment.to_string());
std::env::set_var("ROCKET_ENV", environment.to_string());

let rocket = rocket::ignite()
.attach(AdHoc::on_attach("Local Config", |rocket| {
Expand Down
1 change: 1 addition & 0 deletions examples/content_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "content_types"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
3 changes: 1 addition & 2 deletions examples/content_types/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
extern crate serde_json;

#[cfg(test)] mod tests;

Expand Down Expand Up @@ -42,7 +41,7 @@ fn post_hello(age: u8, name_data: Data) -> io::Result<content::Json<String>> {
}

#[catch(404)]
fn not_found(request: &Request) -> content::Html<String> {
fn not_found(request: &Request<'_>) -> content::Html<String> {
let html = match request.format() {
Some(ref mt) if !mt.is_json() && !mt.is_plain() => {
format!("<p>'{}' requests are not supported.</p>", mt)
Expand Down
2 changes: 0 additions & 2 deletions examples/content_types/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use super::rocket;
use super::serde_json;
use super::Person;
use rocket::http::{Accept, ContentType, Header, MediaType, Method, Status};
use rocket::local::Client;
Expand Down
1 change: 1 addition & 0 deletions examples/cookies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "cookies"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
5 changes: 2 additions & 3 deletions examples/cookies/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
extern crate rocket_contrib;

#[cfg(test)]
mod tests;
Expand All @@ -19,13 +18,13 @@ struct Message {
}

#[post("/submit", data = "<message>")]
fn submit(mut cookies: Cookies, message: Form<Message>) -> Redirect {
fn submit(mut cookies: Cookies<'_>, message: Form<Message>) -> Redirect {
cookies.add(Cookie::new("message", message.into_inner().message));
Redirect::to("/")
}

#[get("/")]
fn index(cookies: Cookies) -> Template {
fn index(cookies: Cookies<'_>) -> Template {
let cookie = cookies.get("message");
let mut context = HashMap::new();
if let Some(ref cookie) = cookie {
Expand Down
1 change: 1 addition & 0 deletions examples/errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "errors"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/errors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn hello(name: String, age: i8) -> String {
}

#[catch(404)]
fn not_found(req: &rocket::Request) -> content::Html<String> {
fn not_found(req: &rocket::Request<'_>) -> content::Html<String> {
content::Html(format!("<p>Sorry, but '{}' is not a valid path!</p>
<p>Try visiting /hello/&lt;name&gt;/&lt;age&gt; instead.</p>",
req.uri()))
Expand Down
1 change: 0 additions & 1 deletion examples/errors/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::rocket;
use rocket::local::Client;
use rocket::http::Status;

Expand Down
1 change: 1 addition & 0 deletions examples/fairings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "fairings"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions examples/fairings/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl Fairing for Counter {
}
}

fn on_request(&self, request: &mut Request, _: &Data) {
fn on_request(&self, request: &mut Request<'_>, _: &Data) {
if request.method() == Method::Get {
self.get.fetch_add(1, Ordering::Relaxed);
} else if request.method() == Method::Post {
self.post.fetch_add(1, Ordering::Relaxed);
}
}

fn on_response(&self, request: &Request, response: &mut Response) {
fn on_response(&self, request: &Request<'_>, response: &mut Response<'_>) {
if response.status() != Status::NotFound {
return
}
Expand All @@ -58,7 +58,7 @@ fn hello() -> &'static str {
}

#[get("/token")]
fn token(token: State<Token>) -> String {
fn token(token: State<'_, Token>) -> String {
format!("{}", token.0)
}

Expand Down
1 change: 1 addition & 0 deletions examples/form_kitchen_sink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "form_kitchen_sink"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/form_kitchen_sink/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct FormInput<'r> {
}

#[post("/", data = "<sink>")]
fn sink(sink: Result<Form<FormInput>, FormError>) -> String {
fn sink(sink: Result<Form<FormInput<'_>>, FormError<'_>>) -> String {
match sink {
Ok(form) => format!("{:?}", &*form),
Err(FormDataError::Io(_)) => format!("Form input was invalid UTF-8."),
Expand Down
8 changes: 4 additions & 4 deletions examples/form_kitchen_sink/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rocket::local::Client;
use rocket::http::ContentType;

impl fmt::Display for FormOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
FormOption::A => write!(f, "a"),
FormOption::B => write!(f, "b"),
Expand All @@ -23,14 +23,14 @@ fn assert_form_eq(client: &Client, form_str: &str, expected: String) {
assert_eq!(res.body_string(), Some(expected));
}

fn assert_valid_form(client: &Client, input: &FormInput) {
fn assert_valid_form(client: &Client, input: &FormInput<'_>) {
let f = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
input.checkbox, input.number, input.radio, input.password,
input.text_area, input.select);
assert_form_eq(client, &f, format!("{:?}", input));
}

fn assert_valid_raw_form(client: &Client, form_str: &str, input: &FormInput) {
fn assert_valid_raw_form(client: &Client, form_str: &str, input: &FormInput<'_>) {
assert_form_eq(client, form_str, format!("{:?}", input));
}

Expand Down Expand Up @@ -176,7 +176,7 @@ fn check_structurally_invalid_forms() {
fn check_bad_utf8() {
let client = Client::new(rocket()).unwrap();
unsafe {
let bad_str = ::std::str::from_utf8_unchecked(b"a=\xff");
let bad_str = std::str::from_utf8_unchecked(b"a=\xff");
assert_form_eq(&client, bad_str, "Form input was invalid UTF-8.".into());
}
}
1 change: 1 addition & 0 deletions examples/form_validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "form_validation"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/form_validation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'v> FromFormValue<'v> for AdultAge {
}

#[post("/login", data = "<user>")]
fn login(user: Form<UserLogin>) -> Result<Redirect, String> {
fn login(user: Form<UserLogin<'_>>) -> Result<Redirect, String> {
if let Err(e) = user.age {
return Err(format!("Age is invalid: {}", e));
}
Expand Down
1 change: 1 addition & 0 deletions examples/handlebars_templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "handlebars_templates"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
13 changes: 6 additions & 7 deletions examples/handlebars_templates/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
extern crate rocket_contrib;

#[cfg(test)] mod tests;

use rocket::Request;
use rocket::response::Redirect;
use rocket_contrib::templates::{Template, handlebars};

use handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender};

#[derive(Serialize)]
struct TemplateContext {
title: &'static str,
Expand Down Expand Up @@ -47,18 +44,20 @@ fn about() -> Template {
}

#[catch(404)]
fn not_found(req: &Request) -> Template {
fn not_found(req: &Request<'_>) -> Template {
let mut map = std::collections::HashMap::new();
map.insert("path", req.uri().path());
Template::render("error/404", &map)
}

use self::handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender};

fn wow_helper(
h: &Helper,
h: &Helper<'_, '_>,
_: &Handlebars,
_: &Context,
_: &mut RenderContext,
out: &mut Output
_: &mut RenderContext<'_>,
out: &mut dyn Output
) -> HelperResult {
if let Some(param) = h.param(0) {
out.write("<b><i>")?;
Expand Down
12 changes: 6 additions & 6 deletions examples/handlebars_templates/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! dispatch {
fn test_root() {
// Check that the redirect works.
for method in &[Get, Head] {
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse| {
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse<'_>| {
assert_eq!(response.status(), Status::SeeOther);
assert!(response.body().is_none());

Expand All @@ -27,8 +27,8 @@ fn test_root() {

// Check that other request methods are not accepted (and instead caught).
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse| {
let mut map = ::std::collections::HashMap::new();
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse<'_>| {
let mut map = std::collections::HashMap::new();
map.insert("path", "/");
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();

Expand All @@ -41,7 +41,7 @@ fn test_root() {
#[test]
fn test_name() {
// Check that the /hello/<name> route works.
dispatch!(Get, "/hello/Jack%20Daniels", |client: &Client, mut response: LocalResponse| {
dispatch!(Get, "/hello/Jack%20Daniels", |client: &Client, mut response: LocalResponse<'_>| {
let context = TemplateContext {
title: "Hello",
name: Some("Jack Daniels".into()),
Expand All @@ -58,8 +58,8 @@ fn test_name() {
#[test]
fn test_404() {
// Check that the error catcher works.
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse| {
let mut map = ::std::collections::HashMap::new();
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse<'_>| {
let mut map = std::collections::HashMap::new();
map.insert("path", "/hello/");

let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_2018/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "hello_2018"
version = "0.0.0"
workspace = "../../"
publish = false
edition = "2018"
publish = false

[dependencies]
rocket = { path = "../../core/lib" }
1 change: 1 addition & 0 deletions examples/hello_person/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "hello_person"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
1 change: 0 additions & 1 deletion examples/hello_person/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::rocket;
use rocket::local::Client;
use rocket::http::Status;

Expand Down
1 change: 1 addition & 0 deletions examples/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "hello_world"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
1 change: 0 additions & 1 deletion examples/hello_world/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::rocket;
use rocket::local::Client;

#[test]
Expand Down
1 change: 1 addition & 0 deletions examples/json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "json"
version = "0.0.0"
workspace = "../../"
edition = "2018"
publish = false

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions examples/json/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Message {

// TODO: This example can be improved by using `route` with multiple HTTP verbs.
#[post("/<id>", format = "json", data = "<message>")]
fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> JsonValue {
fn new(id: ID, message: Json<Message>, map: State<'_, MessageMap>) -> JsonValue {
let mut hashmap = map.lock().expect("map lock.");
if hashmap.contains_key(&id) {
json!({
Expand All @@ -40,7 +40,7 @@ fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> JsonValue {
}

#[put("/<id>", format = "json", data = "<message>")]
fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<JsonValue> {
fn update(id: ID, message: Json<Message>, map: State<'_, MessageMap>) -> Option<JsonValue> {
let mut hashmap = map.lock().unwrap();
if hashmap.contains_key(&id) {
hashmap.insert(id, message.0.contents);
Expand All @@ -51,7 +51,7 @@ fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<Json
}

#[get("/<id>", format = "json")]
fn get(id: ID, map: State<MessageMap>) -> Option<Json<Message>> {
fn get(id: ID, map: State<'_, MessageMap>) -> Option<Json<Message>> {
let hashmap = map.lock().unwrap();
hashmap.get(&id).map(|contents| {
Json(Message {
Expand Down
2 changes: 1 addition & 1 deletion examples/json/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rocket;
use crate::rocket;
use rocket::local::Client;
use rocket::http::{Status, ContentType};

Expand Down
1 change: 1 addition & 0 deletions examples/managed_queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "managed_queue"
version = "0.0.0"
workspace = "../.."
edition = "2018"
publish = false

[dependencies]
Expand Down
Loading

0 comments on commit d9f989a

Please sign in to comment.