rocket_codegen 0.3.9

Code generation for the Rocket web framework.
docs.rs failed to build rocket_codegen-0.3.9
Please check build logs and if you believe this is docs.rs' fault, report into this issue report.

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.

Custom Attributes

This crate implements the following custom attributes:

  • route
  • get
  • put
  • post
  • delete
  • head
  • patch
  • options
  • error

The grammar for all route attributes, including route, get, put, post, delete, head, patch, and options is defined as:

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 error attribute is:

A use of the error attribute looks like:

#[error(404)]

Custom Derives

This crate implements the following custom derives:

  • FromForm

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:

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 attribute's field name is used instead of the structure's field name when parsing a form.

Procedural Macros

This crate implements the following procedural macros:

  • routes
  • errors

The syntax for both of these is defined as:

Debugging Codegen

When the ROCKET_CODEGEN_DEBUG environment variable is set, this crate logs the items it has generated to the console at compile-time. For example, you might run the following to build a Rocket application with codegen logging enabled:

ROCKET_CODEGEN_DEBUG=1 cargo build