Skip to content

Commit b38f02a

Browse files
committed
chore(derive): simplify code for review
Signed-off-by: Joshua Chapman <joshua.chapman@secomind.com>
1 parent 5d6d36e commit b38f02a

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

astarte-device-sdk-derive/src/event.rs

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -370,53 +370,42 @@ impl FromEventDerive {
370370
let variants = variants.iter().enumerate().map(|(i, v)| {
371371
let variant = &v.name;
372372

373-
if v.attrs.allow_unset {
374-
quote! {
375-
#i => {
376-
match event.data {
377-
Value::Individual{..} | Value::Object{..} => {
378-
return Err(FromEventError::InterfaceType(InterfaceTypeError::with_path(
379-
event.interface,
380-
event.path,
381-
InterfaceTypeDef::Properties,
382-
InterfaceTypeDef::Datastream,
383-
)));
384-
},
385-
Value::Property(Some(prop)) => {
386-
prop.try_into()
387-
.map(|value| #name::#variant(Some(value)))
388-
.map_err(FromEventError::from)
389-
},
390-
Value::Property(None) => {
391-
Ok(#name::#variant(None))
392-
},
393-
}
394-
}
395-
}
373+
let prop_set_case = if v.attrs.allow_unset {
374+
quote! { Some(value) }
375+
} else {
376+
quote! { value }
377+
};
378+
379+
let prop_unset = if v.attrs.allow_unset {
380+
quote! { Ok(#name::#variant(None)) }
396381
} else {
397382
quote! {
398-
#i => {
399-
match event.data {
400-
Value::Individual{..} | Value::Object{..} => {
401-
return Err(FromEventError::InterfaceType(InterfaceTypeError::with_path(
402-
event.interface,
403-
event.path,
404-
InterfaceTypeDef::Properties,
405-
InterfaceTypeDef::Datastream,
406-
)));
407-
},
408-
Value::Property(Some(prop)) => {
409-
prop.try_into()
410-
.map(|value| #name::#variant(value))
411-
.map_err(FromEventError::from)
412-
},
413-
Value::Property(None) => {
414-
return Err(FromEventError::Unset {
415-
interface: INTERFACE,
416-
endpoint: event.path,
417-
});
418-
},
419-
}
383+
return Err(FromEventError::Unset {
384+
interface: INTERFACE,
385+
endpoint: event.path,
386+
});
387+
}
388+
};
389+
390+
quote! {
391+
#i => {
392+
match event.data {
393+
Value::Individual{..} | Value::Object{..} => {
394+
return Err(FromEventError::InterfaceType(InterfaceTypeError::with_path(
395+
event.interface,
396+
event.path,
397+
InterfaceTypeDef::Properties,
398+
InterfaceTypeDef::Datastream,
399+
)));
400+
},
401+
Value::Property(Some(prop)) => {
402+
prop.try_into()
403+
.map(|value| #name::#variant(#prop_set_case))
404+
.map_err(FromEventError::from)
405+
},
406+
Value::Property(None) => {
407+
#prop_unset
408+
},
420409
}
421410
}
422411
}

0 commit comments

Comments
 (0)