Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades authored and daniel.eades committed Mar 20, 2023
1 parent 6dfd5f7 commit 537439c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 61 deletions.
92 changes: 37 additions & 55 deletions build/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ impl MavProfile {
fn update_enums(mut self) -> Self {
for msg in self.messages.values() {
for field in &msg.fields {
if let Some(ref enum_name) = field.enumtype {
// it is an enum
if let Some(ref dsp) = field.display {
// it is a bitmask
if dsp == "bitmask" {
// find the corresponding enum
for mut enm in self.enums.values_mut() {
if enm.name == *enum_name {
// this is the right enum
enm.bitfield = Some(field.mavtype.rust_type());
}
if let Some(enum_name) = &field.enumtype {
// it is a bitmask
if let Some("bitmask") = field.display.as_deref() {
// find the corresponding enum
for mut enm in self.enums.values_mut() {
if enm.name == *enum_name {
// this is the right enum
enm.bitfield = Some(field.mavtype.rust_type());
}
}
}
Expand All @@ -88,7 +85,7 @@ impl MavProfile {
// }

/// Simple header comment
fn emit_comments(&self) -> TokenStream {
fn emit_comments() -> TokenStream {
quote!(#![doc = "This file was automatically generated, do not edit"])
}

Expand Down Expand Up @@ -147,23 +144,23 @@ impl MavProfile {
//TODO verify that id_width of u8 is OK even in mavlink v1
let id_width = format_ident!("u32");

let comment = self.emit_comments();
let comment = Self::emit_comments();
let msgs = self.emit_msgs();
let enum_names = self.emit_enum_names();
let struct_names = self.emit_struct_names();
let enums = self.emit_enums();
let msg_ids = self.emit_msg_ids();
let msg_crc = self.emit_msg_crc();

let mav_message = self.emit_mav_message(&enum_names, &struct_names);
let mav_message_parse = self.emit_mav_message_parse(&enum_names, &struct_names, &msg_ids);
let mav_message_crc = self.emit_mav_message_crc(&id_width, &msg_ids, &msg_crc);
let mav_message_name = self.emit_mav_message_name(&enum_names);
let mav_message_id = self.emit_mav_message_id(&enum_names, &msg_ids);
let mav_message_id_from_name = self.emit_mav_message_id_from_name(&enum_names, &msg_ids);
let mav_message = Self::emit_mav_message(&enum_names, &struct_names);
let mav_message_parse = Self::emit_mav_message_parse(&enum_names, &struct_names, &msg_ids);
let mav_message_crc = Self::emit_mav_message_crc(&id_width, &msg_ids, &msg_crc);
let mav_message_name = Self::emit_mav_message_name(&enum_names);
let mav_message_id = Self::emit_mav_message_id(&enum_names, &msg_ids);
let mav_message_id_from_name = Self::emit_mav_message_id_from_name(&enum_names, &msg_ids);
let mav_message_default_from_id =
self.emit_mav_message_default_from_id(&enum_names, &msg_ids);
let mav_message_serialize = self.emit_mav_message_serialize(&enum_names);
Self::emit_mav_message_default_from_id(&enum_names, &msg_ids);
let mav_message_serialize = Self::emit_mav_message_serialize(&enum_names);

quote! {
#comment
Expand Down Expand Up @@ -205,11 +202,7 @@ impl MavProfile {
}
}

fn emit_mav_message(
&self,
enums: &Vec<TokenStream>,
structs: &Vec<TokenStream>,
) -> TokenStream {
fn emit_mav_message(enums: &Vec<TokenStream>, structs: &Vec<TokenStream>) -> TokenStream {
quote! {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
Expand All @@ -220,7 +213,6 @@ impl MavProfile {
}

fn emit_mav_message_parse(
&self,
enums: &[TokenStream],
structs: &[TokenStream],
ids: &[TokenStream],
Expand All @@ -240,7 +232,6 @@ impl MavProfile {
}

fn emit_mav_message_crc(
&self,
id_width: &Ident,
ids: &[TokenStream],
crc: &[TokenStream],
Expand All @@ -257,7 +248,7 @@ impl MavProfile {
}
}

fn emit_mav_message_name(&self, enums: &Vec<TokenStream>) -> TokenStream {
fn emit_mav_message_name(enums: &Vec<TokenStream>) -> TokenStream {
let enum_names = enums.iter().map(|enum_name| {
let name = enum_name.to_string();
quote!(#name)
Expand All @@ -272,7 +263,7 @@ impl MavProfile {
}
}

fn emit_mav_message_id(&self, enums: &Vec<TokenStream>, ids: &Vec<TokenStream>) -> TokenStream {
fn emit_mav_message_id(enums: &Vec<TokenStream>, ids: &Vec<TokenStream>) -> TokenStream {
let id_width = format_ident!("u32");
quote! {
fn message_id(&self) -> #id_width {
Expand All @@ -283,11 +274,7 @@ impl MavProfile {
}
}

fn emit_mav_message_id_from_name(
&self,
enums: &[TokenStream],
ids: &[TokenStream],
) -> TokenStream {
fn emit_mav_message_id_from_name(enums: &[TokenStream], ids: &[TokenStream]) -> TokenStream {
let enum_names = enums.iter().map(|enum_name| {
let name = enum_name.to_string();
quote!(#name)
Expand All @@ -305,11 +292,7 @@ impl MavProfile {
}
}

fn emit_mav_message_default_from_id(
&self,
enums: &[TokenStream],
ids: &[TokenStream],
) -> TokenStream {
fn emit_mav_message_default_from_id(enums: &[TokenStream], ids: &[TokenStream]) -> TokenStream {
let data_name = enums.iter().map(|enum_name| {
let name = format_ident!("{}_DATA", enum_name.to_string());
quote!(#name)
Expand All @@ -327,7 +310,7 @@ impl MavProfile {
}
}

fn emit_mav_message_serialize(&self, enums: &Vec<TokenStream>) -> TokenStream {
fn emit_mav_message_serialize(enums: &Vec<TokenStream>) -> TokenStream {
quote! {
fn ser(&self, version: MavlinkVersion, bytes: &mut [u8]) -> usize {
match self {
Expand Down Expand Up @@ -372,7 +355,7 @@ impl MavEnum {
let value;

#[cfg(feature = "emit-description")]
let description = if let Some(description) = enum_entry.description.as_ref() {
let description = if let Some(description) = &enum_entry.description {
quote!(#[doc = #description])
} else {
quote!()
Expand Down Expand Up @@ -416,9 +399,8 @@ impl MavEnum {
let enum_name = self.emit_name();

#[cfg(feature = "emit-description")]
let description = if let Some(description) = self.description.as_ref() {
let desc = format!("{description}");
quote!(#[doc = #desc])
let description = if let Some(description) = &self.description {
quote!(#[doc = #description])
} else {
quote!()
};
Expand Down Expand Up @@ -518,7 +500,7 @@ impl MavMessage {
#nametype
}
})
.collect::<Vec<TokenStream>>();
.collect();
(field_toks, encoded_payload_len)
}

Expand Down Expand Up @@ -642,7 +624,7 @@ impl MavField {
if matches!(self.mavtype, MavType::Array(_, _)) {
let rt = TokenStream::from_str(&self.mavtype.rust_type()).unwrap();
mavtype = quote!(#rt);
} else if let Some(ref enumname) = self.enumtype {
} else if let Some(enumname) = &self.enumtype {
let en = TokenStream::from_str(enumname).unwrap();
mavtype = quote!(#en);
} else {
Expand Down Expand Up @@ -1090,7 +1072,7 @@ pub fn parse_profile(
let attr = attr.unwrap();
match stack.last() {
Some(&MavXmlElement::Enum) => {
if let b"name" = attr.key.into_inner() {
if attr.key.into_inner() == b"name" {
mavenum.name = attr
.value
.clone()
Expand Down Expand Up @@ -1191,7 +1173,7 @@ pub fn parse_profile(
if entry.params.is_none() {
entry.params = Some(vec![]);
}
if let b"index" = attr.key.into_inner() {
if attr.key.into_inner() == b"index" {
let s = std::str::from_utf8(&attr.value).unwrap();
paramid = Some(s.parse::<usize>().unwrap());
}
Expand Down Expand Up @@ -1241,7 +1223,7 @@ pub fn parse_profile(
entry.description = Some(s.replace('\n', " "));
}
(Some(&Param), Some(&Entry)) => {
if let Some(ref mut params) = entry.params {
if let Some(params) = &mut entry.params {
// Some messages can jump between values, like:
// 0, 1, 2, 7
if params.len() < paramid.unwrap() {
Expand Down Expand Up @@ -1350,21 +1332,21 @@ pub fn extra_crc(msg: &MavMessage) -> u8 {
let mut crc = CRCu16::crc16mcrf4cc();

crc.digest(msg.name.as_bytes());
crc.digest(" ".as_bytes());
crc.digest(b" ");

let mut f = msg.fields.clone();
// only mavlink 1 fields should be part of the extra_crc
f.retain(|f| !f.is_extension);
f.sort_by(|a, b| a.mavtype.compare(&b.mavtype));
for field in &f {
crc.digest(field.mavtype.primitive_type().as_bytes());
crc.digest(" ".as_bytes());
crc.digest(b" ");
if field.name == "mavtype" {
crc.digest("type".as_bytes());
crc.digest(b"type");
} else {
crc.digest(field.name.as_bytes());
}
crc.digest(" ".as_bytes());
crc.digest(b" ");
if let MavType::Array(_, size) = field.mavtype {
crc.digest(&[size as u8]);
}
Expand Down Expand Up @@ -1435,7 +1417,7 @@ impl MavXmlFilter {
Some(kind) => kind,
};

if let MavXmlElement::Message = id {
if id == MavXmlElement::Message {
self.extension_filter.is_in = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/mavlink-dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() {
println!("received: {msg:?}");
}
Err(MessageReadError::Io(e)) => {
if let std::io::ErrorKind::WouldBlock = e.kind() {
if e.kind() == std::io::ErrorKind::WouldBlock {
//no messages currently available to receive -- wait a while
thread::sleep(Duration::from_secs(1));
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub trait Read {
fn read_u8(&mut self) -> Result<u8, MessageReadError>;

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), MessageReadError> {
for i in 0..buf.len() {
buf[i] = self.read_u8()?;
for byte in buf {
*byte = self.read_u8()?;
}

Ok(())
Expand All @@ -26,8 +26,8 @@ pub trait Write {

impl<W: embedded_hal::serial::Write<u8>> Write for W {
fn write_all(&mut self, buf: &[u8]) -> Result<(), MessageWriteError> {
for i in 0..buf.len() {
nb::block!(self.write(buf[i])).map_err(|_| MessageWriteError::Io)?;
for byte in buf {
nb::block!(self.write(*byte)).map_err(|_| MessageWriteError::Io)?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/process_log_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod process_files {
counter += 1;
}
Err(MessageReadError::Io(e)) => {
if let std::io::ErrorKind::WouldBlock = e.kind() {
if e.kind() == std::io::ErrorKind::WouldBlock {
continue;
} else {
println!("recv error: {e:?}");
Expand Down

0 comments on commit 537439c

Please sign in to comment.