Improve error message parsing from and into
This commit is contained in:
parent
bc946e4fd7
commit
42b2fd8eec
@ -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" => {
|
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));
|
from_type.set_opt(Some(from_ty));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse `#[serde(into = "Type")]
|
||||||
MetaItem(NameValue(ref name, ref lit)) if name == "into" => {
|
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));
|
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()))
|
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>, ()> {
|
fn get_renames(cx: &Ctxt, items: &[syn::NestedMetaItem]) -> Result<SerAndDe<String>, ()> {
|
||||||
get_ser_and_de(cx, "rename", items, get_string_from_lit)
|
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, ()> {
|
-> Result<syn::Ty, ()> {
|
||||||
let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit));
|
let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit));
|
||||||
|
|
||||||
if let Ok(ty) = syn::parse_type(&string) {
|
syn::parse_type(&string).map_err(|_| {
|
||||||
Ok(ty)
|
cx.error(format!("failed to parse type: {} = {:?}", attr_name, string))
|
||||||
} else {
|
})
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
#[derive(Deserialize)] //~ ERROR: proc-macro derive panicked
|
#[derive(Deserialize)] //~ ERROR: proc-macro derive panicked
|
||||||
#[serde(from="")]
|
#[serde(from = "Option<T")] //~^ HELP: failed to parse type: from = "Option<T"
|
||||||
enum TestOne {
|
enum TestOne {
|
||||||
Testing,
|
Testing,
|
||||||
One,
|
One,
|
@ -2,7 +2,7 @@
|
|||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
|
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
|
||||||
#[serde(into="")]
|
#[serde(into = "Option<T")] //~^ HELP: failed to parse type: into = "Option<T"
|
||||||
enum TestOne {
|
enum TestOne {
|
||||||
Testing,
|
Testing,
|
||||||
One,
|
One,
|
Loading…
x
Reference in New Issue
Block a user