-
Notifications
You must be signed in to change notification settings - Fork 97
rust: release cleanup #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rust: release cleanup #1071
Changes from all commits
bd593bc
a150889
fce7b6b
b1103c4
87b8e06
0f06e47
d457212
958975b
4f70dc3
306afa1
3f21635
568e4a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
| // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
| // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| //! SBP message definitions. | ||
|
|
||
| ((*- for m in mods *)) | ||
| pub mod (((m))); | ||
|
|
@@ -22,194 +23,204 @@ use self::(((p.identifier|mod_name)))::(((m.identifier|camel_case))); | |
| ((*- endfor *)) | ||
| use self::unknown::Unknown; | ||
|
|
||
| use crate::serialize::SbpSerialize; | ||
| mod lib { | ||
| //! Common imports so we can just `use super::lib::*` in all the message files | ||
|
|
||
| pub trait SBPMessage: SbpSerialize { | ||
| fn get_message_name(&self) -> &'static str; | ||
| fn get_message_type(&self) -> u16; | ||
| fn get_sender_id(&self) -> Option<u16>; | ||
| pub use std::convert::{TryFrom, TryInto}; | ||
|
|
||
| pub use crate::wire_format::{WireFormat, PayloadParseError}; | ||
| pub use crate::sbp_string::{SbpString, Unterminated, NullTerminated, Multipart, DoubleNullTerminated}; | ||
|
|
||
| #[cfg(feature = "swiftnav")] | ||
| pub use crate::time; | ||
|
|
||
| pub use super::{ConcreteMessage, Sbp, SbpMessage, TryFromSbpError}; | ||
| } | ||
|
|
||
| use lib::*; | ||
|
|
||
| /// Common functionality available to all SBP messages. | ||
| pub trait SbpMessage: WireFormat + Clone + Sized { | ||
| /// Get the message name. | ||
| fn message_name(&self) -> &'static str; | ||
| /// Get the message type. | ||
| fn message_type(&self) -> u16; | ||
| /// Get the sender_id if it is set. | ||
| fn sender_id(&self) -> Option<u16>; | ||
| /// Set the sender id. | ||
| fn set_sender_id(&mut self, new_id: u16); | ||
| fn to_frame(&self) -> std::result::Result<Vec<u8>, crate::FramerError>; | ||
| fn write_frame(&self, buf: &mut Vec<u8>) -> std::result::Result<(), crate::FramerError>; | ||
| /// Get the GPS time associated with the message. | ||
| #[cfg(feature = "swiftnav")] | ||
| fn gps_time(&self) -> Option<std::result::Result<crate::time::MessageTime, crate::time::GpsTimeError>> { | ||
| fn gps_time(&self) -> Option<Result<crate::time::MessageTime, crate::time::GpsTimeError>> { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| pub trait ConcreteMessage: SBPMessage + std::convert::TryFrom<SBP, Error = TryFromSBPError> + Clone + Sized { | ||
| /// Implemented by messages who's message name and type are known at compile time. | ||
| /// This is everything that implements [SbpMessage] except for [Sbp]. | ||
| pub trait ConcreteMessage: SbpMessage + TryFrom<Sbp, Error = TryFromSbpError> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm it can't be marked private because it a appears in |
||
| /// The message type. | ||
| const MESSAGE_TYPE: u16; | ||
| /// The message name. | ||
| const MESSAGE_NAME: &'static str; | ||
| } | ||
|
|
||
| /// The error returned when using [TryFrom] to convert [Sbp] to the wrong message type. | ||
| #[derive(Debug, Clone)] | ||
| pub struct TryFromSBPError; | ||
| pub struct TryFromSbpError; | ||
|
|
||
| impl std::fmt::Display for TryFromSBPError { | ||
| impl std::fmt::Display for TryFromSbpError { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "invalid message type for conversion") | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for TryFromSBPError {} | ||
| impl std::error::Error for TryFromSbpError {} | ||
|
|
||
| #[cfg_attr(feature = "sbp_serde", derive(serde::Serialize), serde(untagged))] | ||
| /// Represents any SBP message. | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize), serde(untagged))] | ||
| #[derive(Debug, Clone)] | ||
| pub enum SBP { | ||
| #[non_exhaustive] | ||
| pub enum Sbp { | ||
| ((*- for m in msgs *)) | ||
| /// (((m.short_desc | commentify(indent=2) ))) | ||
| (((m.identifier|camel_case)))( (((m.identifier|camel_case))) ), | ||
| ((*- endfor *)) | ||
| /// Unknown message type | ||
| Unknown( Unknown ), | ||
| } | ||
|
|
||
| impl SBP { | ||
| pub fn parse(msg_id: u16, sender_id: u16, payload: &mut &[u8]) -> Result<SBP, crate::Error> { | ||
| match msg_id { | ||
| impl Sbp { | ||
| pub(crate) fn from_frame(mut frame: crate::de::Frame) -> Result<Sbp, PayloadParseError> { | ||
| match frame.msg_type { | ||
| ((*- for m in msgs *)) | ||
| (((m.sbp_id))) => { | ||
| let mut msg = (((m.identifier|camel_case)))::parse(payload)?; | ||
| msg.set_sender_id(sender_id); | ||
| Ok(SBP::(((m.identifier|camel_case)))(msg)) | ||
| (((m.identifier|camel_case)))::MESSAGE_TYPE => { | ||
| let mut msg = (((m.identifier|camel_case)))::parse(&mut frame.payload)?; | ||
| msg.set_sender_id(frame.sender_id); | ||
| Ok(Sbp::(((m.identifier|camel_case)))(msg)) | ||
| }, | ||
| ((*- endfor *)) | ||
| _ => Ok(SBP::Unknown( Unknown{ msg_id: msg_id, sender_id: sender_id, payload: payload.to_vec() } )) | ||
| _ => { | ||
| let mut msg = Unknown::parse(&mut frame.payload)?; | ||
| msg.set_sender_id(frame.sender_id); | ||
| Ok(Sbp::Unknown(msg)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl crate::SBPMessage for SBP { | ||
| fn get_message_name(&self) -> &'static str { | ||
| impl SbpMessage for Sbp { | ||
| fn message_name(&self) -> &'static str { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.get_message_name() | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| msg.message_name() | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.get_message_name() | ||
| Sbp::Unknown(msg) => { | ||
| msg.message_name() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn get_message_type(&self) -> u16 { | ||
| fn message_type(&self) -> u16 { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.get_message_type() | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| msg.message_type() | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.get_message_type() | ||
| Sbp::Unknown(msg) => { | ||
| msg.message_type() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn get_sender_id(&self) -> Option<u16> { | ||
| fn sender_id(&self) -> Option<u16> { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.get_sender_id() | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| msg.sender_id() | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.get_sender_id() | ||
| Sbp::Unknown(msg) => { | ||
| msg.sender_id() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn set_sender_id(&mut self, new_id: u16) { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| msg.set_sender_id(new_id) | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| Sbp::Unknown(msg) => { | ||
| msg.set_sender_id(new_id) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn to_frame(&self) -> Result<Vec<u8>, crate::FramerError> { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.to_frame() | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.to_frame() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn write_frame(&self, buf: &mut Vec<u8>) -> Result<(), crate::FramerError> { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.write_frame(buf) | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.write_frame(buf) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "swiftnav")] | ||
| fn gps_time(&self) -> Option<std::result::Result<crate::time::MessageTime, crate::time::GpsTimeError>> { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| msg.gps_time() | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| Sbp::Unknown(msg) => { | ||
| msg.gps_time() | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl WireFormat for Sbp { | ||
| const MIN_ENCODED_LEN: usize = crate::MAX_FRAME_LEN; | ||
|
|
||
| fn parse_unchecked(_: &mut bytes::BytesMut) -> Self { | ||
| unimplemented!("Sbp must be parsed with Sbp::from_frame"); | ||
| } | ||
|
|
||
| impl crate::SbpSerialize for SBP { | ||
| fn append_to_sbp_buffer(&self, buf: &mut Vec<u8>) { | ||
| fn write(&self, buf: &mut bytes::BytesMut) { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.append_to_sbp_buffer(buf) | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| WireFormat::write(msg, buf) | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.append_to_sbp_buffer(buf) | ||
| Sbp::Unknown(msg) => { | ||
| WireFormat::write(msg, buf) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn sbp_size(&self) -> usize { | ||
| fn encoded_len(&self) -> usize { | ||
| match self { | ||
| ((*- for m in msgs *)) | ||
| SBP::(((m.identifier|camel_case)))(msg) => { | ||
| msg.sbp_size() | ||
| Sbp::(((m.identifier|camel_case)))(msg) => { | ||
| WireFormat::encoded_len(msg) | ||
| }, | ||
| ((*- endfor *)) | ||
| SBP::Unknown(msg) => { | ||
| msg.sbp_size() | ||
| Sbp::Unknown(msg) => { | ||
| WireFormat::encoded_len(msg) | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ((*- for m in msgs *)) | ||
| impl From<(((m.identifier|camel_case)))> for SBP { | ||
| ((* for m in msgs *)) | ||
| impl From<(((m.identifier|camel_case)))> for Sbp { | ||
| fn from(msg: (((m.identifier|camel_case)))) -> Self { | ||
| SBP::(((m.identifier|camel_case)))(msg) | ||
| Sbp::(((m.identifier|camel_case)))(msg) | ||
| } | ||
| } | ||
| ((*- endfor *)) | ||
|
|
||
| impl From<Unknown> for SBP { | ||
| } | ||
| ((* endfor *)) | ||
| impl From<Unknown> for Sbp { | ||
| fn from(msg: Unknown) -> Self { | ||
| SBP::Unknown(msg) | ||
| Sbp::Unknown(msg) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be called a prelude? Or is that term just for common imports from an external crate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yeah I just took the idea from serde_json where they call it lib - https://docs.serde.rs/src/serde_json/lib.rs.html#366
Although they are doing it for a slightly different reason (still internal though)