From ede4b1a22227676e82b3bd2a2d73c32d0e47781f Mon Sep 17 00:00:00 2001 From: Yong Wen Chua Date: Wed, 26 Jul 2017 14:37:02 +0800 Subject: [PATCH] Cargo clippy fixes --- build.rs | 8 ++++---- src/fairing.rs | 13 +++++++++---- src/lib.rs | 36 ++++++++++++++++-------------------- tests/fairing.rs | 2 +- tests/guard.rs | 2 +- tests/manual.rs | 2 +- 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/build.rs b/build.rs index 95b5a5e..4385c34 100644 --- a/build.rs +++ b/build.rs @@ -44,13 +44,13 @@ fn main() { printerr!( "{} {}", Red.bold().paint("Error:"), - White.paint("rowdy requires a nightly version of Rust.") + White.paint("rocket_cors requires a nightly version of Rust.") ); print_version_err(&*version, &*date); printerr!( "{}{}{}", Blue.paint("See the README ("), - White.paint("https://github.com/lawliet89/rowdy"), + White.paint("https://github.com/lawliet89/rocket_cors"), Blue.paint(") for more information.") ); panic!("Aborting compilation due to incompatible compiler.") @@ -60,7 +60,7 @@ fn main() { printerr!( "{} {}", Red.bold().paint("Error:"), - White.paint("rowdy requires a more recent version of rustc.") + White.paint("rocket_cors requires a more recent version of rustc.") ); printerr!( "{}{}{}", @@ -75,7 +75,7 @@ fn main() { _ => { println!( "cargo:warning={}", - "rowdy was unable to check rustc compatibility." + "rocket_cors was unable to check rustc compatibility." ); println!( "cargo:warning={}", diff --git a/src/fairing.rs b/src/fairing.rs index 3501583..cf375ee 100644 --- a/src/fairing.rs +++ b/src/fairing.rs @@ -1,4 +1,6 @@ //! Fairing implementation +use std::str::FromStr; + use rocket::{self, Request, Outcome}; use rocket::http::{self, Status, Header}; @@ -18,7 +20,10 @@ impl InjectedHeader { InjectedHeader::Failure => "Failure", } } +} +impl FromStr for InjectedHeader { + type Err = Error; fn from_str(s: &str) -> Result { match s { "Success" => Ok(InjectedHeader::Success), @@ -59,7 +64,7 @@ fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Req } /// Inject a header into the Request with result -fn inject_request_header(header: InjectedHeader, request: &mut Request) { +fn inject_request_header(header: &InjectedHeader, request: &mut Request) { request.replace_header(Header::new(CORS_HEADER, header.to_str())); } @@ -89,9 +94,9 @@ fn on_response_wrapper( let cors_response = if request.method() == http::Method::Options { let headers = request_headers(request)?; - preflight_response(options, origin, headers) + preflight_response(options, &origin, headers.as_ref()) } else { - actual_request_response(options, origin) + actual_request_response(options, &origin) }; cors_response.merge(response); @@ -147,7 +152,7 @@ impl rocket::fairing::Fairing for Cors { } }; - inject_request_header(injected_header, request); + inject_request_header(&injected_header, request); } fn on_response(&self, request: &Request, response: &mut rocket::Response) { diff --git a/src/lib.rs b/src/lib.rs index a59c51a..abf1f0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1403,9 +1403,9 @@ fn validate_and_build(options: &Cors, request: &Request) -> Result Response::new(), ValidationResult::Preflight { origin, headers } => { - preflight_response(options, origin, headers) + preflight_response(options, &origin, headers.as_ref()) } - ValidationResult::Request { origin } => actual_request_response(options, origin), + ValidationResult::Request { origin } => actual_request_response(options, &origin), }) } @@ -1532,7 +1532,7 @@ fn preflight_validate( // 2. If the value of the Origin header is not a case-sensitive match for any of the values // in list of origins do not set any additional headers and terminate this set of steps. - validate_origin(&origin, &options.allowed_origins)?; + validate_origin(origin, &options.allowed_origins)?; // 3. Let `method` be the value as result of parsing the Access-Control-Request-Method // header. @@ -1558,7 +1558,7 @@ fn preflight_validate( // values in list of headers do not set any additional headers and terminate this set of // steps. - if let &Some(ref headers) = headers { + if let Some(ref headers) = *headers { validate_allowed_headers(headers, &options.allowed_headers)?; } @@ -1571,8 +1571,8 @@ fn preflight_validate( /// [W3C recommendation](https://www.w3.org/TR/cors/#resource-preflight-requests). fn preflight_response( options: &Cors, - origin: Origin, - headers: Option, + origin: &Origin, + headers: Option<&AccessControlRequestHeaders>, ) -> Response { let response = Response::new(); @@ -1590,10 +1590,10 @@ fn preflight_response( if options.send_wildcard { response.any() } else { - response.origin(&origin, true) + response.origin(origin, true) } } - AllOrSome::Some(_) => response.origin(&origin, false), + AllOrSome::Some(_) => response.origin(origin, false), }; let response = response.credentials(options.allow_credentials); @@ -1623,7 +1623,7 @@ fn preflight_response( // from Access-Control-Allow-Headers can be enough. // We do not do anything special with simple headers - let response = if let Some(ref headers) = headers { + if let Some(headers) = headers { let &AccessControlRequestHeaders(ref headers) = headers; response.headers( headers @@ -1634,9 +1634,7 @@ fn preflight_response( ) } else { response - }; - - response + } } /// Do checks for an actual request @@ -1652,7 +1650,7 @@ fn actual_request_validate(options: &Cors, origin: &Origin) -> Result<(), Error> // in list of origins, do not set any additional headers and terminate this set of steps. // Always matching is acceptable since the list of origins can be unbounded. - validate_origin(&origin, &options.allowed_origins)?; + validate_origin(origin, &options.allowed_origins)?; Ok(()) } @@ -1661,7 +1659,7 @@ fn actual_request_validate(options: &Cors, origin: &Origin) -> Result<(), Error> /// /// This implementation references the /// [W3C recommendation](https://www.w3.org/TR/cors/#resource-requests). -fn actual_request_response(options: &Cors, origin: Origin) -> Response { +fn actual_request_response(options: &Cors, origin: &Origin) -> Response { let response = Response::new(); // 3. If the resource supports credentials add a single Access-Control-Allow-Origin header, @@ -1679,10 +1677,10 @@ fn actual_request_response(options: &Cors, origin: Origin) -> Response { if options.send_wildcard { response.any() } else { - response.origin(&origin, true) + response.origin(origin, true) } } - AllOrSome::Some(_) => response.origin(&origin, false), + AllOrSome::Some(_) => response.origin(origin, false), }; let response = response.credentials(options.allow_credentials); @@ -1694,16 +1692,14 @@ fn actual_request_response(options: &Cors, origin: Origin) -> Response { // of all entries where origin is a case-sensitive match for the value of the Origin header // and url is a case-sensitive match for the URL of the resource. - let response = response.exposed_headers( + response.exposed_headers( options .expose_headers .iter() .map(|s| &**s) .collect::>() .as_slice(), - ); - - response + ) } /// Returns "catch all" OPTIONS routes that you can mount to catch all OPTIONS request. Only works diff --git a/tests/fairing.rs b/tests/fairing.rs index 9d0c365..c96a123 100644 --- a/tests/fairing.rs +++ b/tests/fairing.rs @@ -1,4 +1,4 @@ -//! This crate tests using rocket_cors using Fairings +//! This crate tests using `rocket_cors` using Fairings #![feature(plugin)] #![plugin(rocket_codegen)] diff --git a/tests/guard.rs b/tests/guard.rs index bc0b5b1..ea7eddc 100644 --- a/tests/guard.rs +++ b/tests/guard.rs @@ -1,4 +1,4 @@ -//! This crate tests using rocket_cors using the per-route handling with request guard +//! This crate tests using `rocket_cors` using the per-route handling with request guard #![feature(plugin)] #![plugin(rocket_codegen)] diff --git a/tests/manual.rs b/tests/manual.rs index bb963b9..c266004 100644 --- a/tests/manual.rs +++ b/tests/manual.rs @@ -1,4 +1,4 @@ -//! This crate tests using rocket_cors using manual mode +//! This crate tests using `rocket_cors` using manual mode #![feature(plugin, conservative_impl_trait)] #![plugin(rocket_codegen)]