mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-19 22:44:38 +00:00
add derive for raw request body
This commit is contained in:
parent
f737ac4332
commit
5282dbbe6c
6 changed files with 243 additions and 18 deletions
18
README.md
18
README.md
|
@ -22,7 +22,6 @@ gotham_restful = "0.0.1"
|
||||||
A basic server with only one resource, handling a simple `GET` request, could look like this:
|
A basic server with only one resource, handling a simple `GET` request, could look like this:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#
|
|
||||||
/// Our RESTful Resource.
|
/// Our RESTful Resource.
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
#[rest_resource(read_all)]
|
#[rest_resource(read_all)]
|
||||||
|
@ -54,6 +53,23 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Uploads and Downloads can also be handled, but you need to specify the mime type manually:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Resource)]
|
||||||
|
#[rest_resource(create)]
|
||||||
|
struct ImageResource;
|
||||||
|
|
||||||
|
#[derive(FromBody, RequestBody)]
|
||||||
|
#[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
|
||||||
|
struct RawImage(Vec<u8>);
|
||||||
|
|
||||||
|
#[rest_create(ImageResource)]
|
||||||
|
fn create(_state : &mut State, body : RawImage) -> Raw<Vec<u8>> {
|
||||||
|
Raw::new(body.0, mime::APPLICATION_OCTET_STREAM)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Look at the [example] for more methods and usage with the `openapi` feature.
|
Look at the [example] for more methods and usage with the `openapi` feature.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
|
@ -22,7 +22,6 @@ A basic server with only one resource, handling a simple `GET` request, could lo
|
||||||
# use gotham::{router::builder::*, state::State};
|
# use gotham::{router::builder::*, state::State};
|
||||||
# use gotham_restful::{DrawResources, Resource, Success};
|
# use gotham_restful::{DrawResources, Resource, Success};
|
||||||
# use serde::{Deserialize, Serialize};
|
# use serde::{Deserialize, Serialize};
|
||||||
#
|
|
||||||
/// Our RESTful Resource.
|
/// Our RESTful Resource.
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
#[rest_resource(read_all)]
|
#[rest_resource(read_all)]
|
||||||
|
@ -55,6 +54,32 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Uploads and Downloads can also be handled, but you need to specify the mime type manually:
|
||||||
|
|
||||||
|
```rust,no_run
|
||||||
|
# #[macro_use] extern crate gotham_restful_derive;
|
||||||
|
# use gotham::{router::builder::*, state::State};
|
||||||
|
# use gotham_restful::{DrawResources, Raw, Resource, Success};
|
||||||
|
# use serde::{Deserialize, Serialize};
|
||||||
|
#[derive(Resource)]
|
||||||
|
#[rest_resource(create)]
|
||||||
|
struct ImageResource;
|
||||||
|
|
||||||
|
#[derive(FromBody, RequestBody)]
|
||||||
|
#[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
|
||||||
|
struct RawImage(Vec<u8>);
|
||||||
|
|
||||||
|
#[rest_create(ImageResource)]
|
||||||
|
fn create(_state : &mut State, body : RawImage) -> Raw<Vec<u8>> {
|
||||||
|
Raw::new(body.0, mime::APPLICATION_OCTET_STREAM)
|
||||||
|
}
|
||||||
|
# fn main() {
|
||||||
|
# gotham::start("127.0.0.1:8080", build_simple_router(|route| {
|
||||||
|
# route.resource::<ImageResource, _>("image");
|
||||||
|
# }));
|
||||||
|
# }
|
||||||
|
```
|
||||||
|
|
||||||
Look at the [example] for more methods and usage with the `openapi` feature.
|
Look at the [example] for more methods and usage with the `openapi` feature.
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
|
@ -38,34 +38,42 @@ impl<T : ResourceType + Serialize> ResponseBody for T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// A type that can be used inside a request body. Implemented for every type that is
|
/// This trait must be implemented by every type that can be used as a request body. It allows
|
||||||
/// deserializable with serde. If the `openapi` feature is used, it must also be of type
|
/// to create the type from a hyper body chunk and it's content type.
|
||||||
/// `OpenapiType`.
|
pub trait FromBody : Sized
|
||||||
pub trait RequestBody : ResourceType + Sized
|
|
||||||
{
|
{
|
||||||
type Err : Into<ResourceError>;
|
type Err : Into<ResourceError>;
|
||||||
|
|
||||||
/// Return all types that are supported as content types
|
|
||||||
fn supported_types() -> Option<Vec<Mime>>
|
|
||||||
{
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create the request body from a raw body and the content type.
|
/// Create the request body from a raw body and the content type.
|
||||||
fn from_body(body : Chunk, content_type : Mime) -> Result<Self, Self::Err>;
|
fn from_body(body : Chunk, content_type : Mime) -> Result<Self, Self::Err>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T : ResourceType + DeserializeOwned> RequestBody for T
|
impl<T : DeserializeOwned> FromBody for T
|
||||||
{
|
{
|
||||||
type Err = serde_json::Error;
|
type Err = serde_json::Error;
|
||||||
|
|
||||||
fn supported_types() -> Option<Vec<Mime>>
|
|
||||||
{
|
|
||||||
Some(vec![APPLICATION_JSON])
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_body(body : Chunk, _content_type : Mime) -> Result<Self, Self::Err>
|
fn from_body(body : Chunk, _content_type : Mime) -> Result<Self, Self::Err>
|
||||||
{
|
{
|
||||||
serde_json::from_slice(&body)
|
serde_json::from_slice(&body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A type that can be used inside a request body. Implemented for every type that is
|
||||||
|
/// deserializable with serde. If the `openapi` feature is used, it must also be of type
|
||||||
|
/// `OpenapiType`.
|
||||||
|
pub trait RequestBody : ResourceType + FromBody
|
||||||
|
{
|
||||||
|
/// Return all types that are supported as content types.
|
||||||
|
fn supported_types() -> Option<Vec<Mime>>
|
||||||
|
{
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T : ResourceType + DeserializeOwned> RequestBody for T
|
||||||
|
{
|
||||||
|
fn supported_types() -> Option<Vec<Mime>>
|
||||||
|
{
|
||||||
|
Some(vec![APPLICATION_JSON])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
69
gotham_restful_derive/src/from_body.rs
Normal file
69
gotham_restful_derive/src/from_body.rs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
use syn::{
|
||||||
|
Fields,
|
||||||
|
ItemStruct,
|
||||||
|
parse_macro_input
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn expand_from_body(tokens : TokenStream) -> TokenStream
|
||||||
|
{
|
||||||
|
let krate = super::krate();
|
||||||
|
let input = parse_macro_input!(tokens as ItemStruct);
|
||||||
|
let ident = input.ident;
|
||||||
|
let generics = input.generics;
|
||||||
|
|
||||||
|
let (were, body) = match input.fields {
|
||||||
|
Fields::Named(named) => {
|
||||||
|
let fields = named.named;
|
||||||
|
if fields.len() == 0 // basically unit
|
||||||
|
{
|
||||||
|
(quote!(), quote!(Self{}))
|
||||||
|
}
|
||||||
|
else if fields.len() == 1
|
||||||
|
{
|
||||||
|
let field = fields.first().unwrap();
|
||||||
|
let field_ident = field.ident.as_ref().unwrap();
|
||||||
|
let field_ty = &field.ty;
|
||||||
|
(quote!(where #field_ty : for<'a> From<&'a [u8]>), quote!(Self { #field_ident: body.into() }))
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
panic!("FromBody can only be derived for structs with at most one field")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Fields::Unnamed(unnamed) => {
|
||||||
|
let fields = unnamed.unnamed;
|
||||||
|
if fields.len() == 0 // basically unit
|
||||||
|
{
|
||||||
|
(quote!(), quote!(Self{}))
|
||||||
|
}
|
||||||
|
else if fields.len() == 1
|
||||||
|
{
|
||||||
|
let field = fields.first().unwrap();
|
||||||
|
let field_ty = &field.ty;
|
||||||
|
(quote!(where #field_ty : for<'a> From<&'a [u8]>), quote!(Self(body.into())))
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
panic!("FromBody can only be derived for structs with at most one field")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Fields::Unit => (quote!(), quote!(Self{}))
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = quote! {
|
||||||
|
impl #generics #krate::FromBody for #ident #generics
|
||||||
|
#were
|
||||||
|
{
|
||||||
|
type Err = String;
|
||||||
|
|
||||||
|
fn from_body(body : #krate::Chunk, _content_type : #krate::Mime) -> Result<Self, Self::Err>
|
||||||
|
{
|
||||||
|
let body : &[u8] = &body;
|
||||||
|
Ok(#body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
output.into()
|
||||||
|
}
|
|
@ -4,8 +4,12 @@ use proc_macro::TokenStream;
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
|
mod from_body;
|
||||||
|
use from_body::expand_from_body;
|
||||||
mod method;
|
mod method;
|
||||||
use method::{expand_method, Method};
|
use method::{expand_method, Method};
|
||||||
|
mod request_body;
|
||||||
|
use request_body::expand_request_body;
|
||||||
mod resource;
|
mod resource;
|
||||||
use resource::expand_resource;
|
use resource::expand_resource;
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
|
@ -16,6 +20,12 @@ fn krate() -> TokenStream2
|
||||||
quote!(::gotham_restful)
|
quote!(::gotham_restful)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(FromBody)]
|
||||||
|
pub fn derive_from_body(tokens : TokenStream) -> TokenStream
|
||||||
|
{
|
||||||
|
expand_from_body(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
#[proc_macro_derive(OpenapiType)]
|
#[proc_macro_derive(OpenapiType)]
|
||||||
pub fn derive_openapi_type(tokens : TokenStream) -> TokenStream
|
pub fn derive_openapi_type(tokens : TokenStream) -> TokenStream
|
||||||
|
@ -23,6 +33,12 @@ pub fn derive_openapi_type(tokens : TokenStream) -> TokenStream
|
||||||
openapi_type::expand(tokens)
|
openapi_type::expand(tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(RequestBody, attributes(supported_types))]
|
||||||
|
pub fn derive_request_body(tokens : TokenStream) -> TokenStream
|
||||||
|
{
|
||||||
|
expand_request_body(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(Resource, attributes(rest_resource))]
|
#[proc_macro_derive(Resource, attributes(rest_resource))]
|
||||||
pub fn derive_resource(tokens : TokenStream) -> TokenStream
|
pub fn derive_resource(tokens : TokenStream) -> TokenStream
|
||||||
{
|
{
|
||||||
|
|
91
gotham_restful_derive/src/request_body.rs
Normal file
91
gotham_restful_derive/src/request_body.rs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
|
use quote::quote;
|
||||||
|
use syn::{
|
||||||
|
parse::{Parse, ParseStream, Result as SynResult},
|
||||||
|
punctuated::Punctuated,
|
||||||
|
token::Comma,
|
||||||
|
Generics,
|
||||||
|
Ident,
|
||||||
|
ItemStruct,
|
||||||
|
Path,
|
||||||
|
parenthesized,
|
||||||
|
parse_macro_input
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MimeList(Punctuated<Path, Comma>);
|
||||||
|
|
||||||
|
impl Parse for MimeList
|
||||||
|
{
|
||||||
|
fn parse(input: ParseStream) -> SynResult<Self>
|
||||||
|
{
|
||||||
|
let content;
|
||||||
|
let _paren = parenthesized!(content in input);
|
||||||
|
let list : Punctuated<Path, Comma> = Punctuated::parse_separated_nonempty(&content)?;
|
||||||
|
Ok(Self(list))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "openapi"))]
|
||||||
|
fn impl_openapi_type(_ident : &Ident, _generics : &Generics) -> TokenStream2
|
||||||
|
{
|
||||||
|
quote!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "openapi")]
|
||||||
|
fn impl_openapi_type(ident : &Ident, generics : &Generics) -> TokenStream2
|
||||||
|
{
|
||||||
|
let krate = super::krate();
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl #generics #krate::OpenapiType for #ident #generics
|
||||||
|
{
|
||||||
|
fn schema() -> #krate::OpenapiSchema
|
||||||
|
{
|
||||||
|
use #krate::{export::openapi::*, OpenapiSchema};
|
||||||
|
|
||||||
|
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType {
|
||||||
|
format: VariantOrUnknownOrEmpty::Item(StringFormat::Binary),
|
||||||
|
pattern: None,
|
||||||
|
enumeration: Vec::new()
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn expand_request_body(tokens : TokenStream) -> TokenStream
|
||||||
|
{
|
||||||
|
let krate = super::krate();
|
||||||
|
let input = parse_macro_input!(tokens as ItemStruct);
|
||||||
|
let ident = input.ident;
|
||||||
|
let generics = input.generics;
|
||||||
|
|
||||||
|
let types : Vec<Path> = input.attrs.into_iter().filter(|attr|
|
||||||
|
attr.path.segments.iter().last().map(|segment| segment.ident.to_string()) == Some("supported_types".to_string()) // TODO wtf
|
||||||
|
).flat_map(|attr| {
|
||||||
|
let m : MimeList = syn::parse2(attr.tokens).expect("unable to parse attributes");
|
||||||
|
m.0.into_iter()
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
let types = match types {
|
||||||
|
ref types if types.is_empty() => quote!(None),
|
||||||
|
types => quote!(Some(vec![#(#types),*]))
|
||||||
|
};
|
||||||
|
|
||||||
|
let impl_openapi_type = impl_openapi_type(&ident, &generics);
|
||||||
|
|
||||||
|
let output = quote! {
|
||||||
|
impl #generics #krate::RequestBody for #ident #generics
|
||||||
|
where #ident #generics : #krate::FromBody
|
||||||
|
{
|
||||||
|
fn supported_types() -> Option<Vec<#krate::Mime>>
|
||||||
|
{
|
||||||
|
#types
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#impl_openapi_type
|
||||||
|
};
|
||||||
|
output.into()
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue