1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-19 22:44:38 +00:00
This commit is contained in:
Dominic 2019-10-20 15:44:20 +02:00
parent 5282dbbe6c
commit 197aad3c94
Signed by: msrd0
GPG key ID: DCC8C247452E98F9

View file

@ -16,37 +16,27 @@ pub fn expand_from_body(tokens : TokenStream) -> TokenStream
let (were, body) = match input.fields { let (were, body) = match input.fields {
Fields::Named(named) => { Fields::Named(named) => {
let fields = named.named; let fields = named.named;
if fields.len() == 0 // basically unit match fields.len() {
{ 0 => (quote!(), quote!(Self{})),
(quote!(), quote!(Self{})) 1 => {
} let field = fields.first().unwrap();
else if fields.len() == 1 let field_ident = field.ident.as_ref().unwrap();
{ let field_ty = &field.ty;
let field = fields.first().unwrap(); (quote!(where #field_ty : for<'a> From<&'a [u8]>), quote!(Self { #field_ident: body.into() }))
let field_ident = field.ident.as_ref().unwrap(); },
let field_ty = &field.ty; _ => panic!("FromBody can only be derived for structs with at most one field")
(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) => { Fields::Unnamed(unnamed) => {
let fields = unnamed.unnamed; let fields = unnamed.unnamed;
if fields.len() == 0 // basically unit match fields.len() {
{ 0 => (quote!(), quote!(Self{})),
(quote!(), quote!(Self{})) 1 => {
} let field = fields.first().unwrap();
else if fields.len() == 1 let field_ty = &field.ty;
{ (quote!(where #field_ty : for<'a> From<&'a [u8]>), quote!(Self(body.into())))
let field = fields.first().unwrap(); },
let field_ty = &field.ty; _ => panic!("FromBody can only be derived for structs with at most one field")
(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{})) Fields::Unit => (quote!(), quote!(Self{}))