2019-06-13 01:48:02 +00:00
|
|
|
use rocket;
|
2019-02-02 18:36:23 +00:00
|
|
|
|
|
|
|
use rocket::{get, routes};
|
|
|
|
use rocket::request::{Form, FromForm, FromFormValue};
|
|
|
|
use rocket::response::Responder;
|
|
|
|
|
|
|
|
#[derive(FromFormValue)]
|
|
|
|
enum Thing {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Thing {
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2019-02-02 18:36:23 +00:00
|
|
|
match *self {
|
|
|
|
Thing::A => write!(f, "a"),
|
|
|
|
Thing::B => write!(f, "b"),
|
|
|
|
Thing::C => write!(f, "c"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct ThingForm {
|
|
|
|
thing: Thing,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
struct DerivedResponder {
|
|
|
|
data: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn index() -> DerivedResponder {
|
|
|
|
DerivedResponder { data: "hello".to_string() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/?<params..>")]
|
|
|
|
fn number(params: Form<ThingForm>) -> DerivedResponder {
|
|
|
|
DerivedResponder { data: params.thing.to_string() }
|
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_derive_reexports() {
|
|
|
|
use rocket::local::blocking::Client;
|
2019-02-02 18:36:23 +00:00
|
|
|
|
|
|
|
let rocket = rocket::ignite().mount("/", routes![index, number]);
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket).unwrap();
|
2019-02-02 18:36:23 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "hello");
|
2019-02-02 18:36:23 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/?thing=b").dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "b");
|
2019-02-02 18:36:23 +00:00
|
|
|
}
|