Refactor of AST parsing

This commit is contained in:
Graham Esau 2020-05-10 22:09:15 +01:00
parent 08886799bb
commit 70b5a1a2e6
4 changed files with 197 additions and 76 deletions

View file

@ -0,0 +1,63 @@
mod from_serde;
use from_serde::FromSerde;
use serde_derive_internals::ast as serde_ast;
use serde_derive_internals::{Ctxt, Derive};
pub struct Container<'a> {
pub ident: syn::Ident,
pub serde_attrs: serde_derive_internals::attr::Container,
pub data: Data<'a>,
pub generics: &'a syn::Generics,
pub original: &'a syn::DeriveInput,
}
pub enum Data<'a> {
Enum(Vec<Variant<'a>>),
Struct(serde_ast::Style, Vec<Field<'a>>),
}
pub struct Variant<'a> {
pub ident: syn::Ident,
pub serde_attrs: serde_derive_internals::attr::Variant,
pub style: serde_ast::Style,
pub fields: Vec<Field<'a>>,
pub original: &'a syn::Variant,
pub with: Option<syn::Type>,
}
pub struct Field<'a> {
pub member: syn::Member,
pub serde_attrs: serde_derive_internals::attr::Field,
pub ty: &'a syn::Type,
pub original: &'a syn::Field,
pub with: Option<syn::Type>,
}
impl<'a> Container<'a> {
pub fn from_ast(item: &'a syn::DeriveInput) -> Result<Container<'a>, Vec<syn::Error>> {
let ctxt = Ctxt::new();
let result = serde_ast::Container::from_ast(&ctxt, item, Derive::Deserialize)
.ok_or(())
.and_then(|serde| Self::from_serde(&ctxt, serde));
ctxt.check()
.map(|_| result.expect("from_ast set no errors on Ctxt, so should have returned Ok"))
}
pub fn name(&self) -> String {
self.serde_attrs.name().deserialize_name()
}
}
impl<'a> Variant<'a> {
pub fn name(&self) -> String {
self.serde_attrs.name().deserialize_name()
}
}
impl<'a> Field<'a> {
pub fn name(&self) -> String {
self.serde_attrs.name().deserialize_name()
}
}