Revamp serde_codegen_internals error handling

This commit is contained in:
David Tolnay
2016-09-27 00:11:37 -07:00
parent 4ad6c4fd56
commit 0c18c151e2
5 changed files with 54 additions and 27 deletions
+4 -3
View File
@@ -5,11 +5,12 @@ use bound;
use internals::ast::{Body, Field, Item, Style, Variant};
use internals::{self, attr};
pub fn expand_derive_deserialize(item: &syn::MacroInput) -> Tokens {
pub fn expand_derive_deserialize(item: &syn::MacroInput) -> Result<Tokens, String> {
let item = {
let ctxt = internals::Ctxt::new();
let item = Item::from_ast(&ctxt, item);
check_no_str(&ctxt, &item);
try!(ctxt.check());
item
};
@@ -27,7 +28,7 @@ pub fn expand_derive_deserialize(item: &syn::MacroInput) -> Tokens {
let dummy_const = aster::id(format!("_IMPL_DESERIALIZE_FOR_{}", item.ident));
quote! {
Ok(quote! {
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const #dummy_const: () = {
extern crate serde as _serde;
@@ -40,7 +41,7 @@ pub fn expand_derive_deserialize(item: &syn::MacroInput) -> Tokens {
}
}
};
}
})
}
// All the generics in the input, plus a bound `T: Deserialize` for each generic
+12 -6
View File
@@ -123,7 +123,7 @@ macro_rules! shim {
($name:ident $pkg:ident :: $func:ident) => {
fn $func(
cx: &mut ::syntax::ext::base::ExtCtxt,
_span: ::syntax::codemap::Span,
span: ::syntax::codemap::Span,
meta_item: &::syntax::ast::MetaItem,
annotatable: &::syntax::ext::base::Annotatable,
push: &mut FnMut(::syntax::ext::base::Annotatable)
@@ -158,7 +158,13 @@ macro_rules! shim {
let s = pprust::item_to_string(item);
let syn_item = syn::parse_macro_input(&s).unwrap();
let expanded = $pkg::$func(&syn_item).to_string();
let expanded = match $pkg::$func(&syn_item) {
Ok(expanded) => expanded.to_string(),
Err(msg) => {
cx.span_err(span, &msg);
return;
}
};
use syntax::parse;
let name = stringify!($name).to_string();
@@ -174,21 +180,21 @@ shim!(Serialize ser::expand_derive_serialize);
shim!(Deserialize de::expand_derive_deserialize);
#[cfg(feature = "with-syn")]
pub fn expand_single_item(item: &str) -> String {
pub fn expand_single_item(item: &str) -> Result<String, String> {
let syn_item = syn::parse_macro_input(item).unwrap();
let (ser, de, syn_item) = strip_serde_derives(syn_item);
let expanded_ser = if ser {
Some(ser::expand_derive_serialize(&syn_item))
Some(try!(ser::expand_derive_serialize(&syn_item)))
} else {
None
};
let expanded_de = if de {
Some(de::expand_derive_deserialize(&syn_item))
Some(try!(de::expand_derive_deserialize(&syn_item)))
} else {
None::<quote::Tokens>
};
let syn_item = strip_serde_attrs(syn_item);
return quote!(#expanded_ser #expanded_de #syn_item).to_string();
return Ok(quote!(#expanded_ser #expanded_de #syn_item).to_string());
fn strip_serde_derives(item: syn::MacroInput) -> (bool, bool, syn::MacroInput) {
let mut ser = false;
+6 -4
View File
@@ -5,8 +5,10 @@ use bound;
use internals::ast::{Body, Field, Item, Style, Variant};
use internals::{self, attr};
pub fn expand_derive_serialize(item: &syn::MacroInput) -> Tokens {
let item = Item::from_ast(&internals::Ctxt::new(), item);
pub fn expand_derive_serialize(item: &syn::MacroInput) -> Result<Tokens, String> {
let ctxt = internals::Ctxt::new();
let item = Item::from_ast(&ctxt, item);
try!(ctxt.check());
let impl_generics = build_impl_generics(&item);
@@ -22,7 +24,7 @@ pub fn expand_derive_serialize(item: &syn::MacroInput) -> Tokens {
let dummy_const = aster::id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
quote! {
Ok(quote! {
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const #dummy_const: () = {
extern crate serde as _serde;
@@ -35,7 +37,7 @@ pub fn expand_derive_serialize(item: &syn::MacroInput) -> Tokens {
}
}
};
}
})
}
// All the generics in the input, plus a bound `T: Serialize` for each generic