Skip to content

Commit

Permalink
Upgrade to Rust 2018 to fix Macro use and import issues (#52)
Browse files Browse the repository at this point in the history
* Upgrade to Rust 2018 to fix Macro use and import issues

Fixes #51

* Cargo fmt
  • Loading branch information
lawliet89 authored Nov 21, 2018
1 parent 9cb16ba commit 13a3f73
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 91 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/lawliet89/rocket_cors"
documentation = "https://docs.rs/rocket_cors/"
keywords = ["rocket", "cors"]
categories = ["web-programming"]
edition = "2018"

[badges]
travis-ci = { repository = "lawliet89/rocket_cors" }
Expand Down
4 changes: 2 additions & 2 deletions examples/fairing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)]
extern crate rocket;
extern crate rocket_cors;
use rocket;
use rocket_cors;

use rocket::http::Method;
use rocket::{get, routes};
Expand Down
12 changes: 6 additions & 6 deletions examples/guard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)]
extern crate rocket;
extern crate rocket_cors;
use rocket;
use rocket_cors;

use std::io::Cursor;

Expand All @@ -11,27 +11,27 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder};

/// Using a `Responder` -- the usual way you would use this
#[get("/")]
fn responder(cors: Guard) -> Responder<&str> {
fn responder(cors: Guard<'_>) -> Responder<'_, &str> {
cors.responder("Hello CORS!")
}

/// Using a `Response` instead of a `Responder`. You generally won't have to do this.
#[get("/response")]
fn response(cors: Guard) -> Response {
fn response(cors: Guard<'_>) -> Response<'_> {
let mut response = Response::new();
response.set_sized_body(Cursor::new("Hello CORS!"));
cors.response(response)
}

/// Manually mount an OPTIONS route for your own handling
#[options("/manual")]
fn manual_options(cors: Guard) -> Responder<&str> {
fn manual_options(cors: Guard<'_>) -> Responder<'_, &str> {
cors.responder("Manual OPTIONS preflight handling")
}

/// Manually mount an OPTIONS route for your own handling
#[get("/manual")]
fn manual(cors: Guard) -> Responder<&str> {
fn manual(cors: Guard<'_>) -> Responder<'_, &str> {
cors.responder("Manual OPTIONS preflight handling")
}

Expand Down
6 changes: 3 additions & 3 deletions examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//!
//! Note: This requires the `serialization` feature which is enabled by default.
#![feature(proc_macro_hygiene, decl_macro)]
extern crate rocket;
extern crate rocket_cors as cors;
extern crate serde_json;

use rocket_cors as cors;
use serde_json;

use crate::cors::{AllowedHeaders, AllowedOrigins, Cors};
use rocket::http::Method;
Expand Down
8 changes: 4 additions & 4 deletions examples/manual.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)]
extern crate rocket;
extern crate rocket_cors;
use rocket;
use rocket_cors;

use std::io::Cursor;

Expand All @@ -13,7 +13,7 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors};
/// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime
/// `'r` and so does `Responder`!
#[get("/")]
fn borrowed(options: State<Cors>) -> impl Responder {
fn borrowed(options: State<'_, Cors>) -> impl Responder<'_> {
options
.inner()
.respond_borrowed(|guard| guard.responder("Hello CORS"))
Expand All @@ -23,7 +23,7 @@ fn borrowed(options: State<Cors>) -> impl Responder {
/// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime
/// `'r` and so does `Responder`!
#[get("/response")]
fn response(options: State<Cors>) -> impl Responder {
fn response(options: State<'_, Cors>) -> impl Responder<'_> {
let mut response = Response::new();
response.set_sized_body(Cursor::new("Hello CORS!"));

Expand Down
6 changes: 3 additions & 3 deletions examples/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//! `ping` route that you want to allow all Origins to access.
#![feature(proc_macro_hygiene, decl_macro)]
extern crate rocket;
extern crate rocket_cors;
use rocket;
use rocket_cors;

use rocket::http::Method;
use rocket::response::Responder;
Expand All @@ -14,7 +14,7 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, Guard};

/// The "usual" app route
#[get("/")]
fn app(cors: Guard) -> rocket_cors::Responder<&str> {
fn app(cors: Guard<'_>) -> rocket_cors::Responder<'_, &str> {
cors.responder("Hello CORS!")
}

Expand Down
14 changes: 7 additions & 7 deletions src/fairing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Fairing implementation
use log::{error, info, log};
use ::log::{error, info, log};
use rocket::http::{self, uri::Origin, Status};
use rocket::{self, error_, info_, log_, Outcome, Request};

Expand All @@ -16,7 +16,7 @@ enum CorsValidation {

/// Route for Fairing error handling
pub(crate) fn fairing_error_route<'r>(
request: &'r Request,
request: &'r Request<'_>,
_: rocket::Data,
) -> rocket::handler::Outcome<'r> {
let status = request
Expand All @@ -36,7 +36,7 @@ fn fairing_route(rank: isize) -> rocket::Route {
}

/// Modifies a `Request` to route to Fairing error handler
fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request) {
fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request<'_>) {
let origin = Origin::parse_owned(format!("{}/{}", options.fairing_route_base, status)).unwrap();

request.set_method(http::Method::Get);
Expand All @@ -45,8 +45,8 @@ fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Req

fn on_response_wrapper(
options: &Cors,
request: &Request,
response: &mut rocket::Response,
request: &Request<'_>,
response: &mut rocket::Response<'_>,
) -> Result<(), Error> {
let origin = match origin(request)? {
None => {
Expand Down Expand Up @@ -112,7 +112,7 @@ impl rocket::fairing::Fairing for Cors {
}
}

fn on_request(&self, request: &mut Request, _: &rocket::Data) {
fn on_request(&self, request: &mut Request<'_>, _: &rocket::Data) {
let result = match validate(self, request) {
Ok(_) => CorsValidation::Success,
Err(err) => {
Expand All @@ -126,7 +126,7 @@ impl rocket::fairing::Fairing for Cors {
let _ = request.local_cache(|| result);
}

fn on_response(&self, request: &Request, response: &mut rocket::Response) {
fn on_response(&self, request: &Request<'_>, response: &mut rocket::Response<'_>) {
if let Err(err) = on_response_wrapper(self, request, response) {
error_!("Fairings on_response error: {}\nMost likely a bug", err);
response.set_status(Status::InternalServerError);
Expand Down
4 changes: 2 additions & 2 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Deref for HeaderFieldName {
}

impl fmt::Display for HeaderFieldName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ pub type HeaderFieldNamesSet = HashSet<HeaderFieldName>;
pub struct Url(#[cfg_attr(feature = "serialization", serde(with = "url_serde"))] url::Url);

impl fmt::Display for Url {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
Expand Down
63 changes: 20 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,29 +550,6 @@
)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]

extern crate log;
extern crate rocket;
extern crate unicase;
extern crate url;

#[cfg(feature = "serialization")]
extern crate serde;
#[cfg(feature = "serialization")]
extern crate serde_derive;
#[cfg(feature = "serialization")]
extern crate unicase_serde;
#[cfg(feature = "serialization")]
extern crate url_serde;

#[cfg(test)]
extern crate hyper;
#[cfg(feature = "serialization")]
#[cfg(test)]
extern crate serde_json;
#[cfg(feature = "serialization")]
#[cfg(test)]
extern crate serde_test;

#[cfg(test)]
#[macro_use]
mod test_macros;
Expand All @@ -588,7 +565,7 @@ use std::marker::PhantomData;
use std::ops::Deref;
use std::str::FromStr;

use log::{error, info, log};
use ::log::{error, info, log};
use rocket::http::{self, Status};
use rocket::request::{FromRequest, Request};
use rocket::response;
Expand Down Expand Up @@ -687,7 +664,7 @@ impl error::Error for Error {
}
}

fn cause(&self) -> Option<&error::Error> {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::BadOrigin(ref e) => Some(e),
_ => Some(self),
Expand All @@ -696,7 +673,7 @@ impl error::Error for Error {
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::BadOrigin(ref e) => fmt::Display::fmt(e, f),
_ => write!(f, "{}", error::Error::description(self)),
Expand All @@ -705,7 +682,7 @@ impl fmt::Display for Error {
}

impl<'r> response::Responder<'r> for Error {
fn respond_to(self, _: &Request) -> Result<response::Response<'r>, Status> {
fn respond_to(self, _: &Request<'_>) -> Result<response::Response<'r>, Status> {
error_!("CORS Error: {}", self);
Err(self.status())
}
Expand Down Expand Up @@ -786,7 +763,7 @@ impl From<http::Method> for Method {
}

impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
Expand Down Expand Up @@ -820,7 +797,7 @@ mod method_serde {
impl<'de> Visitor<'de> for MethodVisitor {
type Value = Method;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a string containing a HTTP Verb")
}

Expand Down Expand Up @@ -1303,7 +1280,7 @@ impl Response {
/// Merge CORS headers with an existing `rocket::Response`.
///
/// This will overwrite any existing CORS headers
fn merge(&self, response: &mut response::Response) {
fn merge(&self, response: &mut response::Response<'_>) {
// TODO: We should be able to remove this
let origin = match self.allow_origin {
None => {
Expand Down Expand Up @@ -1426,7 +1403,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Guard<'r> {
type Error = Error;

fn from_request(request: &'a Request<'r>) -> rocket::request::Outcome<Self, Self::Error> {
let options = match request.guard::<State<Cors>>() {
let options = match request.guard::<State<'_, Cors>>() {
Outcome::Success(options) => options,
_ => {
let error = Error::MissingCorsInRocketState;
Expand Down Expand Up @@ -1461,7 +1438,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Guard<'r> {
pub struct Responder<'r, R> {
responder: R,
cors_response: Response,
marker: PhantomData<response::Responder<'r>>,
marker: PhantomData<dyn response::Responder<'r>>,
}

impl<'r, R: response::Responder<'r>> Responder<'r, R> {
Expand All @@ -1474,15 +1451,15 @@ impl<'r, R: response::Responder<'r>> Responder<'r, R> {
}

/// Respond to a request
fn respond(self, request: &Request) -> response::Result<'r> {
fn respond(self, request: &Request<'_>) -> response::Result<'r> {
let mut response = self.responder.respond_to(request)?; // handle status errors?
self.cors_response.merge(&mut response);
Ok(response)
}
}

impl<'r, R: response::Responder<'r>> response::Responder<'r> for Responder<'r, R> {
fn respond_to(self, request: &Request) -> response::Result<'r> {
fn respond_to(self, request: &Request<'_>) -> response::Result<'r> {
self.respond(request)
}
}
Expand Down Expand Up @@ -1514,7 +1491,7 @@ where
}
}

fn build_guard(&self, request: &Request) -> Result<Guard<'r>, Error> {
fn build_guard(&self, request: &Request<'_>) -> Result<Guard<'r>, Error> {
let response = Response::validate_and_build(&self.options, request)?;
Ok(Guard::new(response))
}
Expand All @@ -1525,7 +1502,7 @@ where
F: FnOnce(Guard<'r>) -> R + 'r,
R: response::Responder<'r>,
{
fn respond_to(self, request: &Request) -> response::Result<'r> {
fn respond_to(self, request: &Request<'_>) -> response::Result<'r> {
let guard = match self.build_guard(request) {
Ok(guard) => guard,
Err(err) => {
Expand Down Expand Up @@ -1554,7 +1531,7 @@ enum ValidationResult {
}

/// Validates a request for CORS and returns a CORS Response
fn validate_and_build(options: &Cors, request: &Request) -> Result<Response, Error> {
fn validate_and_build(options: &Cors, request: &Request<'_>) -> Result<Response, Error> {
let result = validate(options, request)?;

Ok(match result {
Expand All @@ -1567,7 +1544,7 @@ fn validate_and_build(options: &Cors, request: &Request) -> Result<Response, Err
}

/// Validate a CORS request
fn validate(options: &Cors, request: &Request) -> Result<ValidationResult, Error> {
fn validate(options: &Cors, request: &Request<'_>) -> Result<ValidationResult, Error> {
// 1. If the Origin header is not present terminate this set of steps.
// The request is outside the scope of this specification.
let origin = origin(request)?;
Expand Down Expand Up @@ -1644,7 +1621,7 @@ fn validate_allowed_headers(
}

/// Gets the `Origin` request header from the request
fn origin(request: &Request) -> Result<Option<Origin>, Error> {
fn origin(request: &Request<'_>) -> Result<Option<Origin>, Error> {
match Origin::from_request(request) {
Outcome::Forward(()) => Ok(None),
Outcome::Success(origin) => Ok(Some(origin)),
Expand All @@ -1653,7 +1630,7 @@ fn origin(request: &Request) -> Result<Option<Origin>, Error> {
}

/// Gets the `Access-Control-Request-Method` request header from the request
fn request_method(request: &Request) -> Result<Option<AccessControlRequestMethod>, Error> {
fn request_method(request: &Request<'_>) -> Result<Option<AccessControlRequestMethod>, Error> {
match AccessControlRequestMethod::from_request(request) {
Outcome::Forward(()) => Ok(None),
Outcome::Success(method) => Ok(Some(method)),
Expand All @@ -1662,7 +1639,7 @@ fn request_method(request: &Request) -> Result<Option<AccessControlRequestMethod
}

/// Gets the `Access-Control-Request-Headers` request header from the request
fn request_headers(request: &Request) -> Result<Option<AccessControlRequestHeaders>, Error> {
fn request_headers(request: &Request<'_>) -> Result<Option<AccessControlRequestHeaders>, Error> {
match AccessControlRequestHeaders::from_request(request) {
Outcome::Forward(()) => Ok(None),
Outcome::Success(geaders) => Ok(Some(geaders)),
Expand Down Expand Up @@ -1887,10 +1864,10 @@ pub fn catch_all_options_routes() -> Vec<rocket::Route> {

/// Handler for the "catch all options route"
fn catch_all_options_route_handler<'r>(
request: &'r Request,
request: &'r Request<'_>,
_: rocket::Data,
) -> rocket::handler::Outcome<'r> {
let guard: Guard = match request.guard() {
let guard: Guard<'_> = match request.guard() {
Outcome::Success(guard) => guard,
Outcome::Failure((status, _)) => return rocket::handler::Outcome::failure(status),
Outcome::Forward(()) => unreachable!("Should not be reachable"),
Expand Down
Loading

0 comments on commit 13a3f73

Please sign in to comment.