Skip to content

Commit cbfdce1

Browse files
committed
change return type on message functions to option
1 parent a087574 commit cbfdce1

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

mavlink-bindgen/src/parser.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ impl MavProfile {
284284

285285
fn emit_mav_message_id_from_name(&self, structs: &[TokenStream]) -> TokenStream {
286286
quote! {
287-
fn message_id_from_name(name: &str) -> Result<u32, &'static str> {
287+
fn message_id_from_name(name: &str) -> Option<u32> {
288288
match name {
289-
#(#structs::NAME => Ok(#structs::ID),)*
289+
#(#structs::NAME => Some(#structs::ID),)*
290290
_ => {
291-
Err("Invalid message name.")
291+
None
292292
}
293293
}
294294
}
@@ -301,11 +301,11 @@ impl MavProfile {
301301
structs: &[TokenStream],
302302
) -> TokenStream {
303303
quote! {
304-
fn default_message_from_id(id: u32) -> Result<Self, &'static str> {
304+
fn default_message_from_id(id: u32) -> Option<Self> {
305305
match id {
306-
#(#structs::ID => Ok(Self::#enums(#structs::default())),)*
306+
#(#structs::ID => Some(Self::#enums(#structs::default())),)*
307307
_ => {
308-
Err("Invalid message id.")
308+
None
309309
}
310310
}
311311
}
@@ -319,10 +319,10 @@ impl MavProfile {
319319
) -> TokenStream {
320320
quote! {
321321
#[cfg(feature = "arbitrary")]
322-
fn random_message_from_id<R: rand::RngCore>(id: u32, rng: &mut R) -> Result<Self, &'static str> {
322+
fn random_message_from_id<R: rand::RngCore>(id: u32, rng: &mut R) -> Option<Self> {
323323
match id {
324-
#(#structs::ID => Ok(Self::#enums(#structs::random(rng))),)*
325-
_ => Err("Invalid message id."),
324+
#(#structs::ID => Some(Self::#enums(#structs::random(rng))),)*
325+
_ => None,
326326
}
327327
}
328328
}

mavlink-core/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,12 @@ where
134134
) -> Result<Self, error::ParserError>;
135135

136136
/// Return message id of specific message name
137-
fn message_id_from_name(name: &str) -> Result<u32, &'static str>;
137+
fn message_id_from_name(name: &str) -> Option<u32>;
138138
/// Return a default message of the speicfied message id
139-
fn default_message_from_id(id: u32) -> Result<Self, &'static str>;
139+
fn default_message_from_id(id: u32) -> Option<Self>;
140140
/// Return random valid message of the speicfied message id
141141
#[cfg(feature = "arbitrary")]
142-
fn random_message_from_id<R: rand::RngCore>(id: u32, rng: &mut R)
143-
-> Result<Self, &'static str>;
142+
fn random_message_from_id<R: rand::RngCore>(id: u32, rng: &mut R) -> Option<Self>;
144143
/// Return a message types [CRC_EXTRA byte](https://mavlink.io/en/guide/serialization.html#crc_extra)
145144
fn extra_crc(id: u32) -> u8;
146145
}

mavlink/tests/helper_tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ mod helper_tests {
55
#[test]
66
fn test_get_default_message_from_id() {
77
let message_name = "PING";
8-
let id: std::result::Result<u32, &'static str> =
9-
MavMessage::message_id_from_name(message_name);
8+
let id: Option<u32> = MavMessage::message_id_from_name(message_name);
109
let id = id.unwrap();
1110
assert!(id == 4, "Invalid id for message name: PING");
1211
let message = MavMessage::default_message_from_id(id);
13-
if !matches!(message, Ok(MavMessage::PING(_))) {
12+
if !matches!(message, Some(MavMessage::PING(_))) {
1413
unreachable!("Invalid message type.")
1514
}
1615
assert!(

0 commit comments

Comments
 (0)