From 3f5a39a5b7116c890a1e105d08b6ac111d954237 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 1 Jun 2018 09:37:14 -0700 Subject: [PATCH] cargo fmt --- src/application.rs | 91 ++++++++++++++++---------------- src/client/mod.rs | 3 +- src/client/request.rs | 14 +++-- src/error.rs | 4 +- src/extractor.rs | 48 ++++++++--------- src/handler.rs | 42 ++++++++------- src/httprequest.rs | 6 +-- src/httpresponse.rs | 9 ++-- src/json.rs | 23 ++++---- src/middleware/cors.rs | 37 +++++++------ src/middleware/csrf.rs | 13 +++-- src/middleware/defaultheaders.rs | 9 ++-- src/middleware/errhandlers.rs | 16 +++--- src/middleware/identity.rs | 27 +++++----- src/middleware/logger.rs | 2 +- src/param.rs | 4 +- src/pred.rs | 20 ++++--- src/resource.rs | 13 ++--- src/route.rs | 48 +++++++++-------- src/scope.rs | 81 ++++++++++++++-------------- src/with.rs | 20 +++---- 21 files changed, 279 insertions(+), 251 deletions(-) diff --git a/src/application.rs b/src/application.rs index 94fb70fb2..ab1548bff 100644 --- a/src/application.rs +++ b/src/application.rs @@ -267,8 +267,8 @@ where /// let app = App::new() /// .prefix("/app") /// .resource("/test", |r| { - /// r.get().f(|_| HttpResponse::Ok()); - /// r.head().f(|_| HttpResponse::MethodNotAllowed()); + /// r.get().f(|_| HttpResponse::Ok()); + /// r.head().f(|_| HttpResponse::MethodNotAllowed()); /// }) /// .finish(); /// } @@ -300,10 +300,12 @@ where /// /// fn main() { /// let app = App::new() - /// .route("/test", http::Method::GET, - /// |_: HttpRequest| HttpResponse::Ok()) - /// .route("/test", http::Method::POST, - /// |_: HttpRequest| HttpResponse::MethodNotAllowed()); + /// .route("/test", http::Method::GET, |_: HttpRequest| { + /// HttpResponse::Ok() + /// }) + /// .route("/test", http::Method::POST, |_: HttpRequest| { + /// HttpResponse::MethodNotAllowed() + /// }); /// } /// ``` pub fn route(mut self, path: &str, method: Method, f: F) -> App @@ -345,12 +347,12 @@ where /// use actix_web::{http, App, HttpRequest, HttpResponse}; /// /// fn main() { - /// let app = App::new() - /// .scope("/{project_id}", |scope| { - /// scope.resource("/path1", |r| r.f(|_| HttpResponse::Ok())) - /// .resource("/path2", |r| r.f(|_| HttpResponse::Ok())) - /// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed())) - /// }); + /// let app = App::new().scope("/{project_id}", |scope| { + /// scope + /// .resource("/path1", |r| r.f(|_| HttpResponse::Ok())) + /// .resource("/path2", |r| r.f(|_| HttpResponse::Ok())) + /// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed())) + /// }); /// } /// ``` /// @@ -402,11 +404,10 @@ where /// use actix_web::{http, App, HttpResponse}; /// /// fn main() { - /// let app = App::new() - /// .resource("/users/{userid}/{friend}", |r| { - /// r.get().f(|_| HttpResponse::Ok()); - /// r.head().f(|_| HttpResponse::MethodNotAllowed()); - /// }); + /// let app = App::new().resource("/users/{userid}/{friend}", |r| { + /// r.get().f(|_| HttpResponse::Ok()); + /// r.head().f(|_| HttpResponse::MethodNotAllowed()); + /// }); /// } /// ``` pub fn resource(mut self, path: &str, f: F) -> App @@ -469,9 +470,9 @@ where /// use actix_web::{App, HttpRequest, HttpResponse, Result}; /// /// fn index(mut req: HttpRequest) -> Result { - /// let url = req.url_for("youtube", &["oHg5SJYRHA0"])?; - /// assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0"); - /// Ok(HttpResponse::Ok().into()) + /// let url = req.url_for("youtube", &["oHg5SJYRHA0"])?; + /// assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0"); + /// Ok(HttpResponse::Ok().into()) /// } /// /// fn main() { @@ -514,13 +515,11 @@ where /// use actix_web::{http, App, HttpRequest, HttpResponse}; /// /// fn main() { - /// let app = App::new() - /// .handler("/app", |req: HttpRequest| { - /// match *req.method() { - /// http::Method::GET => HttpResponse::Ok(), - /// http::Method::POST => HttpResponse::MethodNotAllowed(), - /// _ => HttpResponse::NotFound(), - /// }}); + /// let app = App::new().handler("/app", |req: HttpRequest| match *req.method() { + /// http::Method::GET => HttpResponse::Ok(), + /// http::Method::POST => HttpResponse::MethodNotAllowed(), + /// _ => HttpResponse::NotFound(), + /// }); /// } /// ``` pub fn handler>(mut self, path: &str, handler: H) -> App { @@ -561,15 +560,14 @@ where /// /// ```rust /// # extern crate actix_web; - /// use actix_web::{App, HttpResponse, fs, middleware}; + /// use actix_web::{fs, middleware, App, HttpResponse}; /// /// // this function could be located in different module /// fn config(app: App) -> App { - /// app - /// .resource("/test", |r| { - /// r.get().f(|_| HttpResponse::Ok()); - /// r.head().f(|_| HttpResponse::MethodNotAllowed()); - /// }) + /// app.resource("/test", |r| { + /// r.get().f(|_| HttpResponse::Ok()); + /// r.head().f(|_| HttpResponse::MethodNotAllowed()); + /// }) /// } /// /// fn main() { @@ -636,19 +634,22 @@ where /// struct State2; /// /// fn main() { - /// # thread::spawn(|| { - /// server::new(|| { vec![ - /// App::with_state(State1) - /// .prefix("/app1") - /// .resource("/", |r| r.f(|r| HttpResponse::Ok())) - /// .boxed(), - /// App::with_state(State2) - /// .prefix("/app2") - /// .resource("/", |r| r.f(|r| HttpResponse::Ok())) - /// .boxed() ]}) - /// .bind("127.0.0.1:8080").unwrap() + /// //#### # thread::spawn(|| { + /// server::new(|| { + /// vec![ + /// App::with_state(State1) + /// .prefix("/app1") + /// .resource("/", |r| r.f(|r| HttpResponse::Ok())) + /// .boxed(), + /// App::with_state(State2) + /// .prefix("/app2") + /// .resource("/", |r| r.f(|r| HttpResponse::Ok())) + /// .boxed(), + /// ] + /// }).bind("127.0.0.1:8080") + /// .unwrap() /// .run() - /// # }); + /// //#### # }); /// } /// ``` pub fn boxed(mut self) -> Box { diff --git a/src/client/mod.rs b/src/client/mod.rs index 0e7befc3c..3c9567333 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -81,7 +81,8 @@ impl ResponseError for SendRequestError { /// println!("Response: {:?}", response); /// # process::exit(0); /// Ok(()) -/// })); +/// }), +/// ); /// } /// ``` pub fn get>(uri: U) -> ClientRequestBuilder { diff --git a/src/client/request.rs b/src/client/request.rs index eebf8e007..4fef3e5d0 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -43,7 +43,7 @@ use httprequest::HttpRequest; /// println!("Response: {:?}", response); /// # process::exit(0); /// Ok(()) -/// }) +/// }), /// ); /// } /// ``` @@ -344,7 +344,8 @@ impl ClientRequestBuilder { /// let req = client::ClientRequest::build() /// .set(http::header::Date::now()) /// .set(http::header::ContentType(mime::TEXT_HTML)) - /// .finish().unwrap(); + /// .finish() + /// .unwrap(); /// } /// ``` #[doc(hidden)] @@ -376,7 +377,8 @@ impl ClientRequestBuilder { /// let req = ClientRequest::build() /// .header("X-TEST", "value") /// .header(header::CONTENT_TYPE, "application/json") - /// .finish().unwrap(); + /// .finish() + /// .unwrap(); /// } /// ``` pub fn header(&mut self, key: K, value: V) -> &mut Self @@ -486,8 +488,10 @@ impl ClientRequestBuilder { /// .path("/") /// .secure(true) /// .http_only(true) - /// .finish()) - /// .finish().unwrap(); + /// .finish(), + /// ) + /// .finish() + /// .unwrap(); /// } /// ``` pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self { diff --git a/src/error.rs b/src/error.rs index 6d739702d..a4c513fb3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -552,8 +552,8 @@ impl From for UrlGenerationError { /// use actix_web::fs::NamedFile; /// /// fn index(req: HttpRequest) -> Result { -/// let f = NamedFile::open("test.txt").map_err(error::ErrorBadRequest)?; -/// Ok(f) +/// let f = NamedFile::open("test.txt").map_err(error::ErrorBadRequest)?; +/// Ok(f) /// } /// # fn main() {} /// ``` diff --git a/src/extractor.rs b/src/extractor.rs index fc9145b92..caf34332d 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -24,7 +24,7 @@ use httprequest::HttpRequest; /// # extern crate bytes; /// # extern crate actix_web; /// # extern crate futures; -/// use actix_web::{App, Path, Result, http}; +/// use actix_web::{http, App, Path, Result}; /// /// /// extract path info from "/{username}/{count}/index.html" url /// /// {username} - deserializes to a String @@ -35,8 +35,9 @@ use httprequest::HttpRequest; /// /// fn main() { /// let app = App::new().resource( -/// "/{username}/{count}/index.html", // <- define path parameters -/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor +/// "/{username}/{count}/index.html", // <- define path parameters +/// |r| r.method(http::Method::GET).with(index), +/// ); // <- use `with` extractor /// } /// ``` /// @@ -48,7 +49,7 @@ use httprequest::HttpRequest; /// # extern crate actix_web; /// # extern crate futures; /// #[macro_use] extern crate serde_derive; -/// use actix_web::{App, Path, Result, http}; +/// use actix_web::{http, App, Path, Result}; /// /// #[derive(Deserialize)] /// struct Info { @@ -62,8 +63,9 @@ use httprequest::HttpRequest; /// /// fn main() { /// let app = App::new().resource( -/// "/{username}/index.html", // <- define path parameters -/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor +/// "/{username}/index.html", // <- define path parameters +/// |r| r.method(http::Method::GET).with(index), +/// ); // <- use `with` extractor /// } /// ``` pub struct Path { @@ -118,13 +120,13 @@ where /// ## Example /// /// ```rust -/// # extern crate bytes; -/// # extern crate actix_web; -/// # extern crate futures; -/// #[macro_use] extern crate serde_derive; +/// //#### # extern crate bytes; +/// //#### # extern crate actix_web; +/// //#### # extern crate futures; +/// //#### #[macro_use] extern crate serde_derive; /// use actix_web::{App, Query, http}; /// -/// #[derive(Deserialize)] +/// //#### #[derive(Deserialize)] /// struct Info { /// username: String, /// } @@ -255,7 +257,7 @@ where /// ```rust /// # extern crate actix_web; /// #[macro_use] extern crate serde_derive; -/// use actix_web::{App, Form, Result, http}; +/// use actix_web::{http, App, Form, Result}; /// /// #[derive(Deserialize)] /// struct FormData { @@ -270,10 +272,10 @@ where /// /// fn main() { /// let app = App::new().resource( -/// "/index.html", |r| { -/// r.method(http::Method::GET) -/// .with(index) -/// .limit(4096);} // <- change form extractor configuration +/// "/index.html", +/// |r| { +/// r.method(http::Method::GET).with(index).limit(4096); +/// }, // <- change form extractor configuration /// ); /// } /// ``` @@ -315,9 +317,8 @@ impl Default for FormConfig { /// } /// /// fn main() { -/// let app = App::new().resource( -/// "/index.html", |r| -/// r.method(http::Method::GET).with(index)); +/// let app = App::new() +/// .resource("/index.html", |r| r.method(http::Method::GET).with(index)); /// } /// ``` impl FromRequest for Bytes { @@ -354,12 +355,11 @@ impl FromRequest for Bytes { /// } /// /// fn main() { -/// let app = App::new().resource( -/// "/index.html", |r| { -/// r.method(http::Method::GET) +/// let app = App::new().resource("/index.html", |r| { +/// r.method(http::Method::GET) /// .with(index) // <- register handler with extractor params -/// .limit(4096); // <- limit size of the payload -/// }); +/// .limit(4096); // <- limit size of the payload +/// }); /// } /// ``` impl FromRequest for String { diff --git a/src/handler.rs b/src/handler.rs index 759291a20..bae50937d 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -61,22 +61,24 @@ pub trait FromRequest: Sized { /// # extern crate actix_web; /// # extern crate futures; /// # use futures::future::Future; +/// use actix_web::{AsyncResponder, Either, Error, HttpRequest, HttpResponse}; /// use futures::future::result; -/// use actix_web::{Either, Error, HttpRequest, HttpResponse, AsyncResponder}; -/// -/// type RegisterResult = Either>>; /// +/// type RegisterResult = +/// Either>>; /// /// fn index(req: HttpRequest) -> RegisterResult { -/// if is_a_variant() { // <- choose variant A -/// Either::A( -/// HttpResponse::BadRequest().body("Bad data")) +/// if is_a_variant() { +/// // <- choose variant A +/// Either::A(HttpResponse::BadRequest().body("Bad data")) /// } else { -/// Either::B( // <- variant B +/// Either::B( +/// // <- variant B /// result(Ok(HttpResponse::Ok() -/// .content_type("text/html") -/// .body("Hello!"))) -/// .responder()) +/// .content_type("text/html") +/// .body("Hello!"))) +/// .responder(), +/// ) /// } /// } /// # fn is_a_variant() -> bool { true } @@ -138,16 +140,17 @@ where /// # extern crate actix_web; /// # extern crate futures; /// # #[macro_use] extern crate serde_derive; -/// use futures::future::Future; /// use actix_web::{ -/// App, HttpRequest, HttpResponse, HttpMessage, Error, AsyncResponder}; +/// App, AsyncResponder, Error, HttpMessage, HttpRequest, HttpResponse, +/// }; +/// use futures::future::Future; /// /// #[derive(Deserialize, Debug)] /// struct MyObj { /// name: String, /// } /// -/// fn index(mut req: HttpRequest) -> Box> { +/// fn index(mut req: HttpRequest) -> Box> { /// req.json() // <- get JsonBody future /// .from_err() /// .and_then(|val: MyObj| { // <- deserialized value @@ -479,10 +482,12 @@ where /// # extern crate actix_web; /// # extern crate futures; /// #[macro_use] extern crate serde_derive; -/// use actix_web::{App, Path, State, http}; +/// use actix_web::{http, App, Path, State}; /// /// /// Application state -/// struct MyApp {msg: &'static str} +/// struct MyApp { +/// msg: &'static str, +/// } /// /// #[derive(Deserialize)] /// struct Info { @@ -496,9 +501,10 @@ where /// } /// /// fn main() { -/// let app = App::with_state(MyApp{msg: "Welcome"}).resource( -/// "/{username}/index.html", // <- define path parameters -/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor +/// let app = App::with_state(MyApp { msg: "Welcome" }).resource( +/// "/{username}/index.html", // <- define path parameters +/// |r| r.method(http::Method::GET).with(index), +/// ); // <- use `with` extractor /// } /// ``` pub struct State(HttpRequest); diff --git a/src/httprequest.rs b/src/httprequest.rs index 0a14ca043..b75bbbe4e 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -283,9 +283,9 @@ impl HttpRequest { /// Generate url for named resource /// /// ```rust - /// # extern crate actix_web; - /// # use actix_web::{App, HttpRequest, HttpResponse, http}; - /// # + /// //#### # extern crate actix_web; + /// //#### # use actix_web::{App, HttpRequest, HttpResponse, http}; + /// //#### # /// fn index(req: HttpRequest) -> HttpResponse { /// let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource /// HttpResponse::Ok().into() diff --git a/src/httpresponse.rs b/src/httpresponse.rs index af5a20c58..ac84b2be4 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -297,11 +297,13 @@ impl HttpResponseBuilder { /// /// ```rust /// # extern crate actix_web; - /// use actix_web::{HttpRequest, HttpResponse, Result, http}; + /// use actix_web::{http, HttpRequest, HttpResponse, Result}; /// /// fn index(req: HttpRequest) -> Result { /// Ok(HttpResponse::Ok() - /// .set(http::header::IfModifiedSince("Sun, 07 Nov 1994 08:48:37 GMT".parse()?)) + /// .set(http::header::IfModifiedSince( + /// "Sun, 07 Nov 1994 08:48:37 GMT".parse()?, + /// )) /// .finish()) /// } /// fn main() {} @@ -455,7 +457,8 @@ impl HttpResponseBuilder { /// .path("/") /// .secure(true) /// .http_only(true) - /// .finish()) + /// .finish(), + /// ) /// .finish() /// } /// ``` diff --git a/src/json.rs b/src/json.rs index e48c27ef4..8e5cc293c 100644 --- a/src/json.rs +++ b/src/json.rs @@ -32,11 +32,11 @@ use httpresponse::HttpResponse; /// ## Example /// /// ```rust -/// # extern crate actix_web; -/// #[macro_use] extern crate serde_derive; +/// //#### # extern crate actix_web; +/// //#### #[macro_use] extern crate serde_derive; /// use actix_web::{App, Json, Result, http}; /// -/// #[derive(Deserialize)] +/// //#### #[derive(Deserialize)] /// struct Info { /// username: String, /// } @@ -69,7 +69,9 @@ use httpresponse::HttpResponse; /// } /// /// fn index(req: HttpRequest) -> Result> { -/// Ok(Json(MyObj{name: req.match_info().query("name")?})) +/// Ok(Json(MyObj { +/// name: req.match_info().query("name")?, +/// })) /// } /// # fn main() {} /// ``` @@ -154,7 +156,7 @@ where /// ```rust /// # extern crate actix_web; /// #[macro_use] extern crate serde_derive; -/// use actix_web::{App, Json, HttpResponse, Result, http, error}; +/// use actix_web::{error, http, App, HttpResponse, Json, Result}; /// /// #[derive(Deserialize)] /// struct Info { @@ -167,16 +169,15 @@ where /// } /// /// fn main() { -/// let app = App::new().resource( -/// "/index.html", |r| { -/// r.method(http::Method::POST) +/// let app = App::new().resource("/index.html", |r| { +/// r.method(http::Method::POST) /// .with(index) /// .limit(4096) // <- change json extractor configuration /// .error_handler(|err, req| { // <- create custom error response /// error::InternalError::from_response( /// err, HttpResponse::Conflict().finish()).into() /// }); -/// }); +/// }); /// } /// ``` pub struct JsonConfig { @@ -223,15 +224,15 @@ impl Default for JsonConfig { /// # extern crate actix_web; /// # extern crate futures; /// # #[macro_use] extern crate serde_derive; +/// use actix_web::{AsyncResponder, Error, HttpMessage, HttpRequest, HttpResponse}; /// use futures::future::Future; -/// use actix_web::{AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error}; /// /// #[derive(Deserialize, Debug)] /// struct MyObj { /// name: String, /// } /// -/// fn index(mut req: HttpRequest) -> Box> { +/// fn index(mut req: HttpRequest) -> Box> { /// req.json() // <- get JsonBody future /// .from_err() /// .and_then(|val: MyObj| { // <- deserialized value diff --git a/src/middleware/cors.rs b/src/middleware/cors.rs index a7b0110f8..93c8aeb56 100644 --- a/src/middleware/cors.rs +++ b/src/middleware/cors.rs @@ -19,16 +19,16 @@ //! //! ```rust //! # extern crate actix_web; -//! use actix_web::{http, App, HttpRequest, HttpResponse}; //! use actix_web::middleware::cors::Cors; +//! use actix_web::{http, App, HttpRequest, HttpResponse}; //! //! fn index(mut req: HttpRequest) -> &'static str { -//! "Hello world" +//! "Hello world" //! } //! //! fn main() { -//! let app = App::new() -//! .configure(|app| Cors::for_app(app) // <- Construct CORS middleware builder +//! let app = App::new().configure(|app| { +//! Cors::for_app(app) // <- Construct CORS middleware builder //! .allowed_origin("https://www.rust-lang.org/") //! .allowed_methods(vec!["GET", "POST"]) //! .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT]) @@ -38,7 +38,8 @@ //! r.method(http::Method::GET).f(|_| HttpResponse::Ok()); //! r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); //! }) -//! .register()); +//! .register() +//! }); //! } //! ``` //! In this example custom *CORS* middleware get registered for "/index.html" @@ -232,18 +233,20 @@ impl Cors { /// /// ```rust /// # extern crate actix_web; - /// use actix_web::{http, App, HttpResponse}; /// use actix_web::middleware::cors::Cors; + /// use actix_web::{http, App, HttpResponse}; /// /// fn main() { - /// let app = App::new() - /// .configure(|app| Cors::for_app(app) // <- Construct CORS builder + /// let app = App::new().configure( + /// |app| { + /// Cors::for_app(app) // <- Construct CORS builder /// .allowed_origin("https://www.rust-lang.org/") /// .resource("/resource", |r| { // register resource /// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); /// }) - /// .register() // construct CORS and return application instance - /// ); + /// .register() + /// }, // construct CORS and return application instance + /// ); /// } /// ``` pub fn for_app(app: App) -> CorsBuilder { @@ -491,8 +494,8 @@ impl Middleware for Cors { /// ```rust /// # extern crate http; /// # extern crate actix_web; -/// use http::header; /// use actix_web::middleware::cors; +/// use http::header; /// /// # fn main() { /// let cors = cors::Cors::build() @@ -764,12 +767,13 @@ impl CorsBuilder { /// /// ```rust /// # extern crate actix_web; - /// use actix_web::{http, App, HttpResponse}; /// use actix_web::middleware::cors::Cors; + /// use actix_web::{http, App, HttpResponse}; /// /// fn main() { - /// let app = App::new() - /// .configure(|app| Cors::for_app(app) // <- Construct CORS builder + /// let app = App::new().configure( + /// |app| { + /// Cors::for_app(app) // <- Construct CORS builder /// .allowed_origin("https://www.rust-lang.org/") /// .allowed_methods(vec!["GET", "POST"]) /// .allowed_header(http::header::CONTENT_TYPE) @@ -781,8 +785,9 @@ impl CorsBuilder { /// r.method(http::Method::HEAD) /// .f(|_| HttpResponse::MethodNotAllowed()); /// }) - /// .register() // construct CORS and return application instance - /// ); + /// .register() + /// }, // construct CORS and return application instance + /// ); /// } /// ``` pub fn resource(&mut self, path: &str, f: F) -> &mut CorsBuilder diff --git a/src/middleware/csrf.rs b/src/middleware/csrf.rs index 9ff23b530..670ec1c1f 100644 --- a/src/middleware/csrf.rs +++ b/src/middleware/csrf.rs @@ -22,8 +22,8 @@ //! //! ``` //! # extern crate actix_web; -//! use actix_web::{http, App, HttpRequest, HttpResponse}; //! use actix_web::middleware::csrf; +//! use actix_web::{http, App, HttpRequest, HttpResponse}; //! //! fn handle_post(_: HttpRequest) -> &'static str { //! "This action should only be triggered with requests from the same site" @@ -32,8 +32,8 @@ //! fn main() { //! let app = App::new() //! .middleware( -//! csrf::CsrfFilter::new() -//! .allowed_origin("https://www.example.com")) +//! csrf::CsrfFilter::new().allowed_origin("https://www.example.com"), +//! ) //! .resource("/", |r| { //! r.method(http::Method::GET).f(|_| HttpResponse::Ok()); //! r.method(http::Method::POST).f(handle_post); @@ -120,13 +120,12 @@ fn origin(headers: &HeaderMap) -> Option, CsrfError>> { /// # Example /// /// ``` -/// use actix_web::App; /// use actix_web::middleware::csrf; +/// use actix_web::App; /// /// # fn main() { -/// let app = App::new().middleware( -/// csrf::CsrfFilter::new() -/// .allowed_origin("https://www.example.com")); +/// let app = App::new() +/// .middleware(csrf::CsrfFilter::new().allowed_origin("https://www.example.com")); /// # } /// ``` #[derive(Default)] diff --git a/src/middleware/defaultheaders.rs b/src/middleware/defaultheaders.rs index ebe3ea1d4..dca8dfbe1 100644 --- a/src/middleware/defaultheaders.rs +++ b/src/middleware/defaultheaders.rs @@ -17,12 +17,11 @@ use middleware::{Middleware, Response}; /// /// fn main() { /// let app = App::new() -/// .middleware( -/// middleware::DefaultHeaders::new() -/// .header("X-Version", "0.2")) +/// .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2")) /// .resource("/test", |r| { -/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); -/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); +/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); +/// r.method(http::Method::HEAD) +/// .f(|_| HttpResponse::MethodNotAllowed()); /// }) /// .finish(); /// } diff --git a/src/middleware/errhandlers.rs b/src/middleware/errhandlers.rs index 757b38150..42f75a3de 100644 --- a/src/middleware/errhandlers.rs +++ b/src/middleware/errhandlers.rs @@ -18,23 +18,25 @@ type ErrorHandler = Fn(&mut HttpRequest, HttpResponse) -> Result /// /// ```rust /// # extern crate actix_web; +/// use actix_web::middleware::{ErrorHandlers, Response}; /// use actix_web::{http, App, HttpRequest, HttpResponse, Result}; -/// use actix_web::middleware::{Response, ErrorHandlers}; /// /// fn render_500(_: &mut HttpRequest, resp: HttpResponse) -> Result { -/// let mut builder = resp.into_builder(); -/// builder.header(http::header::CONTENT_TYPE, "application/json"); -/// Ok(Response::Done(builder.into())) +/// let mut builder = resp.into_builder(); +/// builder.header(http::header::CONTENT_TYPE, "application/json"); +/// Ok(Response::Done(builder.into())) /// } /// /// fn main() { /// let app = App::new() /// .middleware( /// ErrorHandlers::new() -/// .handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500)) +/// .handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500), +/// ) /// .resource("/test", |r| { -/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); -/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); +/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); +/// r.method(http::Method::HEAD) +/// .f(|_| HttpResponse::MethodNotAllowed()); /// }) /// .finish(); /// } diff --git a/src/middleware/identity.rs b/src/middleware/identity.rs index 54d97a1c8..c8505d686 100644 --- a/src/middleware/identity.rs +++ b/src/middleware/identity.rs @@ -62,8 +62,8 @@ use middleware::{Middleware, Response, Started}; /// The helper trait to obtain your identity from a request. /// /// ```rust -/// use actix_web::*; /// use actix_web::middleware::identity::RequestIdentity; +/// use actix_web::*; /// /// fn index(req: HttpRequest) -> Result { /// // access request identity @@ -80,7 +80,7 @@ use middleware::{Middleware, Response, Started}; /// } /// /// fn logout(mut req: HttpRequest) -> HttpResponse { -/// req.forget(); // <- remove identity +/// req.forget(); // <- remove identity /// HttpResponse::Ok().finish() /// } /// # fn main() {} @@ -144,16 +144,16 @@ pub trait IdentityPolicy: Sized + 'static { /// /// ```rust /// # extern crate actix_web; +/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService}; /// use actix_web::App; -/// use actix_web::middleware::identity::{IdentityService, CookieIdentityPolicy}; /// /// fn main() { -/// let app = App::new().middleware( -/// IdentityService::new( // <- create identity middleware -/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend +/// let app = App::new().middleware(IdentityService::new( +/// // <- create identity middleware +/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend /// .name("auth-cookie") -/// .secure(false)) -/// ); +/// .secure(false), +/// )); /// } /// ``` pub struct IdentityService { @@ -317,17 +317,18 @@ impl CookieIdentityInner { /// /// ```rust /// # extern crate actix_web; +/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService}; /// use actix_web::App; -/// use actix_web::middleware::identity::{IdentityService, CookieIdentityPolicy}; /// /// fn main() { -/// let app = App::new().middleware( -/// IdentityService::new( // <- create identity middleware -/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy +/// let app = App::new().middleware(IdentityService::new( +/// // <- create identity middleware +/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy /// .domain("www.rust-lang.org") /// .name("actix_auth") /// .path("/") -/// .secure(true))); +/// .secure(true), +/// )); /// } /// ``` pub struct CookieIdentityPolicy(Rc); diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index 985a5dfe1..a731d6955 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -31,8 +31,8 @@ use middleware::{Finished, Middleware, Started}; /// ```rust /// # extern crate actix_web; /// extern crate env_logger; -/// use actix_web::App; /// use actix_web::middleware::Logger; +/// use actix_web::App; /// /// fn main() { /// std::env::set_var("RUST_LOG", "actix_web=info"); diff --git a/src/param.rs b/src/param.rs index fd07acf4d..48d1f3a3f 100644 --- a/src/param.rs +++ b/src/param.rs @@ -96,8 +96,8 @@ impl<'a> Params<'a> { /// # extern crate actix_web; /// # use actix_web::*; /// fn index(req: HttpRequest) -> Result { - /// let ivalue: isize = req.match_info().query("val")?; - /// Ok(format!("isuze value: {:?}", ivalue)) + /// let ivalue: isize = req.match_info().query("val")?; + /// Ok(format!("isuze value: {:?}", ivalue)) /// } /// # fn main() {} /// ``` diff --git a/src/pred.rs b/src/pred.rs index 90a0d61f6..206a7941c 100644 --- a/src/pred.rs +++ b/src/pred.rs @@ -22,10 +22,11 @@ pub trait Predicate { /// use actix_web::{pred, App, HttpResponse}; /// /// fn main() { -/// App::new() -/// .resource("/index.html", |r| r.route() +/// App::new().resource("/index.html", |r| { +/// r.route() /// .filter(pred::Any(pred::Get()).or(pred::Post())) -/// .f(|r| HttpResponse::MethodNotAllowed())); +/// .f(|r| HttpResponse::MethodNotAllowed()) +/// }); /// } /// ``` pub fn Any + 'static>(pred: P) -> AnyPredicate { @@ -61,11 +62,14 @@ impl Predicate for AnyPredicate { /// use actix_web::{pred, App, HttpResponse}; /// /// fn main() { -/// App::new() -/// .resource("/index.html", |r| r.route() -/// .filter(pred::All(pred::Get()) -/// .and(pred::Header("content-type", "plain/text"))) -/// .f(|_| HttpResponse::MethodNotAllowed())); +/// App::new().resource("/index.html", |r| { +/// r.route() +/// .filter( +/// pred::All(pred::Get()) +/// .and(pred::Header("content-type", "plain/text")), +/// ) +/// .f(|_| HttpResponse::MethodNotAllowed()) +/// }); /// } /// ``` pub fn All + 'static>(pred: P) -> AllPredicate { diff --git a/src/resource.rs b/src/resource.rs index 7b1a7502d..df6a05f2f 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -24,7 +24,7 @@ use route::Route; /// predicates route route considered matched and route handler get called. /// /// ```rust -/// # extern crate actix_web; +/// //#### # extern crate actix_web; /// use actix_web::{App, HttpResponse, http}; /// /// fn main() { @@ -82,11 +82,12 @@ impl ResourceHandler { /// /// fn main() { /// let app = App::new() - /// .resource( - /// "/", |r| r.route() - /// .filter(pred::Any(pred::Get()).or(pred::Put())) - /// .filter(pred::Header("Content-Type", "text/plain")) - /// .f(|r| HttpResponse::Ok())) + /// .resource("/", |r| { + /// r.route() + /// .filter(pred::Any(pred::Get()).or(pred::Put())) + /// .filter(pred::Header("Content-Type", "text/plain")) + /// .f(|r| HttpResponse::Ok()) + /// }) /// .finish(); /// } /// ``` diff --git a/src/route.rs b/src/route.rs index ff19db802..040baa2d6 100644 --- a/src/route.rs +++ b/src/route.rs @@ -66,13 +66,12 @@ impl Route { /// # extern crate actix_web; /// # use actix_web::*; /// # fn main() { - /// App::new() - /// .resource("/path", |r| - /// r.route() - /// .filter(pred::Get()) - /// .filter(pred::Header("content-type", "text/plain")) - /// .f(|req| HttpResponse::Ok()) - /// ) + /// App::new().resource("/path", |r| { + /// r.route() + /// .filter(pred::Get()) + /// .filter(pred::Header("content-type", "text/plain")) + /// .f(|req| HttpResponse::Ok()) + /// }) /// # .finish(); /// # } /// ``` @@ -115,7 +114,7 @@ impl Route { /// # extern crate actix_web; /// # extern crate futures; /// #[macro_use] extern crate serde_derive; - /// use actix_web::{App, Path, Result, http}; + /// use actix_web::{http, App, Path, Result}; /// /// #[derive(Deserialize)] /// struct Info { @@ -129,8 +128,9 @@ impl Route { /// /// fn main() { /// let app = App::new().resource( - /// "/{username}/index.html", // <- define path parameters - /// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor + /// "/{username}/index.html", // <- define path parameters + /// |r| r.method(http::Method::GET).with(index), + /// ); // <- use `with` extractor /// } /// ``` /// @@ -143,7 +143,7 @@ impl Route { /// # extern crate futures; /// #[macro_use] extern crate serde_derive; /// # use std::collections::HashMap; - /// use actix_web::{http, App, Query, Path, Result, Json}; + /// use actix_web::{http, App, Json, Path, Query, Result}; /// /// #[derive(Deserialize)] /// struct Info { @@ -151,14 +151,17 @@ impl Route { /// } /// /// /// extract path info using serde - /// fn index(info: (Path, Query>, Json)) -> Result { + /// fn index( + /// info: (Path, Query>, Json), + /// ) -> Result { /// Ok(format!("Welcome {}!", info.0.username)) /// } /// /// fn main() { /// let app = App::new().resource( - /// "/{username}/index.html", // <- define path parameters - /// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor + /// "/{username}/index.html", // <- define path parameters + /// |r| r.method(http::Method::GET).with(index), + /// ); // <- use `with` extractor /// } /// ``` pub fn with(&mut self, handler: F) -> ExtractorConfig @@ -181,7 +184,7 @@ impl Route { /// # extern crate actix_web; /// # extern crate futures; /// #[macro_use] extern crate serde_derive; - /// use actix_web::{App, Path, Error, http}; + /// use actix_web::{http, App, Error, Path}; /// use futures::Future; /// /// #[derive(Deserialize)] @@ -190,15 +193,15 @@ impl Route { /// } /// /// /// extract path info using serde - /// fn index(info: Path) -> Box> { + /// fn index(info: Path) -> Box> { /// unimplemented!() /// } /// /// fn main() { /// let app = App::new().resource( - /// "/{username}/index.html", // <- define path parameters - /// |r| r.method(http::Method::GET) - /// .with_async(index)); // <- use `with` extractor + /// "/{username}/index.html", // <- define path parameters + /// |r| r.method(http::Method::GET).with_async(index), + /// ); // <- use `with` extractor /// } /// ``` pub fn with_async(&mut self, handler: F) -> ExtractorConfig @@ -222,7 +225,7 @@ impl Route { /// # extern crate actix_web; /// # extern crate futures; /// #[macro_use] extern crate serde_derive; - /// use actix_web::{App, Query, Path, Result, http}; + /// use actix_web::{http, App, Path, Query, Result}; /// /// #[derive(Deserialize)] /// struct PParam { @@ -241,8 +244,9 @@ impl Route { /// /// fn main() { /// let app = App::new().resource( - /// "/{username}/index.html", // <- define path parameters - /// |r| r.method(http::Method::GET).with2(index)); // <- use `with` extractor + /// "/{username}/index.html", // <- define path parameters + /// |r| r.method(http::Method::GET).with2(index), + /// ); // <- use `with` extractor /// } /// ``` pub fn with2( diff --git a/src/scope.rs b/src/scope.rs index dba490b2f..9452191f5 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -38,12 +38,12 @@ type NestedInfo = (Resource, Route, Vec>>); /// use actix_web::{http, App, HttpRequest, HttpResponse}; /// /// fn main() { -/// let app = App::new() -/// .scope("/{project_id}/", |scope| { -/// scope.resource("/path1", |r| r.f(|_| HttpResponse::Ok())) -/// .resource("/path2", |r| r.f(|_| HttpResponse::Ok())) -/// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed())) -/// }); +/// let app = App::new().scope("/{project_id}/", |scope| { +/// scope +/// .resource("/path1", |r| r.f(|_| HttpResponse::Ok())) +/// .resource("/path2", |r| r.f(|_| HttpResponse::Ok())) +/// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed())) +/// }); /// } /// ``` /// @@ -89,13 +89,14 @@ impl Scope { /// } /// /// fn main() { - /// let app = App::new() - /// .scope("/app", |scope| { - /// scope.filter(pred::Header("content-type", "text/plain")) - /// .route("/test1", http::Method::GET, index) - /// .route("/test2", http::Method::POST, - /// |_: HttpRequest| HttpResponse::MethodNotAllowed()) - /// }); + /// let app = App::new().scope("/app", |scope| { + /// scope + /// .filter(pred::Header("content-type", "text/plain")) + /// .route("/test1", http::Method::GET, index) + /// .route("/test2", http::Method::POST, |_: HttpRequest| { + /// HttpResponse::MethodNotAllowed() + /// }) + /// }); /// } /// ``` pub fn filter + 'static>(mut self, p: T) -> Self { @@ -116,12 +117,11 @@ impl Scope { /// } /// /// fn main() { - /// let app = App::new() - /// .scope("/app", |scope| { - /// scope.with_state("/state2", AppState, |scope| { - /// scope.resource("/test1", |r| r.f(index)) - /// }) - /// }); + /// let app = App::new().scope("/app", |scope| { + /// scope.with_state("/state2", AppState, |scope| { + /// scope.resource("/test1", |r| r.f(index)) + /// }) + /// }); /// } /// ``` pub fn with_state(mut self, path: &str, state: T, f: F) -> Scope @@ -162,12 +162,9 @@ impl Scope { /// } /// /// fn main() { - /// let app = App::with_state(AppState) - /// .scope("/app", |scope| { - /// scope.nested("/v1", |scope| { - /// scope.resource("/test1", |r| r.f(index)) - /// }) - /// }); + /// let app = App::with_state(AppState).scope("/app", |scope| { + /// scope.nested("/v1", |scope| scope.resource("/test1", |r| r.f(index))) + /// }); /// } /// ``` pub fn nested(mut self, path: &str, f: F) -> Scope @@ -211,12 +208,13 @@ impl Scope { /// } /// /// fn main() { - /// let app = App::new() - /// .scope("/app", |scope| { - /// scope.route("/test1", http::Method::GET, index) - /// .route("/test2", http::Method::POST, - /// |_: HttpRequest| HttpResponse::MethodNotAllowed()) - /// }); + /// let app = App::new().scope("/app", |scope| { + /// scope.route("/test1", http::Method::GET, index).route( + /// "/test2", + /// http::Method::POST, + /// |_: HttpRequest| HttpResponse::MethodNotAllowed(), + /// ) + /// }); /// } /// ``` pub fn route(mut self, path: &str, method: Method, f: F) -> Scope @@ -261,17 +259,16 @@ impl Scope { /// use actix_web::*; /// /// fn main() { - /// let app = App::new() - /// .scope("/api", |scope| { - /// scope.resource("/users/{userid}/{friend}", |r| { - /// r.get().f(|_| HttpResponse::Ok()); - /// r.head().f(|_| HttpResponse::MethodNotAllowed()); - /// r.route() - /// .filter(pred::Any(pred::Get()).or(pred::Put())) - /// .filter(pred::Header("Content-Type", "text/plain")) - /// .f(|_| HttpResponse::Ok()) - /// }) - /// }); + /// let app = App::new().scope("/api", |scope| { + /// scope.resource("/users/{userid}/{friend}", |r| { + /// r.get().f(|_| HttpResponse::Ok()); + /// r.head().f(|_| HttpResponse::MethodNotAllowed()); + /// r.route() + /// .filter(pred::Any(pred::Get()).or(pred::Put())) + /// .filter(pred::Header("Content-Type", "text/plain")) + /// .f(|_| HttpResponse::Ok()) + /// }) + /// }); /// } /// ``` pub fn resource(mut self, path: &str, f: F) -> Scope diff --git a/src/with.rs b/src/with.rs index ea549e31d..f7d75e5d7 100644 --- a/src/with.rs +++ b/src/with.rs @@ -19,7 +19,7 @@ use httpresponse::HttpResponse; /// ```rust /// # extern crate actix_web; /// #[macro_use] extern crate serde_derive; -/// use actix_web::{App, Form, Result, http}; +/// use actix_web::{http, App, Form, Result}; /// /// #[derive(Deserialize)] /// struct FormData { @@ -32,10 +32,10 @@ use httpresponse::HttpResponse; /// /// fn main() { /// let app = App::new().resource( -/// "/index.html", |r| { -/// r.method(http::Method::GET) -/// .with(index) -/// .limit(4096);} // <- change form extractor configuration +/// "/index.html", +/// |r| { +/// r.method(http::Method::GET).with(index).limit(4096); +/// }, // <- change form extractor configuration /// ); /// } /// ``` @@ -45,7 +45,7 @@ use httpresponse::HttpResponse; /// ```rust /// # extern crate actix_web; /// #[macro_use] extern crate serde_derive; -/// use actix_web::{App, Form, Path, Result, http}; +/// use actix_web::{http, App, Form, Path, Result}; /// /// #[derive(Deserialize)] /// struct FormData { @@ -58,10 +58,10 @@ use httpresponse::HttpResponse; /// /// fn main() { /// let app = App::new().resource( -/// "/index.html", |r| { -/// r.method(http::Method::GET) -/// .with(index) -/// .1.limit(4096);} // <- change form extractor configuration +/// "/index.html", +/// |r| { +/// r.method(http::Method::GET).with(index).1.limit(4096); +/// }, // <- change form extractor configuration /// ); /// } /// ```