diff --git a/serde_codegen_internals/src/attr.rs b/serde_codegen_internals/src/attr.rs index ca6697a3..11a2eb2a 100644 --- a/serde_codegen_internals/src/attr.rs +++ b/serde_codegen_internals/src/attr.rs @@ -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(cx: &Ctxt, Ok((ser_item.get(), de_item.get())) } -fn get_type(cx: &Ctxt, - attr_name: &str, - lit: &syn::Lit) - -> Result { - 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, ()> { get_ser_and_de(cx, "rename", items, get_string_from_lit) } @@ -842,9 +832,7 @@ fn parse_lit_into_ty(cx: &Ctxt, -> Result { 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)) + }) } diff --git a/test_suite/tests/compile-fail/type-attribute/type_attribute_fail_from.rs b/test_suite/tests/compile-fail/type-attribute/from.rs similarity index 67% rename from test_suite/tests/compile-fail/type-attribute/type_attribute_fail_from.rs rename to test_suite/tests/compile-fail/type-attribute/from.rs index 86556615..408f33b5 100644 --- a/test_suite/tests/compile-fail/type-attribute/type_attribute_fail_from.rs +++ b/test_suite/tests/compile-fail/type-attribute/from.rs @@ -2,7 +2,7 @@ extern crate serde_derive; #[derive(Deserialize)] //~ ERROR: proc-macro derive panicked -#[serde(from="")] +#[serde(from = "Option