Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions build/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum PayloadType {
I16,
I32,
F32,
F64,
VECTOR(Box<VectorType>),
}

Expand All @@ -41,6 +42,7 @@ impl PayloadType {
"i16" | "int16_t" => PayloadType::I16,
"i32" | "int32_t" => PayloadType::I32,
"float" => PayloadType::F32,
"double" => PayloadType::F64,
"vector" => panic!("Can't convert vector with from_string."),
something => panic!("No available type: {:#?}", something),
}
Expand Down Expand Up @@ -73,6 +75,7 @@ impl PayloadType {
PayloadType::I16 => quote! {i16},
PayloadType::I32 => quote! {i32},
PayloadType::F32 => quote! {f32},
PayloadType::F64 => quote! {f64},
PayloadType::VECTOR(_vector) => panic!("Can't convert vector to rust."),
}
}
Expand All @@ -82,6 +85,7 @@ impl PayloadType {
PayloadType::CHAR | PayloadType::U8 | PayloadType::I8 => 1,
PayloadType::U16 | PayloadType::I16 => 2,
PayloadType::U32 | PayloadType::I32 | PayloadType::F32 => 4,
PayloadType::F64 => 8,
PayloadType::VECTOR(_) => 0,
}
}
Expand Down Expand Up @@ -383,7 +387,7 @@ impl MessageDefinition {
b += field.typ.to_size();
value
}
PayloadType::U16 | PayloadType::I16 | PayloadType::U32 | PayloadType::I32 | PayloadType::F32 => {
PayloadType::U16 | PayloadType::I16 | PayloadType::U32 | PayloadType::I32 | PayloadType::F32 | PayloadType::F64 => {
let data_type = field.typ.to_rust();
let data_size = field.typ.to_size();
let field_token = quote! {
Expand Down Expand Up @@ -411,11 +415,12 @@ impl MessageDefinition {
PayloadType::U32 |
PayloadType::I16 |
PayloadType::I32 |
PayloadType::F32 => quote! {
PayloadType::F32 |
PayloadType::F64=> quote! {
payload[#b + #length_size..payload.len()]
.chunks_exact(#data_size)
.into_iter()
.map(|a| u16::from_le_bytes((*a).try_into().expect("Wrong slice length")))
.map(|a| #data_type::from_le_bytes((*a).try_into().expect("Wrong slice length")))
.collect::<Vec<#data_type>>()
},
PayloadType::VECTOR(_) => unimplemented!("Vector of vectors are not supported"),
Expand All @@ -442,6 +447,7 @@ impl MessageDefinition {
.collect();

let id = self.id;
let name = self.name.clone();
quote! {
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -470,6 +476,9 @@ impl MessageDefinition {
fn id() -> u16 {
#id
}
fn name() -> &'static str {
#name
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,17 @@ pub trait PingDevice {
};
let message = Messages::try_from(&answer)
.map_err(|_e| PingError::TryFromError(answer))?;
return Ok(message.inner::<T>().unwrap().clone());
match message.inner::<T>() {
Some(message) => return Ok(message.clone()),
None => {
error!(
"Received message is not of type `{}` ({}), receiving: {:?}",
T::name(),
T::id(),
message,
);
}
}
}
Err(broadcast::error::RecvError::Lagged(_)) => continue,
Err(e) => return Err(e.into()),
Expand Down
1 change: 1 addition & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub trait DeserializePayload {

pub trait MessageInfo {
fn id() -> u16;
fn name() -> &'static str;
}

pub trait DeserializeGenericMessage
Expand Down