#![feature(proc_macro_diagnostic, proc_macro_span)] #![feature(crate_visibility_modifier)] #![feature(transpose_result)] #![feature(rustc_private)] #![recursion_limit="128"] //! # Rocket - Code Generation //! //! This crate implements the code generation portions of Rocket. This includes //! custom derives, custom attributes, and procedural macros. The documentation //! here is purely technical. The code generation facilities are documented //! thoroughly in the [Rocket programming guide](https://rocket.rs/guide). //! //! ## **Table of Contents** //! //! 1. [Custom Attributes](#custom-attributes) //! 2. [Custom Derives](#custom-derives) //! * [`FromForm`](#fromform) //! * [`FromFormValue`](#fromformvalue) //! * [`Responder`](#responder) //! 3. [Procedural Macros](#procedural-macros) //! 4. [Debugging Generated Code](#debugging-codegen) //! //! ## Custom Attributes //! //! This crate implements the following custom attributes: //! //! * **route** //! * **get** //! * **put** //! * **post** //! * **delete** //! * **head** //! * **patch** //! * **options** //! * **catch** //! //! The grammar for all _route_ attributes, including **route**, **get**, //! **put**, **post**, **delete**, **head**, **patch**, and **options** is //! defined as: //! //!
//! route := METHOD? '(' ('path' '=')? path (',' kv_param)* ')' //! //! path := URI_SEG //! | DYNAMIC_PARAM //! | '?' DYNAMIC_PARAM //! | path '/' path //! (string literal) //! //! kv_param := 'rank' '=' INTEGER //! | 'format' '=' STRING //! | 'data' '=' DYNAMIC_PARAM //! //! INTEGER := isize, as defined by Rust //! STRING := UTF-8 string literal, as defined by Rust //! IDENT := valid identifier, as defined by Rust //! //! URI_SEG := valid HTTP URI Segment //! DYNAMIC_PARAM := '<' IDENT '..'? '>' (string literal) //!//! //! Note that the **route** attribute takes a method as its first argument, //! while the remaining do not. That is, **route** looks like: //! //! #[route(GET, path = "/hello")] //! //! while the equivalent using **get** looks like: //! //! #[get("/hello")] //! //! The syntax for the **catch** attribute is: //! //!
//! catch := INTEGER //!//! //! A use of the `catch` attribute looks like: //! //! #[catch(404)] //! //! ## Custom Derives //! //! This crate* implements the following custom derives: //! //! * **FromForm** //! * **FromFormValue** //! * **Responder** //! //! * In reality, all of these custom derives are currently implemented //! by the `rocket_codegen_next` crate. Nonetheless, they are documented //! here. //! ### `FromForm` //! //! The [`FromForm`] derive can be applied to structures with named fields: //! //! #[derive(FromForm)] //! struct MyStruct { //! field: usize, //! other: String //! } //! //! Each field's type is required to implement [`FromFormValue`]. //! //! The derive accepts one field attribute: `form`, with the following syntax: //! //!
//! form := 'field' '=' '"' IDENT '"' //! //! IDENT := valid identifier, as defined by Rust //!//! //! When applied, the attribute looks as follows: //! //! #[derive(FromForm)] //! struct MyStruct { //! field: usize, //! #[form(field = "renamed_field")] //! other: String //! } //! //! The derive generates an implementation for the [`FromForm`] trait. The //! implementation parses a form whose field names match the field names of the //! structure on which the derive was applied. Each field's value is parsed with //! the [`FromFormValue`] implementation of the field's type. The `FromForm` //! implementation succeeds only when all of the field parses succeed. //! //! The `form` field attribute can be used to direct that a different incoming //! field name is expected. In this case, the `field` name in the attribute is //! used instead of the structure's actual field name when parsing a form. //! //! [`FromForm`]: /rocket/request/trait.FromForm.html //! [`FromFormValue`]: /rocket/request/trait.FromFormValue.html //! //! ### `FromFormValue` //! //! The [`FromFormValue`] derive can be applied to enums with nullary //! (zero-length) fields: //! //! #[derive(FromFormValue)] //! enum MyValue { //! First, //! Second, //! Third, //! } //! //! The derive generates an implementation of the [`FromFormValue`] trait for //! the decorated `enum`. The implementation returns successfully when the form //! value matches, case insensitively, the stringified version of a variant's //! name, returning an instance of said variant. //! //! As an example, for the `enum` above, the form values `"first"`, `"FIRST"`, //! `"fiRSt"`, and so on would parse as `MyValue::First`, while `"second"` and //! `"third"` would parse as `MyValue::Second` and `MyValue::Third`, //! respectively. //! //! The `form` field attribute can be used to change the string that is compared //! against for a given variant: //! //! #[derive(FromFormValue)] //! enum MyValue { //! First, //! Second, //! #[form(value = "fourth")] //! Third, //! } //! //! The attribute's grammar is: //! //!
//! form := 'field' '=' STRING_LIT //! //! STRING_LIT := any valid string literal, as defined by Rust //!//! //! The attribute accepts a single string parameter of name `value` //! corresponding to the string to use to match against for the decorated //! variant. In the example above, the the strings `"fourth"`, `"FOUrth"` and so //! on would parse as `MyValue::Third`. //! //! ## `Responder` //! //! The [`Responder`] derive can be applied to enums and named structs. When //! applied to enums, variants must have at least one field. When applied to //! structs, the struct must have at least one field. //! //! #[derive(Responder)] //! enum MyResponder { //! A(String), //! B(OtherResponse, ContentType), //! } //! //! #[derive(Responder)] //! struct MyResponder { //! inner: OtherResponder, //! header: ContentType, //! } //! //! The derive generates an implementation of the [`Responder`] trait for the //! decorated enum or structure. The derive uses the _first_ field of a variant //! or structure to generate a `Response`. As such, the type of the first field //! must implement [`Responder`]. The remaining fields of a variant or structure //! are set as headers in the produced [`Response`] using //! [`Response::set_header()`]. As such, every other field (unless explicitly //! ignored, explained next) must implement `Into
//! response := parameter (',' parameter)? //! //! parameter := 'status' '=' STATUS //! | 'content_type' '=' CONTENT_TYPE //! //! STATUS := unsigned integer >= 100 and < 600 //! CONTENT_TYPE := string literal, as defined by Rust, identifying a valid //! Content-Type, as defined by Rocket //!//! //! It can be used as follows: //! //! #[derive(Responder)] //! enum Error { //! #[response(status = 500, content_type = "json")] //! A(String), //! #[response(status = 404)] //! B(OtherResponse, ContentType), //! } //! //! #[derive(Responder)] //! #[response(status = 400)] //! struct MyResponder { //! inner: InnerResponder, //! header: ContentType, //! #[response(ignore)] //! other: Other, //! } //! //! The attribute accepts two key/value pairs: `status` and `content_type`. The //! value of `status` must be an unsigned integer representing a valid status //! code. The [`Response`] produced from the generated implementation will have //! its status overriden to this value. //! //! The value of `content_type` must be a valid media-type in `top/sub` form or //! `shorthand` form. Examples include: //! //! * `"text/html"` //! * `"application/x-custom"` //! * `"html"` //! * `"json"` //! * `"plain"` //! * `"binary"` //! //! The [`Response`] produced from the generated implementation will have its //! content-type overriden to this value. //! //! [`Responder`]: /rocket/response/trait.Responder.html //! [`Response`]: /rocket/struct.Response.html //! [`Response::set_header()`]: /rocket/struct.Response.html#method.set_header //! //! ## Procedural Macros //! //! This crate implements the following procedural macros: //! //! * **routes** //! * **catchers** //! * **uri** //! //! The syntax for `routes!` and `catchers!` is defined as: //! //!
//! macro := PATH (',' PATH)* //! //! PATH := a path, as defined by Rust //!//! //! ### Typed URIs: `uri!` //! //! The `uri!` macro creates a type-safe URI given a route and values for the //! route's URI parameters. The inputs to the macro are the path to a route, a //! colon, and one argument for each dynamic parameter (parameters in `<>`) in //! the route's path. //! //! For example, for the following route: //! //! ```rust,ignore //! #[get("/person/
//! uri := (mount ',')? PATH (':' params)? //! //! mount = STRING //! params := unnamed | named //! unnamed := EXPR (',' EXPR)* //! named := IDENT = EXPR (',' named)? //! //! EXPR := a valid Rust expression (examples: `foo()`, `12`, `"hey"`) //! IDENT := a valid Rust identifier (examples: `name`, `age`) //! STRING := an uncooked string literal, as defined by Rust (example: `"hi"`) //! PATH := a path, as defined by Rust (examples: `route`, `my_mod::route`) //!//! //! #### Semantics //! //! The `uri!` macro returns an [`Origin`](rocket::uri::Origin) structure with //! the URI of the supplied route interpolated with the given values. Note that //! `Origin` implements `Into