1+ //! Typed Protobuf message support for `rustecal`.
2+ //!
3+ //! This crate provides a wrapper around `prost::Message`-based types, allowing
4+ //! seamless integration with `TypedPublisher` and `TypedSubscriber` from the `rustecal` API.
5+ //!
6+ //! ## Usage
7+ //!
8+ //! To publish or subscribe to Protobuf messages, wrap your message type in `ProtobufMessage<T>`
9+ //! and ensure the inner type implements both `prost::Message` and `IsProtobufType`.
10+ //!
11+ //! ```rust
12+ //! use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType};
13+ //! use my_proto::MyMessage;
14+ //!
15+ //! impl IsProtobufType for MyMessage {}
16+ //!
17+ //! let msg = ProtobufMessage(MyMessage::default());
18+ //! ```
19+
120use prost:: Message ;
221use rustecal:: pubsub:: { PublisherMessage , SubscriberMessage } ;
322use rustecal:: pubsub:: types:: DataTypeInfo ;
423
5- /// Marker trait to opt-in for Protobuf support
24+ /// Marker trait to opt-in a Protobuf type for use with eCAL.
25+ ///
26+ /// This trait must be implemented for any `prost::Message` you wish to use
27+ /// with `ProtobufMessage<T>`. It provides a type-level opt-in mechanism
28+ /// to ensure users are aware of what's being exposed to eCAL.
629pub trait IsProtobufType { }
730
8- /// Wrapper around a `prost::Message` to enable use with TypedPublisher and TypedSubscriber
31+ /// Wrapper around a `prost::Message` type to enable typed publishing and subscription.
32+ ///
33+ /// This is the type that should be used with `TypedPublisher` and `TypedSubscriber`
34+ /// for Protobuf messages.
35+ ///
36+ /// ```rust
37+ /// use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType};
38+ /// use my_proto::MyMessage;
39+ ///
40+ /// impl IsProtobufType for MyMessage {}
41+ ///
42+ /// let wrapped = ProtobufMessage(MyMessage::default());
43+ /// ```
944#[ derive( Debug , Clone ) ]
1045pub struct ProtobufMessage < T > ( pub T ) ;
1146
1247impl < T > SubscriberMessage for ProtobufMessage < T >
1348where
1449 T : Message + Default + IsProtobufType ,
1550{
51+ /// Returns metadata used by eCAL to describe the Protobuf type.
52+ ///
53+ /// This includes:
54+ /// - `proto` as encoding
55+ /// - the Rust type name
56+ /// - an optional descriptor (currently empty)
1657 fn datatype ( ) -> DataTypeInfo {
1758 DataTypeInfo {
1859 encoding : "proto" . to_string ( ) ,
2162 }
2263 }
2364
65+ /// Decodes a Protobuf message from bytes.
66+ ///
67+ /// # Returns
68+ /// - `Some(ProtobufMessage<T>)` on success
69+ /// - `None` if decoding fails
2470 fn from_bytes ( bytes : & [ u8 ] ) -> Option < Self > {
2571 T :: decode ( bytes) . ok ( ) . map ( ProtobufMessage )
2672 }
@@ -30,10 +76,15 @@ impl<T> PublisherMessage for ProtobufMessage<T>
3076where
3177 T : Message + Default + IsProtobufType ,
3278{
79+ /// Returns the same datatype information as [`SubscriberMessage`] implementation.
3380 fn datatype ( ) -> DataTypeInfo {
3481 <ProtobufMessage < T > as SubscriberMessage >:: datatype ( )
3582 }
3683
84+ /// Encodes the message to a byte buffer.
85+ ///
86+ /// # Panics
87+ /// Will panic if `prost::Message::encode` fails (should never panic for valid messages).
3788 fn to_bytes ( & self ) -> Vec < u8 > {
3889 let mut buf = Vec :: with_capacity ( self . 0 . encoded_len ( ) ) ;
3990 self . 0
0 commit comments