rocket

Derive Macro UriDisplayQuery

Source
#[derive(UriDisplayQuery)]
{
    // Attributes available to this derive:
    #[form]
}
Expand description

Derive for the UriDisplay<Query> trait.

The UriDisplay<Query> derive can be applied to enums and 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(UriDisplayQuery)]
enum Kind {
    A(String),
    B(usize),
}

#[derive(UriDisplayQuery)]
struct MyStruct {
    name: String,
    id: usize,
    kind: Kind,
}

Each field’s type is required to implement UriDisplay<Query>.

The derive generates an implementation of the UriDisplay<Query> trait. The implementation calls Formatter::write_named_value() for every named field, using the field’s name (unless overriden, explained next) as the name parameter, and Formatter::write_value() for every unnamed field in the order the fields are declared.

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(UriDisplayQuery)]
struct MyStruct {
    name: String,
    id: usize,
    #[form(field = "type")]
    kind: Kind,
}

The field attribute directs that a different field name be used when calling Formatter::write_named_value() for the given field. The value of the field attribute is used instead of the structure’s actual field name. In the example above, the field MyStruct::kind is rendered with a name of type.