From 6bf751fb226016cee7efda4056a1f9dfcbbea339 Mon Sep 17 00:00:00 2001 From: jeb Date: Sat, 2 Feb 2019 10:36:23 -0800 Subject: [PATCH] Reexport derive macros alongside derived traits. --- core/codegen/tests/from_form_value.rs | 2 +- core/codegen/tests/responder.rs | 2 +- core/lib/src/request/mod.rs | 2 + core/lib/src/response/mod.rs | 2 + core/lib/tests/derive-reexports.rs | 58 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 core/lib/tests/derive-reexports.rs diff --git a/core/codegen/tests/from_form_value.rs b/core/codegen/tests/from_form_value.rs index 68f87330..0d81ad8a 100644 --- a/core/codegen/tests/from_form_value.rs +++ b/core/codegen/tests/from_form_value.rs @@ -1,4 +1,4 @@ -#[macro_use] extern crate rocket; +extern crate rocket; use rocket::request::FromFormValue; diff --git a/core/codegen/tests/responder.rs b/core/codegen/tests/responder.rs index de0025d5..f7c82338 100644 --- a/core/codegen/tests/responder.rs +++ b/core/codegen/tests/responder.rs @@ -1,6 +1,6 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] extern crate rocket; +extern crate rocket; use rocket::local::Client; use rocket::response::Responder; diff --git a/core/lib/src/request/mod.rs b/core/lib/src/request/mod.rs index 09be821a..748c994b 100644 --- a/core/lib/src/request/mod.rs +++ b/core/lib/src/request/mod.rs @@ -10,6 +10,8 @@ mod query; #[cfg(test)] mod tests; +#[doc(hidden)] pub use rocket_codegen::{FromForm, FromFormValue}; + pub use self::request::Request; pub use self::from_request::{FromRequest, Outcome}; pub use self::param::{FromParam, FromSegments}; diff --git a/core/lib/src/response/mod.rs b/core/lib/src/response/mod.rs index 44ec2e2e..4fe734b6 100644 --- a/core/lib/src/response/mod.rs +++ b/core/lib/src/response/mod.rs @@ -31,6 +31,8 @@ crate mod flash; pub mod content; pub mod status; +#[doc(hidden)] pub use rocket_codegen::Responder; + pub use self::response::{Response, ResponseBuilder, Body, DEFAULT_CHUNK_SIZE}; pub use self::responder::Responder; pub use self::redirect::Redirect; diff --git a/core/lib/tests/derive-reexports.rs b/core/lib/tests/derive-reexports.rs new file mode 100644 index 00000000..43eaeb49 --- /dev/null +++ b/core/lib/tests/derive-reexports.rs @@ -0,0 +1,58 @@ +#![feature(proc_macro_hygiene, decl_macro)] + +extern crate rocket; + +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 { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + 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("/?")] +fn number(params: Form) -> DerivedResponder { + DerivedResponder { data: params.thing.to_string() } +} + +#[test] +fn test_derive_reexports() { + use rocket::local::Client; + + let rocket = rocket::ignite().mount("/", routes![index, number]); + let client = Client::new(rocket).unwrap(); + + let mut response = client.get("/").dispatch(); + assert_eq!(response.body_string().unwrap(), "hello"); + + let mut response = client.get("/?thing=b").dispatch(); + assert_eq!(response.body_string().unwrap(), "b"); +}