Less indentiation in deserialize_from_body

This commit is contained in:
David Tolnay 2017-12-10 23:04:44 -08:00
parent ccae35d92a
commit 85e3ddc2b8
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 22 additions and 14 deletions

View File

@ -266,24 +266,23 @@ fn deserialize_from_body(cont: &Container, params: &Parameters) -> Option<Fragme
// for remote derives.
assert!(!params.has_getter);
if cont.body.all_fields().all(|field| field.attrs.deserialize_with().is_some()) {
if cont.attrs.from_type().is_some()
|| cont.attrs.identifier().is_some()
|| cont.body.all_fields().all(|f| f.attrs.deserialize_with().is_some())
{
return None;
}
if let (None, attr::Identifier::No) = (cont.attrs.from_type(), cont.attrs.identifier()) {
match cont.body {
Body::Enum(_) => None,
Body::Struct(Style::Struct, ref fields) => {
Some(deserialize_from_struct(None, params, fields, &cont.attrs, None, Untagged::No))
}
Body::Struct(Style::Tuple, ref fields) |
Body::Struct(Style::Newtype, ref fields) => {
Some(deserialize_from_tuple(None, params, fields, &cont.attrs, None))
}
Body::Struct(Style::Unit, _) => None,
match cont.body {
Body::Enum(_) => None,
Body::Struct(Style::Struct, ref fields) => {
Some(deserialize_from_struct(None, params, fields, &cont.attrs, None, Untagged::No))
}
} else {
None
Body::Struct(Style::Tuple, ref fields) |
Body::Struct(Style::Newtype, ref fields) => {
Some(deserialize_from_tuple(None, params, fields, &cont.attrs, None))
}
Body::Struct(Style::Unit, _) => None,
}
}

View File

@ -164,6 +164,15 @@ pub enum Identifier {
Variant,
}
impl Identifier {
pub fn is_some(self) -> bool {
match self {
Identifier::No => false,
Identifier::Field | Identifier::Variant => true,
}
}
}
impl Container {
/// Extract out the `#[serde(...)]` attributes from an item.
pub fn from_ast(cx: &Ctxt, item: &syn::DeriveInput) -> Self {