use serde::{Deserialize, Serialize};
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
pub trait Message: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static {
fn type_name() -> &'static str;
fn version() -> &'static str {
"1.0"
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize)]
pub struct RobotState {
pub position: [f64; 3],
pub velocity: [f64; 3],
pub timestamp: i64,
}
impl Message for RobotState {
fn type_name() -> &'static str {
"ros3_msgs/RobotState"
}
}
impl Default for RobotState {
fn default() -> Self {
Self {
position: [0.0; 3],
velocity: [0.0; 3],
timestamp: 0,
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize)]
pub struct Point3D {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize)]
pub struct PointCloud {
pub points: Vec<Point3D>,
pub intensities: Vec<f32>,
pub timestamp: i64,
}
impl Message for PointCloud {
fn type_name() -> &'static str {
"ros3_msgs/PointCloud"
}
}
impl Default for PointCloud {
fn default() -> Self {
Self {
points: Vec::new(),
intensities: Vec::new(),
timestamp: 0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize)]
pub struct Pose {
pub position: [f64; 3],
pub orientation: [f64; 4], }
impl Message for Pose {
fn type_name() -> &'static str {
"ros3_msgs/Pose"
}
}
impl Default for Pose {
fn default() -> Self {
Self {
position: [0.0; 3],
orientation: [0.0, 0.0, 0.0, 1.0], }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_robot_state() {
let state = RobotState::default();
assert_eq!(state.position, [0.0; 3]);
assert_eq!(RobotState::type_name(), "ros3_msgs/RobotState");
}
#[test]
fn test_point_cloud() {
let cloud = PointCloud::default();
assert_eq!(cloud.points.len(), 0);
assert_eq!(PointCloud::type_name(), "ros3_msgs/PointCloud");
}
}