macro_rules! accept {
($request:expr, $($mime:expr => $val:expr),+ $(,)*) => { ... };
}Expand description
Dispatches between blocks depending on the value of the Accept header.
This macro takes as first parameter the request object, and then each additional parameter must
be of the form mime => value where mime is a MIME type in quotes and value is an
expression of any type.
The macro returns the value corresponding to the MIME type that has the highest priority in
the request’s Accept header. If multiple MIME types have the same priority, the earliest in
the list passed to the macro is chosen. If no MIME matches the request, the first in the list
is chosen. If there is no Accept header in the request, it is as if the header’s value
was */*.
You can also use * in the MIME types you pass to the macro. The MIME */* can be used as a
default handler.
Note: Using
|just like in real match expressions is not yet supported because the author didn’t find a way to make it work with Rust macros.
§Basic example
use rouille::Request;
use rouille::Response;
fn handle(request: &Request) -> Response {
accept!(request,
"text/html" => Response::html("<p>Hello world</p>"),
"text/plain" => Response::text("Hello world"),
)
}§Example with a default handler
use rouille::Request;
use rouille::Response;
fn handle(request: &Request) -> Response {
accept!(request,
"text/html" => Response::html("<p>Hello world</p>"),
"text/plain" => Response::text("Hello world"),
"*/*" => Response::empty_406()
)
}