Improve error message parsing from and into

This commit is contained in:
David Tolnay 2017-03-18 09:21:46 -07:00
parent bc946e4fd7
commit 42b2fd8eec
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 10 additions and 22 deletions

View File

@ -274,14 +274,16 @@ impl Item {
}
}
// Parse `#[serde(from = "type", into = "type")]
// Parse `#[serde(from = "Type")]
MetaItem(NameValue(ref name, ref lit)) if name == "from" => {
if let Ok(from_ty) = get_type(cx, name.as_ref(), lit) {
if let Ok(from_ty) = parse_lit_into_ty(cx, name.as_ref(), lit) {
from_type.set_opt(Some(from_ty));
}
}
// Parse `#[serde(into = "Type")]
MetaItem(NameValue(ref name, ref lit)) if name == "into" => {
if let Ok(into_ty) = get_type(cx, name.as_ref(), lit) {
if let Ok(into_ty) = parse_lit_into_ty(cx, name.as_ref(), lit) {
into_type.set_opt(Some(into_ty));
}
}
@ -772,18 +774,6 @@ fn get_ser_and_de<T, F>(cx: &Ctxt,
Ok((ser_item.get(), de_item.get()))
}
fn get_type(cx: &Ctxt,
attr_name: &str,
lit: &syn::Lit)
-> Result<syn::Ty, ()> {
if let Ok(ty) = parse_lit_into_ty(cx, attr_name, &lit) {
Ok(ty)
} else {
cx.error(format!("error parsing type for attribute {}", attr_name));
return Err(());
}
}
fn get_renames(cx: &Ctxt, items: &[syn::NestedMetaItem]) -> Result<SerAndDe<String>, ()> {
get_ser_and_de(cx, "rename", items, get_string_from_lit)
}
@ -842,9 +832,7 @@ fn parse_lit_into_ty(cx: &Ctxt,
-> Result<syn::Ty, ()> {
let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit));
if let Ok(ty) = syn::parse_type(&string) {
Ok(ty)
} else {
Err(())
}
syn::parse_type(&string).map_err(|_| {
cx.error(format!("failed to parse type: {} = {:?}", attr_name, string))
})
}

View File

@ -2,7 +2,7 @@
extern crate serde_derive;
#[derive(Deserialize)] //~ ERROR: proc-macro derive panicked
#[serde(from="")]
#[serde(from = "Option<T")] //~^ HELP: failed to parse type: from = "Option<T"
enum TestOne {
Testing,
One,

View File

@ -2,7 +2,7 @@
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(into="")]
#[serde(into = "Option<T")] //~^ HELP: failed to parse type: into = "Option<T"
enum TestOne {
Testing,
One,