Skip to content

Commit d7b46e0

Browse files
committed
Fix review comments
1 parent e8eea75 commit d7b46e0

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

mavlink-bindgen/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ impl MavMessage {
710710
quote!()
711711
};
712712

713-
let serde_with_attr = if matches!(field.mavtype, MavType::Array(_, _)) {
713+
let serde_with_attr = if matches!(field.mavtype, MavType::Array(_, _) | MavType::CharArray(_)) {
714714
quote!(
715715
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
716716
#[cfg_attr(feature = "ts", ts(type = "Array<number>"))]

mavlink-bindgen/tests/snapshots/[email protected]

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: mavlink-bindgen/tests/e2e_snapshots.rs
3-
assertion_line: 26
43
expression: contents
54
---
65
#![doc = "MAVLink deprecated dialect."]
@@ -11,7 +10,9 @@ expression: contents
1110
use arbitrary::Arbitrary;
1211
#[allow(unused_imports)]
1312
use bitflags::bitflags;
14-
use mavlink_core::{bytes::Bytes, bytes_mut::BytesMut, MavlinkVersion, Message, MessageData};
13+
use mavlink_core::{
14+
bytes::Bytes, bytes_mut::BytesMut, types::CharArray, MavlinkVersion, Message, MessageData,
15+
};
1516
#[allow(unused_imports)]
1617
use num_derive::FromPrimitive;
1718
#[allow(unused_imports)]

mavlink-bindgen/tests/snapshots/e2e_snapshots__no_field_description.xml@no_field_description.rs.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: mavlink-bindgen/tests/e2e_snapshots.rs
3-
assertion_line: 26
43
expression: contents
54
---
65
#![doc = "MAVLink no_field_description dialect."]
@@ -11,7 +10,9 @@ expression: contents
1110
use arbitrary::Arbitrary;
1211
#[allow(unused_imports)]
1312
use bitflags::bitflags;
14-
use mavlink_core::{bytes::Bytes, bytes_mut::BytesMut, MavlinkVersion, Message, MessageData};
13+
use mavlink_core::{
14+
bytes::Bytes, bytes_mut::BytesMut, types::CharArray, MavlinkVersion, Message, MessageData,
15+
};
1516
#[allow(unused_imports)]
1617
use num_derive::FromPrimitive;
1718
#[allow(unused_imports)]

mavlink-core/src/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use arbitrary::{Arbitrary, Unstructured};
2020
/// let ca = CharArray::new(data);
2121
/// assert_eq!(ca.to_str(), "HELLO");
2222
/// ```
23-
#[derive(Debug, PartialEq, Clone, Copy)]
2423
#[cfg_attr(feature = "serde", derive(Serialize))]
2524
#[cfg_attr(feature = "serde", serde(transparent))]
25+
#[derive(Debug, PartialEq, Clone, Copy)]
2626
pub struct CharArray<const N: usize> {
2727
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
2828
data: [u8; N],
@@ -56,8 +56,8 @@ impl<const N: usize> CharArray<N> {
5656
/// Get the string representation of the char array.
5757
/// Returns the string stopping at the first null byte and if the string is not valid utf8
5858
/// the returned string will be empty.
59-
pub fn to_str(&self) -> &str {
60-
core::str::from_utf8(&self.data[..self.str_len]).unwrap_or("")
59+
pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
60+
core::str::from_utf8(&self.data[..self.str_len])
6161
}
6262
}
6363

@@ -132,7 +132,7 @@ mod tests {
132132
let data = *b"HELLOWORLD";
133133
let ca = CharArray::new(data);
134134
assert_eq!(ca.len(), 10);
135-
assert_eq!(ca.to_str(), "HELLOWORLD");
135+
assert_eq!(ca.to_str().unwrap(), "HELLOWORLD");
136136
}
137137

138138
#[test]
@@ -142,6 +142,6 @@ mod tests {
142142
// data[3..] are zeros
143143
let ca = CharArray::new(data);
144144
assert_eq!(ca.len(), 10);
145-
assert_eq!(ca.to_str(), "abc");
145+
assert_eq!(ca.to_str().unwrap(), "abc");
146146
}
147147
}

0 commit comments

Comments
 (0)