mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-17 17:21:04 +00:00
Produce errors on attr that fails to parse as Meta
This commit is contained in:
@@ -305,7 +305,12 @@ impl Container {
|
|||||||
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
|
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
|
||||||
let mut serde_path = Attr::none(cx, CRATE);
|
let mut serde_path = Attr::none(cx, CRATE);
|
||||||
|
|
||||||
for meta_item in item.attrs.iter().filter_map(get_serde_meta_items).flatten() {
|
for meta_item in item
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.flat_map(|attr| get_serde_meta_items(cx, attr))
|
||||||
|
.flatten()
|
||||||
|
{
|
||||||
match meta_item {
|
match meta_item {
|
||||||
// Parse `#[serde(rename = "foo")]`
|
// Parse `#[serde(rename = "foo")]`
|
||||||
Meta(NameValue(ref m)) if m.path == RENAME => {
|
Meta(NameValue(ref m)) if m.path == RENAME => {
|
||||||
@@ -891,7 +896,7 @@ impl Variant {
|
|||||||
for meta_item in variant
|
for meta_item in variant
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(get_serde_meta_items)
|
.flat_map(|attr| get_serde_meta_items(cx, attr))
|
||||||
.flatten()
|
.flatten()
|
||||||
{
|
{
|
||||||
match meta_item {
|
match meta_item {
|
||||||
@@ -1210,7 +1215,7 @@ impl Field {
|
|||||||
for meta_item in field
|
for meta_item in field
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(get_serde_meta_items)
|
.flat_map(|attr| get_serde_meta_items(cx, attr))
|
||||||
.flatten()
|
.flatten()
|
||||||
.chain(variant_borrow)
|
.chain(variant_borrow)
|
||||||
{
|
{
|
||||||
@@ -1599,17 +1604,21 @@ fn get_where_predicates(
|
|||||||
Ok((ser.at_most_one()?, de.at_most_one()?))
|
Ok((ser.at_most_one()?, de.at_most_one()?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_serde_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMeta>> {
|
pub fn get_serde_meta_items(cx: &Ctxt, attr: &syn::Attribute) -> Result<Vec<syn::NestedMeta>, ()> {
|
||||||
if attr.path == SERDE {
|
if attr.path != SERDE {
|
||||||
match attr.parse_meta() {
|
return Ok(Vec::new());
|
||||||
Ok(List(ref meta)) => Some(meta.nested.iter().cloned().collect()),
|
}
|
||||||
_ => {
|
|
||||||
// TODO: produce an error
|
match attr.parse_meta() {
|
||||||
None
|
Ok(List(ref meta)) => Ok(meta.nested.iter().cloned().collect()),
|
||||||
}
|
Ok(other) => {
|
||||||
|
cx.error_spanned_by(other, "expected #[serde(...)]");
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
cx.syn_error(err);
|
||||||
|
Err(())
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,11 @@ impl Ctxt {
|
|||||||
.push(syn::Error::new_spanned(obj.into_token_stream(), msg));
|
.push(syn::Error::new_spanned(obj.into_token_stream(), msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add one of Syn's parse errors.
|
||||||
|
pub fn syn_error(&self, err: syn::Error) {
|
||||||
|
self.errors.borrow_mut().as_mut().unwrap().push(err);
|
||||||
|
}
|
||||||
|
|
||||||
/// Consume this object, producing a formatted error string if there are errors.
|
/// Consume this object, producing a formatted error string if there are errors.
|
||||||
pub fn check(self) -> Result<(), Vec<syn::Error>> {
|
pub fn check(self) -> Result<(), Vec<syn::Error>> {
|
||||||
let errors = self.errors.borrow_mut().take().unwrap();
|
let errors = self.errors.borrow_mut().take().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user