Rocket

A web framework for Rust. https://rocket.rs (mirror)
git clone git://git.thc420.xyz/Rocket
Log | Files | Refs | README

commit 6bf751fb226016cee7efda4056a1f9dfcbbea339
parent 1e611ff86e4f85ee024ac698943f97b6b422ba14
Author: jeb <jeb@jebrosen.com>
Date:   Sat,  2 Feb 2019 10:36:23 -0800

Reexport derive macros alongside derived traits.

Diffstat:
Mcore/codegen/tests/from_form_value.rs | 2+-
Mcore/codegen/tests/responder.rs | 2+-
Mcore/lib/src/request/mod.rs | 2++
Mcore/lib/src/response/mod.rs | 2++
Acore/lib/tests/derive-reexports.rs | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 64 insertions(+), 2 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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("/?<params..>")] +fn number(params: Form<ThingForm>) -> 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"); +}