1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

clippy warnings; extend url_for example in user guide

This commit is contained in:
Nikolay Kim 2018-04-09 13:30:38 -07:00
parent 458e6bdcc2
commit 7df2d6b12a
3 changed files with 36 additions and 30 deletions

View File

@ -350,10 +350,33 @@ List of `FromParam` implementations can be found in
## Path information extractor ## Path information extractor
Actix provides functionality for type safe request path information extraction. Actix provides functionality for type safe path information extraction.
It uses *serde* package as a deserialization library. [Path](../actix_web/struct.Path.html) extracts information, destination type
[Path](../actix_web/struct.Path.html) extracts information, the destination type could be defined in several different forms. Simplest approach is to use
has to implement *serde's *`Deserialize` trait. `tuple` type. Each element in tuple must correpond to one element from
path pattern. i.e. you can match path pattern `/{id}/{username}/` against
`Pyth<(u32, String)>` type, but `Path<(String, String, String)>` type will
always fail.
```rust
# extern crate actix_web;
use actix_web::{App, Path, Result, http::Method};
// extract path info using serde
fn index(info: Path<(String, u32)>) -> Result<String> {
Ok(format!("Welcome {}! id: {}", info.0, info.1))
}
fn main() {
let app = App::new()
.resource("/{username}/{id}/index.html", // <- define path parameters
|r| r.method(Method::GET).with(index));
}
```
It also possible to extract path pattern information to a struct. In this case,
this struct must implement *serde's *`Deserialize` trait.
```rust ```rust
# extern crate actix_web; # extern crate actix_web;
@ -377,27 +400,6 @@ fn main() {
} }
``` ```
It also possible to extract path information to a tuple. In this case, you don't need
to define an extra type; use a tuple as a `Path` generic type.
Here is previous example re-written using tuple instead of specific type.
```rust
# extern crate actix_web;
use actix_web::{App, Path, Result, http::Method};
// extract path info using serde
fn index(info: Path<(String, u32)>) -> Result<String> {
Ok(format!("Welcome {}! id: {}", info.0, info.1))
}
fn main() {
let app = App::new()
.resource("/{username}/{id}/index.html", // <- define path parameters
|r| r.method(Method::GET).with(index));
}
```
[Query](../actix_web/struct.Query.html) provides similar functionality for [Query](../actix_web/struct.Query.html) provides similar functionality for
request query parameters. request query parameters.
@ -410,11 +412,13 @@ resource with the name "foo" and the pattern "{a}/{b}/{c}", you might do this:
```rust ```rust
# extern crate actix_web; # extern crate actix_web;
# use actix_web::{App, HttpRequest, HttpResponse, http::Method}; # use actix_web::{App, Result, HttpRequest, HttpResponse, http::Method, http::header};
# #
fn index(req: HttpRequest) -> HttpResponse { fn index(req: HttpRequest) -> Result<HttpResponse> {
let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
HttpResponse::Ok().into() Ok(HttpResponse::Found()
.header(header::LOCATION, url.as_str())
.finish())
} }
fn main() { fn main() {
@ -423,6 +427,7 @@ fn main() {
r.name("foo"); // <- set resource name, then it could be used in `url_for` r.name("foo"); // <- set resource name, then it could be used in `url_for`
r.method(Method::GET).f(|_| HttpResponse::Ok()); r.method(Method::GET).f(|_| HttpResponse::Ok());
}) })
.route("/test/", Method::GET, index)
.finish(); .finish();
} }
``` ```

View File

@ -131,6 +131,7 @@ fn origin(headers: &HeaderMap) -> Option<Result<Cow<str>, CsrfError>> {
/// .allowed_origin("https://www.example.com")); /// .allowed_origin("https://www.example.com"));
/// # } /// # }
/// ``` /// ```
#[derive(Default)]
pub struct CsrfFilter { pub struct CsrfFilter {
origins: HashSet<String>, origins: HashSet<String>,
allow_xhr: bool, allow_xhr: bool,

View File

@ -276,8 +276,8 @@ impl CookieSessionInner {
fn new(key: &[u8], security: CookieSecurity) -> CookieSessionInner { fn new(key: &[u8], security: CookieSecurity) -> CookieSessionInner {
CookieSessionInner { CookieSessionInner {
security,
key: Key::from_master(key), key: Key::from_master(key),
security: security,
name: "actix-session".to_owned(), name: "actix-session".to_owned(),
path: "/".to_owned(), path: "/".to_owned(),
domain: None, domain: None,