Skip to content
Open
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
74 changes: 45 additions & 29 deletions mavlink-bindgen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl MavProfile {
#[allow(unused_imports)]
use bitflags::bitflags;

use mavlink_core::{MavlinkVersion, Message, MessageData, bytes::Bytes, bytes_mut::BytesMut};
use mavlink_core::{MavlinkVersion, Message, MessageData, bytes::Bytes, bytes_mut::BytesMut, types::CharArray};

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
Expand Down Expand Up @@ -710,23 +710,15 @@ impl MavMessage {
quote!()
};

let serde_with_attr = if let MavType::Array(_, size) = field.mavtype {
if field.mavtype.primitive_type() == "char" {
let format_serialize = format!("crate::nulstr::serialize::<_, {}>", size);
let format_deserialize = format!("crate::nulstr::deserialize::<_, {}>", size);
quote!(
#[cfg_attr(feature = "serde", serde(
serialize_with = #format_serialize,
deserialize_with = #format_deserialize
))]
#[cfg_attr(feature = "ts", ts(type = "string"))]
)
} else {
quote!(
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
#[cfg_attr(feature = "ts", ts(type = "Array<number>"))]
)
}
let serde_with_attr = if matches!(field.mavtype, MavType::Array(_, _)) {
quote!(
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
#[cfg_attr(feature = "ts", ts(type = "Array<number>"))]
)
} else if matches!(field.mavtype, MavType::CharArray(_)) {
quote!(
#[cfg_attr(feature = "ts", ts(type = "string"))]
)
} else {
quote!()
};
Expand Down Expand Up @@ -1113,6 +1105,7 @@ pub enum MavType {
Char,
Float,
Double,
CharArray(usize),
Array(Box<MavType>, usize),
}

Expand All @@ -1133,16 +1126,18 @@ impl MavType {
"float" => Some(Float),
"Double" => Some(Double),
"double" => Some(Double),
_ => {
if s.ends_with(']') {
let start = s.find('[')?;
let size = s[start + 1..(s.len() - 1)].parse::<usize>().ok()?;
let mtype = Self::parse_type(&s[0..start])?;
Some(Array(Box::new(mtype), size))
} else {
None
}
_ if s.starts_with("char[") => {
let start = 4;
let size = s[start + 1..(s.len() - 1)].parse::<usize>().ok()?;
Some(CharArray(size))
}
_ if s.ends_with(']') => {
let start = s.find('[')?;
let size = s[start + 1..(s.len() - 1)].parse::<usize>().ok()?;
let mtype = Self::parse_type(&s[0..start])?;
Some(Array(Box::new(mtype), size))
}
_ => None,
}
}

