1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 13:02:28 +00:00
deprecated-gotham-restful/gotham_restful_derive/src/from_body.rs

133 lines
3 KiB
Rust
Raw Normal View History

2020-05-04 19:08:22 +02:00
use proc_macro2::TokenStream;
2020-04-19 22:26:29 +02:00
use quote::{format_ident, quote};
2020-04-29 21:00:06 +02:00
use std::cmp::min;
2019-10-20 14:49:53 +00:00
use syn::{
spanned::Spanned,
Data,
DeriveInput,
2020-04-08 21:53:57 +02:00
Error,
2020-04-19 22:26:29 +02:00
Field,
2019-10-20 14:49:53 +00:00
Fields,
2020-04-19 22:26:29 +02:00
Ident,
2020-05-04 19:08:22 +02:00
Result,
2020-05-03 23:25:48 +02:00
Type
2019-10-20 14:49:53 +00:00
};
2020-04-19 22:26:29 +02:00
struct ParsedFields
{
fields : Vec<(Ident, Type)>,
named : bool
}
impl ParsedFields
{
2020-05-04 19:08:22 +02:00
fn from_named<I>(fields : I) -> Result<Self>
2020-05-03 23:25:48 +02:00
where
I : Iterator<Item = Field>
2020-04-19 22:26:29 +02:00
{
2020-05-03 23:25:48 +02:00
let fields = fields.map(|field| (field.ident.unwrap(), field.ty)).collect();
2020-04-19 22:26:29 +02:00
Ok(Self { fields, named: true })
}
2020-05-04 19:08:22 +02:00
fn from_unnamed<I>(fields : I) -> Result<Self>
2020-05-03 23:25:48 +02:00
where
I : Iterator<Item = Field>
2020-04-19 22:26:29 +02:00
{
2020-05-04 00:27:14 +02:00
let fields = fields.enumerate().map(|(i, field)| (format_ident!("arg{}", i), field.ty)).collect();
2020-04-19 22:26:29 +02:00
Ok(Self { fields, named: false })
}
2020-05-04 19:08:22 +02:00
fn from_unit() -> Result<Self>
2020-04-19 22:26:29 +02:00
{
Ok(Self { fields: Vec::new(), named: false })
}
}
2020-05-04 19:08:22 +02:00
pub fn expand_from_body(input : DeriveInput) -> Result<TokenStream>
2019-10-20 14:49:53 +00:00
{
let krate = super::krate();
let ident = input.ident;
let generics = input.generics;
let strukt = match input.data {
Data::Enum(inum) => Err(inum.enum_token.span()),
Data::Struct(strukt) => Ok(strukt),
Data::Union(uni) => Err(uni.union_token.span())
}.map_err(|span| Error::new(span, "#[derive(FromBody)] only works for enums"))?;
let fields = match strukt.fields {
2020-05-03 23:25:48 +02:00
Fields::Named(named) => ParsedFields::from_named(named.named.into_iter())?,
Fields::Unnamed(unnamed) => ParsedFields::from_unnamed(unnamed.unnamed.into_iter())?,
2020-04-19 22:26:29 +02:00
Fields::Unit => ParsedFields::from_unit()?
};
let mut where_clause = quote!();
let mut block = quote!();
let mut body_ident = format_ident!("_body");
let mut type_ident = format_ident!("_type");
if let Some(body_field) = fields.fields.get(0)
{
body_ident = body_field.0.clone();
let body_ty = &body_field.1;
where_clause = quote! {
#where_clause
#body_ty : for<'a> From<&'a [u8]>,
};
block = quote! {
#block
let #body_ident : &[u8] = &#body_ident;
let #body_ident : #body_ty = #body_ident.into();
};
}
if let Some(type_field) = fields.fields.get(1)
{
type_ident = type_field.0.clone();
let type_ty = &type_field.1;
where_clause = quote! {
#where_clause
#type_ty : From<#krate::Mime>,
};
block = quote! {
#block
let #type_ident : #type_ty = #type_ident.into();
};
}
2020-04-29 21:00:06 +02:00
for field in &fields.fields[min(2, fields.fields.len())..]
2020-04-19 22:26:29 +02:00
{
let field_ident = &field.0;
let field_ty = &field.1;
where_clause = quote! {
#where_clause
#field_ty : Default,
};
block = quote! {
#block
let #field_ident : #field_ty = Default::default();
};
}
let field_names : Vec<&Ident> = fields.fields.iter().map(|field| &field.0).collect();
let ctor = if fields.named {
quote!(Self { #(#field_names),* })
} else {
quote!(Self ( #(#field_names),* ))
2019-10-20 14:49:53 +00:00
};
2020-04-08 21:53:57 +02:00
Ok(quote! {
2019-10-20 14:49:53 +00:00
impl #generics #krate::FromBody for #ident #generics
2020-04-19 22:26:29 +02:00
where #where_clause
2019-10-20 14:49:53 +00:00
{
type Err = #krate::FromBodyNoError;
2019-10-20 14:49:53 +00:00
fn from_body(#body_ident : #krate::gotham::hyper::body::Bytes, #type_ident : #krate::Mime) -> Result<Self, #krate::FromBodyNoError>
2019-10-20 14:49:53 +00:00
{
2020-04-19 22:26:29 +02:00
#block
Ok(#ctor)
2019-10-20 14:49:53 +00:00
}
}
2020-04-08 21:53:57 +02:00
})
2019-10-20 14:49:53 +00:00
}