2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2019-02-02 18:36:23 +00:00
|
|
|
|
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() }
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_derive_reexports() {
|
2019-02-02 18:36:23 +00:00
|
|
|
use rocket::local::Client;
|
|
|
|
|
|
|
|
let rocket = rocket::ignite().mount("/", routes![index, number]);
|
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.get("/").dispatch().await;
|
|
|
|
assert_eq!(response.body_string().await.unwrap(), "hello");
|
2019-02-02 18:36:23 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.get("/?thing=b").dispatch().await;
|
|
|
|
assert_eq!(response.body_string().await.unwrap(), "b");
|
2019-02-02 18:36:23 +00:00
|
|
|
}
|