Expand All @@ -1162,6 +1157,15 @@ impl MavType {
Int64 => quote! {#val = #buf.get_i64_le();},
Float => quote! {#val = #buf.get_f32_le();},
Double => quote! {#val = #buf.get_f64_le();},
CharArray(size) => {
quote! {
let mut tmp = [0_u8; #size];
for v in &mut tmp {
*v = #buf.get_u8();
}
#val = CharArray::new(tmp);
}
}
Array(t, _) => {
let r = t.rust_reader(&quote!(let val), buf);
quote! {
Expand Down Expand Up @@ -1190,6 +1194,14 @@ impl MavType {
UInt64 => quote! {#buf.put_u64_le(#val);},
Int64 => quote! {#buf.put_i64_le(#val);},
Double => quote! {#buf.put_f64_le(#val);},
CharArray(_) => {
let w = Char.rust_writer(&quote!(*val), buf);
quote! {
for val in &#val {
#w
}
}
}
Array(t, _size) => {
let w = t.rust_writer(&quote!(*val), buf);
quote! {
Expand All @@ -1209,6 +1221,7 @@ impl MavType {
UInt16 | Int16 => 2,
UInt32 | Int32 | Float => 4,
UInt64 | Int64 | Double => 8,
CharArray(size) => *size,
Array(t, size) => t.len() * size,
}
}
Expand All @@ -1217,7 +1230,7 @@ impl MavType {
fn order_len(&self) -> usize {
use self::MavType::*;
match self {
UInt8MavlinkVersion | UInt8 | Int8 | Char => 1,
UInt8MavlinkVersion | UInt8 | Int8 | Char | CharArray(_) => 1,
UInt16 | Int16 => 2,
UInt32 | Int32 | Float => 4,
UInt64 | Int64 | Double => 8,
Expand All @@ -1241,6 +1254,7 @@ impl MavType {
UInt64 => "uint64_t".into(),
Int64 => "int64_t".into(),
Double => "double".into(),
CharArray(_) => "char".into(),
Array(t, _) => t.primitive_type(),
}
}
Expand All @@ -1261,6 +1275,7 @@ impl MavType {
UInt64 => "u64".into(),
Int64 => "i64".into(),
Double => "f64".into(),
CharArray(size) => format!("CharArray<{}>", size),
Array(t, size) => format!("[{};{}]", t.rust_type(), size),
}
}
Expand All @@ -1286,6 +1301,7 @@ impl MavType {
UInt64 => quote!(0_u64),
Int64 => quote!(0_i64),
Double => quote!(0.0_f64),
CharArray(size) => quote!(CharArray::new([0_u8; #size])),
Array(ty, size) => {
let default_value = ty.emit_default_value(dialect_has_version);
quote!([#default_value; #size])
Expand Down Expand Up @@ -1866,7 +1882,7 @@ pub fn extra_crc(msg: &MavMessage) -> u8 {
crc.digest(field.name.as_bytes());
}
crc.digest(b" ");
if let MavType::Array(_, size) = field.mavtype {
if let MavType::Array(_, size) | MavType::CharArray(size) = field.mavtype {
crc.digest(&[size as u8]);
}
}
Expand Down
5 changes: 3 additions & 2 deletions mavlink-bindgen/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: mavlink-bindgen/tests/e2e_snapshots.rs
assertion_line: 26
expression: contents
---
#![doc = "MAVLink deprecated dialect."]
Expand All @@ -11,7 +10,9 @@ expression: contents
use arbitrary::Arbitrary;
#[allow(unused_imports)]
use bitflags::bitflags;
use mavlink_core::{bytes::Bytes, bytes_mut::BytesMut, MavlinkVersion, Message, MessageData};
use mavlink_core::{
bytes::Bytes, bytes_mut::BytesMut, types::CharArray, MavlinkVersion, Message, MessageData,
};
#[allow(unused_imports)]
use num_derive::FromPrimitive;
#[allow(unused_imports)]
Expand Down
4 changes: 3 additions & 1 deletion mavlink-bindgen/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ expression: contents
use arbitrary::Arbitrary;
#[allow(unused_imports)]
use bitflags::bitflags;
use mavlink_core::{bytes::Bytes, bytes_mut::BytesMut, MavlinkVersion, Message, MessageData};
use mavlink_core::{
bytes::Bytes, bytes_mut::BytesMut, types::CharArray, MavlinkVersion, Message, MessageData,
};
#[allow(unused_imports)]
use num_derive::FromPrimitive;
#[allow(unused_imports)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: mavlink-bindgen/tests/e2e_snapshots.rs
assertion_line: 26
expression: contents
---
#![doc = "MAVLink no_field_description dialect."]
Expand All @@ -11,7 +10,9 @@ expression: contents
use arbitrary::Arbitrary;
#[allow(unused_imports)]
use bitflags::bitflags;
use mavlink_core::{bytes::Bytes, bytes_mut::BytesMut, MavlinkVersion, Message, MessageData};
use mavlink_core::{
bytes::Bytes, bytes_mut::BytesMut, types::CharArray, MavlinkVersion, Message, MessageData,
};
#[allow(unused_imports)]
use num_derive::FromPrimitive;
#[allow(unused_imports)]
Expand Down
59 changes: 21 additions & 38 deletions mavlink-bindgen/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: mavlink-bindgen/tests/e2e_snapshots.rs
assertion_line: 26
expression: contents
---
#![doc = "MAVLink parameters dialect."]
Expand All @@ -11,7 +10,9 @@ expression: contents
use arbitrary::Arbitrary;
#[allow(unused_imports)]
use bitflags::bitflags;
use mavlink_core::{bytes::Bytes, bytes_mut::BytesMut, MavlinkVersion, Message, MessageData};
use mavlink_core::{
bytes::Bytes, bytes_mut::BytesMut, types::CharArray, MavlinkVersion, Message, MessageData,
};
#[allow(unused_imports)]
use num_derive::FromPrimitive;
#[allow(unused_imports)]
Expand Down Expand Up @@ -156,23 +157,16 @@ pub struct PARAM_REQUEST_READ_DATA {
#[doc = "Component ID"]
pub target_component: u8,
#[doc = "Onboard parameter id, terminated by NULL if the length is less than 16 human-readable chars and WITHOUT null termination (NULL) byte if the length is exactly 16 chars - applications have to provide 16+1 bytes storage if the ID is stored as string"]
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::nulstr::serialize::<_, 16>",
deserialize_with = "crate::nulstr::deserialize::<_, 16>"
)
)]
#[cfg_attr(feature = "ts", ts(type = "string"))]
pub param_id: [u8; 16],
pub param_id: CharArray<16>,
}
impl PARAM_REQUEST_READ_DATA {
pub const ENCODED_LEN: usize = 20usize;
pub const DEFAULT: Self = Self {
param_index: 0_i16,
target_system: 0_u8,
target_component: 0_u8,
param_id: [0_u8; 16usize],
param_id: CharArray::new([0_u8; 16usize]),
};
#[cfg(feature = "arbitrary")]
pub fn random<R: rand::RngCore>(rng: &mut R) -> Self {
Expand Down Expand Up @@ -210,10 +204,11 @@ impl MessageData for PARAM_REQUEST_READ_DATA {
__struct.param_index = buf.get_i16_le();
__struct.target_system = buf.get_u8();
__struct.target_component = buf.get_u8();
for v in &mut __struct.param_id {
let val = buf.get_u8();
*v = val;
let mut tmp = [0_u8; 16usize];
for v in &mut tmp {
*v = buf.get_u8();
}
__struct.param_id = CharArray::new(tmp);
Ok(__struct)
}
fn ser(&self, version: MavlinkVersion, bytes: &mut [u8]) -> usize {
Expand Down Expand Up @@ -257,15 +252,8 @@ pub struct PARAM_SET_DATA {
#[doc = "Component ID"]
pub target_component: u8,
#[doc = "Onboard parameter id, terminated by NULL if the length is less than 16 human-readable chars and WITHOUT null termination (NULL) byte if the length is exactly 16 chars - applications have to provide 16+1 bytes storage if the ID is stored as string"]
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::nulstr::serialize::<_, 16>",
deserialize_with = "crate::nulstr::deserialize::<_, 16>"
)
)]
#[cfg_attr(feature = "ts", ts(type = "string"))]
pub param_id: [u8; 16],
pub param_id: CharArray<16>,
#[doc = "Onboard parameter type."]
pub param_type: MavParamType,
}
Expand All @@ -275,7 +263,7 @@ impl PARAM_SET_DATA {
param_value: 0.0_f32,
target_system: 0_u8,
target_component: 0_u8,
param_id: [0_u8; 16usize],
param_id: CharArray::new([0_u8; 16usize]),
param_type: MavParamType::DEFAULT,
};
#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -314,10 +302,11 @@ impl MessageData for PARAM_SET_DATA {
__struct.param_value = buf.get_f32_le();
__struct.target_system = buf.get_u8();
__struct.target_component = buf.get_u8();
for v in &mut __struct.param_id {
let val = buf.get_u8();
*v = val;
let mut tmp = [0_u8; 16usize];
for v in &mut tmp {
*v = buf.get_u8();
}
__struct.param_id = CharArray::new(tmp);
let tmp = buf.get_u8();
__struct.param_type =
FromPrimitive::from_u8(tmp).ok_or(::mavlink_core::error::ParserError::InvalidEnum {
Expand Down Expand Up @@ -368,15 +357,8 @@ pub struct PARAM_VALUE_DATA {
#[doc = "Index of this onboard parameter"]
pub param_index: u16,
#[doc = "Onboard parameter id, terminated by NULL if the length is less than 16 human-readable chars and WITHOUT null termination (NULL) byte if the length is exactly 16 chars - applications have to provide 16+1 bytes storage if the ID is stored as string"]
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::nulstr::serialize::<_, 16>",
deserialize_with = "crate::nulstr::deserialize::<_, 16>"
)
)]
#[cfg_attr(feature = "ts", ts(type = "string"))]
pub param_id: [u8; 16],
pub param_id: CharArray<16>,
#[doc = "Onboard parameter type."]
pub param_type: MavParamType,
}
Expand All @@ -386,7 +368,7 @@ impl PARAM_VALUE_DATA {
param_value: 0.0_f32,
param_count: 0_u16,
param_index: 0_u16,
param_id: [0_u8; 16usize],
param_id: CharArray::new([0_u8; 16usize]),
param_type: MavParamType::DEFAULT,
};
#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -425,10 +407,11 @@ impl MessageData for PARAM_VALUE_DATA {
__struct.param_value = buf.get_f32_le();
__struct.param_count = buf.get_u16_le();
__struct.param_index = buf.get_u16_le();
for v in &mut __struct.param_id {
let val = buf.get_u8();
*v = val;
let mut tmp = [0_u8; 16usize];
for v in &mut tmp {
*v = buf.get_u8();
}
__struct.param_id = CharArray::new(tmp);
let tmp = buf.get_u8();
__struct.param_type =
FromPrimitive::from_u8(tmp).ok_or(::mavlink_core::error::ParserError::InvalidEnum {
Expand Down
1 change: 1 addition & 0 deletions mavlink-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub mod bytes_mut;
#[cfg(feature = "std")]
mod connection;
pub mod error;
pub mod types;
#[cfg(feature = "std")]
pub use self::connection::{connect, Connectable, MavConnection};

Expand Down
Loading