Refactor the guide for 0.3

This commit is contained in:
Lori Holden 2017-06-06 13:23:46 -04:00
parent 3eeae77ed6
commit d0fa48d92d
8 changed files with 52 additions and 29 deletions

View File

@ -22,7 +22,7 @@ guide.
The best way to learn Rocket is to _build something_. It should be fun and easy,
and there's always someone to help. Alternatively, you can read through the
[Rocket examples](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples)
[Rocket examples](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples)
or the [Rocket source
code](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/lib/src). Whatever you
code](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/lib/src). Whatever you
decide to do next, we hope you have a blast!

View File

@ -53,8 +53,8 @@ project by ensuring your `Cargo.toml` contains the following:
```
[dependencies]
rocket = "0.2.8"
rocket_codegen = "0.2.8"
rocket = "0.3.0"
rocket_codegen = "0.3.0"
```
Modify `src/main.rs` so that it contains the code for the Rocket `Hello, world!`

View File

@ -87,7 +87,7 @@ For instance, to mount the `world` route we declared above, we would use the
following code:
```rust
rocket::ignite().mount("/hello", routes![world])
rocket::ignite().mount("/hello", routes![world]);
```
Altogether, this creates a new `Rocket` instance via the `ignite` function and
@ -111,7 +111,7 @@ use other::world;
fn main() {
// error[E0425]: cannot find value `static_rocket_route_info_for_world` in this scope
rocket::ignite().mount("/hello", routes![world])
rocket::ignite().mount("/hello", routes![world]);
}
```
@ -120,7 +120,7 @@ into the name of a structure generated by Rocket's code generation. The solution
is to name the route by a module path instead:
```rust
rocket::ignite().mount("/hello", routes![other::world])
rocket::ignite().mount("/hello", routes![other::world]);
```
## Launching
@ -172,10 +172,10 @@ we expected.
A version of this example's complete crate, ready to `cargo run`, can be found
on
[GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/hello_world).
[GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/hello_world).
You can find dozens of other complete examples, spanning all of Rocket's
features, in the [GitHub examples
directory](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/).
directory](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/).
## Configuration

View File

@ -211,6 +211,7 @@ use std::io;
use std::path::Path;
use rocket::Data;
use rocket::http::RawStr;
```
The [Data](https://api.rocket.rs/rocket/data/struct.Data.html) structure is key
@ -297,7 +298,7 @@ paste doesn't exist.
use std::fs::File;
#[get("/<id>")]
fn retrieve(id: &str) -> Option<File> {
fn retrieve(id: &RawStr) -> Option<File> {
let filename = format!("upload/{id}", id = id);
File::open(&filename).ok()
}
@ -326,7 +327,7 @@ using it. We do this by implementing `FromParam` for `PasteID` in
use rocket::request::FromParam;
/// Returns `true` if `id` is a valid paste ID and `false` otherwise.
fn valid_id(id: &str) -> bool {
fn valid_id(id: &RawStr) -> bool {
id.chars().all(|c| {
(c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
@ -402,4 +403,4 @@ through some of them to get a better feel for Rocket. Here are some ideas:
You can find the full source code for the completed pastebin tutorial in the
[Rocket Github
Repo](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/pastebin).
Repo](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/pastebin).

View File

@ -31,7 +31,7 @@ requests under certain conditions. If a `POST` request contains a body of
field has the name `_method` and a valid HTTP method as its value, that field's
value is used as the method for the incoming request. This allows Rocket
applications to submit non-`POST` forms. The [todo
example](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/todo/static/index.html.tera#L47)
example](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/todo/static/index.html.tera#L47)
makes use of this feature to submit `PUT` and `DELETE` requests from a web form.
## Format
@ -57,7 +57,7 @@ not just the world, we could declare a route and handler like so:
```rust
#[get("/hello/<name>")]
fn hello(name: &str) -> String {
fn hello(name: String) -> String {
format!("Hello, {}!", name)
}
```
@ -76,7 +76,7 @@ illustrate varied usage:
```rust
#[get("/hello/<name>/<age>/<cool>")]
fn hello(name: &str, age: u8, cool: bool) -> String {
fn hello(name: String, age: u8, cool: bool) -> String {
if cool {
format!("You're a cool {} year old, {}!", age, name)
} else {
@ -85,6 +85,19 @@ fn hello(name: &str, age: u8, cool: bool) -> String {
}
```
## Raw Strings
Rocket provides the `RawStr` type for handling raw strings. When used as a path
segment, it is passed directly with no modification. This is used instead of
the `&str` type.
```rust
#[get("/hello/<name>")]
fn hello(name: &RawStr) -> String {
format!("Hello, {}!", name)
}
```
## Forwarding
In this example above, what if `cool` isn't a `bool`? Or, what if `age` isn't a
@ -107,7 +120,7 @@ fn user(id: usize) -> T { ... }
fn user_int(id: isize) -> T { ... }
#[get("/user/<id>", rank = 3)]
fn user_str(id: &str) -> T { ... }
fn user_str(id: &RawStr) -> T { ... }
```
Notice the `rank` parameters in `user_int` and `user_str`. If we run this
@ -125,7 +138,7 @@ be routed as follows:
route always matches. The `user_str` handler is called.
Forwards can be _caught_ by using a `Result` or `Option` type. For example, if
the type of `id` in the `user` function was `Result<usize, &str>`, then `user`
the type of `id` in the `user` function was `Result<usize, &RawStr>`, then `user`
would never forward. An `Ok` variant would indicate that `<id>` was a valid
`usize`, while an `Err` would indicate that `<id>` was not a `usize`. The
`Err`'s value would contain the string that failed to parse as a `usize`.
@ -185,7 +198,7 @@ fn index(cookies: &Cookies, content: ContentType) -> String { ... }
```
The [cookies example on
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/cookies)
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/cookies)
illustrates how to use the `Cookies` type to get and set cookies.
You can implement `FromRequest` for your own types. For instance, to protect a
@ -259,9 +272,9 @@ validates integers over that age. If a form is submitted with a bad age,
Rocket won't call a handler requiring a valid form for that structure. You can
use `Option` or `Result` types for fields to catch parse failures.
The [forms](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/forms)
The [forms](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/forms)
and [forms kitchen
sink](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/form_kitchen_sink)
sink](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/form_kitchen_sink)
examples on GitHub provide further illustrations.
### JSON
@ -282,7 +295,7 @@ fn new(task: JSON<Task>) -> String { ... }
The only condition is that the generic type to `JSON` implements the
`Deserialize` trait. See the [JSON example on
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/json) for a
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/json) for a
complete example.
### Streaming
@ -305,7 +318,7 @@ The route above accepts any `POST` request to the `/upload` path with
text response if the upload succeeds. If the upload fails, an error response is
returned. The handler above is complete. It really is that simple! See the
[GitHub example
code](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/raw_upload)
code](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/raw_upload)
for the full crate.
## Query Strings
@ -341,7 +354,7 @@ the request is forwarded to the next matching route. To catch parse failures,
you can use `Option` or `Result` as the type of the field to catch errors for.
See [the GitHub
example](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/query_params)
example](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/query_params)
for a complete illustration.
## Error Catchers
@ -378,7 +391,7 @@ types [Request](https://api.rocket.rs/rocket/struct.Request.html) and/or
[Error](https://api.rocket.rs/rocket/enum.Error.html). At present, the `Error`
type is not particularly useful, and so it is often omitted. The
[error catcher
example](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/errors) on
example](https://github.com/SergioBenitez/Rocket/tree/v0.3.3/examples/errors) on
GitHub illustrates their use in full.
Rocket has a default catcher for all of the standard HTTP error codes including

View File

@ -127,12 +127,14 @@ serialization in a fixed-sized body. If serialization fails, the request is
forwarded to the **500** error catcher.
For a complete example, see the [JSON example on
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/json).
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/json).
## Templates
Rocket has built-in support for templating. To respond with a rendered template,
simply return a
ensure that you are using
[`Template::fairing()`](https://api.rocket.rs/rocket_contrib/struct.Template.html#method.fairing)
and then simply return a
[Template](https://api.rocket.rs/rocket_contrib/struct.Template.html) type.
```rust
@ -141,6 +143,13 @@ fn index() -> Template {
let context = /* object-like value */;
Template::render("index", &context)
}
fn main() {
rocket::ignite()
.mount("/", routes![index])
.attach(Template::fairing())
.launch();
}
```
Templates are rendered with the `render` method. The method takes in the name of
@ -156,7 +165,7 @@ The context can be any type that implements `Serialize` and serializes to an
[Template](https://api.rocket.rs/rocket_contrib/struct.Template.html) API
documentation contains more information about templates, while the [Handlebars
Templates example on
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/handlebars_templates)
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/handlebars_templates)
is a fully composed application that makes use of Handlebars templates.
## Streaming

View File

@ -138,7 +138,7 @@ attributes.
You can find a complete example using the `HitCounter` structure in the [state
example on
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/state) and
GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/state) and
learn more about the [manage
method](https://api.rocket.rs/rocket/struct.Rocket.html#method.manage) and
[State type](https://api.rocket.rs/rocket/struct.State.html) in the API docs.

View File

@ -107,7 +107,7 @@ assert_eq!(body_str, Some("Hello, world!".to_string()));
That's it! Run the tests with `cargo test`. The complete application, with
testing, can be found in the [GitHub testing
example](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/testing).
example](https://github.com/SergioBenitez/Rocket/tree/v0.3.0/examples/testing).
## Codegen Debug