From 7954d9d0788ecbe289639a38c9670bae19d61a1b Mon Sep 17 00:00:00 2001 From: Yong Wen Chua Date: Wed, 18 Jul 2018 13:26:33 +0800 Subject: [PATCH] Cargo fmt (#42) --- examples/guard.rs | 2 +- examples/json.rs | 2 +- examples/manual.rs | 2 +- src/fairing.rs | 7 +-- src/headers.rs | 8 ++- src/lib.rs | 132 +++++++++++++++++++++++++++++++++------------ src/test_macros.rs | 44 +++++++++------ tests/guard.rs | 2 +- tests/headers.rs | 2 +- tests/manual.rs | 2 +- 10 files changed, 141 insertions(+), 62 deletions(-) diff --git a/examples/guard.rs b/examples/guard.rs index 96cea68..3a8e59d 100644 --- a/examples/guard.rs +++ b/examples/guard.rs @@ -5,8 +5,8 @@ extern crate rocket_cors; use std::io::Cursor; -use rocket::Response; use rocket::http::Method; +use rocket::Response; use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder}; /// Using a `Responder` -- the usual way you would use this diff --git a/examples/json.rs b/examples/json.rs index 9fabe09..8f9e5d7 100644 --- a/examples/json.rs +++ b/examples/json.rs @@ -5,8 +5,8 @@ extern crate rocket; extern crate rocket_cors as cors; extern crate serde_json; -use rocket::http::Method; use cors::{AllowedHeaders, AllowedOrigins, Cors}; +use rocket::http::Method; fn main() { // The default demonstrates the "All" serialization of several of the settings diff --git a/examples/manual.rs b/examples/manual.rs index 9bb628e..4998cc3 100644 --- a/examples/manual.rs +++ b/examples/manual.rs @@ -5,9 +5,9 @@ extern crate rocket_cors; use std::io::Cursor; -use rocket::{Response, State}; use rocket::http::Method; use rocket::response::Responder; +use rocket::{Response, State}; use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors}; /// Using a borrowed Cors diff --git a/src/fairing.rs b/src/fairing.rs index 09343aa..dac9985 100644 --- a/src/fairing.rs +++ b/src/fairing.rs @@ -1,8 +1,8 @@ //! Fairing implementation use std::str::FromStr; -use rocket::{self, Outcome, Request}; use rocket::http::{self, Header, Status}; +use rocket::{self, Outcome, Request}; use {actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error}; @@ -123,7 +123,8 @@ impl rocket::fairing::Fairing for Cors { fn info(&self) -> rocket::fairing::Info { rocket::fairing::Info { name: "CORS", - kind: rocket::fairing::Kind::Attach | rocket::fairing::Kind::Request + kind: rocket::fairing::Kind::Attach + | rocket::fairing::Kind::Request | rocket::fairing::Kind::Response, } } @@ -166,9 +167,9 @@ impl rocket::fairing::Fairing for Cors { #[cfg(test)] mod tests { - use rocket::Rocket; use rocket::http::{Method, Status}; use rocket::local::Client; + use rocket::Rocket; use {AllOrSome, AllowedHeaders, AllowedOrigins, Cors}; diff --git a/src/headers.rs b/src/headers.rs index e315ef1..cb48f80 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -5,9 +5,9 @@ use std::fmt; use std::ops::Deref; use std::str::FromStr; -use rocket::{self, Outcome}; use rocket::http::Status; use rocket::request::{self, FromRequest}; +use rocket::{self, Outcome}; use unicase::UniCase; use url; @@ -20,7 +20,11 @@ use url_serde; #[derive(Eq, PartialEq, Clone, Debug, Hash)] #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] pub struct HeaderFieldName( - #[cfg_attr(feature = "serialization", serde(with = "unicase_serde::unicase"))] UniCase, + #[cfg_attr( + feature = "serialization", + serde(with = "unicase_serde::unicase") + )] + UniCase, ); impl Deref for HeaderFieldName { diff --git a/src/lib.rs b/src/lib.rs index d30e650..d861b7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -501,18 +501,56 @@ //! - [Supplanted W3C CORS Specification](https://www.w3.org/TR/cors/) //! - [Resource Advice](https://w3c.github.io/webappsec-cors-for-developers/#resources) -#![deny(const_err, dead_code, deprecated, exceeding_bitshifts, improper_ctypes, missing_docs, - mutable_transmutes, no_mangle_const_items, non_camel_case_types, - non_shorthand_field_patterns, non_upper_case_globals, overflowing_literals, - path_statements, plugin_as_library, private_no_mangle_fns, private_no_mangle_statics, - stable_features, trivial_casts, trivial_numeric_casts, unconditional_recursion, - unknown_crate_types, unreachable_code, unused_allocation, unused_assignments, - unused_attributes, unused_comparisons, unused_extern_crates, unused_features, - unused_imports, unused_import_braces, unused_qualifications, unused_must_use, unused_mut, - unused_parens, unused_results, unused_unsafe, unused_variables, variant_size_differences, - warnings, while_true)] -#![allow(legacy_directory_ownership, missing_copy_implementations, missing_debug_implementations, - unknown_lints, unsafe_code, intra_doc_link_resolution_failure)] +#![deny( + const_err, + dead_code, + deprecated, + exceeding_bitshifts, + improper_ctypes, + missing_docs, + mutable_transmutes, + no_mangle_const_items, + non_camel_case_types, + non_shorthand_field_patterns, + non_upper_case_globals, + overflowing_literals, + path_statements, + plugin_as_library, + private_no_mangle_fns, + private_no_mangle_statics, + stable_features, + trivial_casts, + trivial_numeric_casts, + unconditional_recursion, + unknown_crate_types, + unreachable_code, + unused_allocation, + unused_assignments, + unused_attributes, + unused_comparisons, + unused_extern_crates, + unused_features, + unused_imports, + unused_import_braces, + unused_qualifications, + unused_must_use, + unused_mut, + unused_parens, + unused_results, + unused_unsafe, + unused_variables, + variant_size_differences, + warnings, + while_true +)] +#![allow( + legacy_directory_ownership, + missing_copy_implementations, + missing_debug_implementations, + unknown_lints, + unsafe_code, + intra_doc_link_resolution_failure +)] #![cfg_attr(test, feature(plugin))] #![cfg_attr(test, plugin(rocket_codegen))] #![doc(test(attr(allow(unused_variables), deny(warnings))))] @@ -558,13 +596,15 @@ use std::marker::PhantomData; use std::ops::Deref; use std::str::FromStr; -use rocket::{Outcome, State}; use rocket::http::{self, Status}; use rocket::request::{FromRequest, Request}; use rocket::response; +use rocket::{Outcome, State}; -use headers::{AccessControlRequestHeaders, AccessControlRequestMethod, HeaderFieldName, - HeaderFieldNamesSet, Origin, Url}; +use headers::{ + AccessControlRequestHeaders, AccessControlRequestMethod, HeaderFieldName, HeaderFieldNamesSet, + Origin, Url, +}; /// Errors during operations /// @@ -722,7 +762,10 @@ impl AllOrSome { } impl AllOrSome> { - #[deprecated(since = "0.1.3", note = "please use `AllowedOrigins::Some` instead")] + #[deprecated( + since = "0.1.3", + note = "please use `AllowedOrigins::Some` instead" + )] /// New `AllOrSome` from a list of URL strings. /// Returns a tuple where the first element is the struct `AllOrSome`, /// and the second element @@ -833,7 +876,8 @@ impl AllowedOrigins { /// and the second element /// is a map of strings which failed to parse into URLs and their associated parse errors. pub fn some(urls: &[&str]) -> (Self, HashMap) { - let (ok_set, error_map): (Vec<_>, Vec<_>) = urls.iter() + let (ok_set, error_map): (Vec<_>, Vec<_>) = urls + .iter() .map(|s| (s.to_string(), Url::from_str(s))) .partition(|&(_, ref r)| r.is_ok()); @@ -995,7 +1039,10 @@ pub struct Cors { /// [Resource Processing Model](https://www.w3.org/TR/cors/#resource-processing-model). /// /// Defaults to `[GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE]` - #[cfg_attr(feature = "serialization", serde(default = "Cors::default_allowed_methods"))] + #[cfg_attr( + feature = "serialization", + serde(default = "Cors::default_allowed_methods") + )] pub allowed_methods: AllowedMethods, /// The list of header field names which can be used when this resource is accessed by allowed /// origins. @@ -1054,14 +1101,20 @@ pub struct Cors { /// of your existing routes. /// /// Defaults to "/cors" - #[cfg_attr(feature = "serialization", serde(default = "Cors::default_fairing_route_base"))] + #[cfg_attr( + feature = "serialization", + serde(default = "Cors::default_fairing_route_base") + )] pub fairing_route_base: String, /// When used as Fairing, Cors will need to redirect failed CORS checks to a custom route /// mounted by the fairing. Specify the rank of the route so that it doesn't clash with any /// of your existing routes. Remember that a higher ranked route has lower priority. /// /// Defaults to 0 - #[cfg_attr(feature = "serialization", serde(default = "Cors::default_fairing_route_rank"))] + #[cfg_attr( + feature = "serialization", + serde(default = "Cors::default_fairing_route_rank") + )] pub fairing_route_rank: isize, } @@ -1291,7 +1344,8 @@ impl Response { } if !self.expose_headers.is_empty() { - let headers: Vec = self.expose_headers + let headers: Vec = self + .expose_headers .iter() .map(|s| s.deref().to_string()) .collect(); @@ -1303,7 +1357,8 @@ impl Response { } if !self.allow_headers.is_empty() { - let headers: Vec = self.allow_headers + let headers: Vec = self + .allow_headers .iter() .map(|s| s.deref().to_string()) .collect(); @@ -1868,8 +1923,8 @@ fn catch_all_options_route_handler<'r>( mod tests { use std::str::FromStr; - use rocket::local::Client; use rocket::http::Header; + use rocket::local::Client; #[cfg(feature = "serialization")] use serde_json; @@ -1976,7 +2031,10 @@ mod tests { // Build response and check built response header let expected_header = vec!["https://www.example.com"]; let response = response.response(response::Response::new()); - let actual_header: Vec<_> = response.headers().get("Access-Control-Allow-Origin").collect(); + let actual_header: Vec<_> = response + .headers() + .get("Access-Control-Allow-Origin") + .collect(); assert_eq!(expected_header, actual_header); assert!(response.headers().get("Vary").next().is_none()); @@ -1985,15 +2043,16 @@ mod tests { #[test] fn response_sets_allow_origin_with_vary_correctly() { let response = Response::new(); - let response = response.origin( - &FromStr::from_str("https://www.example.com").unwrap(), - true, - ); + let response = + response.origin(&FromStr::from_str("https://www.example.com").unwrap(), true); // Build response and check built response header let expected_header = vec!["https://www.example.com"]; let response = response.response(response::Response::new()); - let actual_header: Vec<_> = response.headers().get("Access-Control-Allow-Origin").collect(); + let actual_header: Vec<_> = response + .headers() + .get("Access-Control-Allow-Origin") + .collect(); assert_eq!(expected_header, actual_header); } @@ -2005,22 +2064,25 @@ mod tests { // Build response and check built response header let expected_header = vec!["*"]; let response = response.response(response::Response::new()); - let actual_header: Vec<_> = response.headers().get("Access-Control-Allow-Origin").collect(); + let actual_header: Vec<_> = response + .headers() + .get("Access-Control-Allow-Origin") + .collect(); assert_eq!(expected_header, actual_header); } #[test] fn response_sets_allow_origin_with_ascii_serialization() { let response = Response::new(); - let response = response.origin( - &FromStr::from_str("https://аpple.com").unwrap(), - false, - ); + let response = response.origin(&FromStr::from_str("https://аpple.com").unwrap(), false); // Build response and check built response header let expected_header = vec!["https://xn--pple-43d.com"]; let response = response.response(response::Response::new()); - let actual_header: Vec<_> = response.headers().get("Access-Control-Allow-Origin").collect(); + let actual_header: Vec<_> = response + .headers() + .get("Access-Control-Allow-Origin") + .collect(); assert_eq!(expected_header, actual_header); } diff --git a/src/test_macros.rs b/src/test_macros.rs index 43c3b87..1733187 100644 --- a/src/test_macros.rs +++ b/src/test_macros.rs @@ -1,26 +1,38 @@ macro_rules! not_err { - ($e:expr) => (match $e { - Ok(e) => e, - Err(e) => panic!("{} failed with {:?}", stringify!($e), e), - }) + ($e:expr) => { + match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {:?}", stringify!($e), e), + } + }; } macro_rules! is_err { - ($e:expr) => (match $e { - Ok(e) => panic!("{} did not return with an error, but with {:?}", stringify!($e), e), - Err(e) => e, - }) + ($e:expr) => { + match $e { + Ok(e) => panic!( + "{} did not return with an error, but with {:?}", + stringify!($e), + e + ), + Err(e) => e, + } + }; } macro_rules! assert_matches { - ($e: expr, $p: pat) => (assert_matches!($e, $p, ())); - ($e: expr, $p: pat, $f: expr) => (match $e { - $p => $f, - e => { - panic!( + ($e:expr, $p:pat) => { + assert_matches!($e, $p, ()) + }; + ($e:expr, $p:pat, $f:expr) => { + match $e { + $p => $f, + e => panic!( "{}: Expected pattern {} \ndoes not match {:?}", - stringify!($e), stringify!($p), e - ) + stringify!($e), + stringify!($p), + e + ), } - }) + }; } diff --git a/tests/guard.rs b/tests/guard.rs index 45e27ec..fc25f7a 100644 --- a/tests/guard.rs +++ b/tests/guard.rs @@ -8,10 +8,10 @@ extern crate rocket_cors as cors; use std::str::FromStr; -use rocket::{Response, State}; use rocket::http::Method; use rocket::http::{Header, Status}; use rocket::local::Client; +use rocket::{Response, State}; #[get("/")] fn cors(cors: cors::Guard) -> cors::Responder<&str> { diff --git a/tests/headers.rs b/tests/headers.rs index c3e6fcb..d857d83 100644 --- a/tests/headers.rs +++ b/tests/headers.rs @@ -8,8 +8,8 @@ extern crate rocket_cors; use std::ops::Deref; use std::str::FromStr; -use rocket::local::Client; use rocket::http::Header; +use rocket::local::Client; use rocket_cors::headers::*; #[get("/request_headers")] diff --git a/tests/manual.rs b/tests/manual.rs index 8c07081..f868f1a 100644 --- a/tests/manual.rs +++ b/tests/manual.rs @@ -8,11 +8,11 @@ extern crate rocket_cors; use std::str::FromStr; -use rocket::State; use rocket::http::Method; use rocket::http::{Header, Status}; use rocket::local::Client; use rocket::response::Responder; +use rocket::State; use rocket_cors::*; /// Using a borrowed `Cors`