#[cfg(feature = "rmw-zenoh")]
pub mod rmw_zenoh;
#[cfg(feature = "ros2dds")]
pub mod ros2dds;
use alloc::string::String;
use zenoh::{key_expr::KeyExpr, session::ZenohId, Result};
use crate::{
entity::{EndpointEntity, Entity, LivelinessKE, NodeEntity, TopicKE},
qos::QosProfile,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum KeyExprFormat {
#[default]
RmwZenoh,
Ros2Dds,
}
#[allow(unused_variables)]
impl KeyExprFormat {
pub fn topic_key_expr(&self, entity: &EndpointEntity) -> Result<TopicKE> {
match self {
KeyExprFormat::RmwZenoh => {
#[cfg(feature = "rmw-zenoh")]
{
rmw_zenoh::RmwZenohFormatter::topic_key_expr(entity)
}
#[cfg(not(feature = "rmw-zenoh"))]
{
Err(zenoh::Error::from("rmw-zenoh format not enabled"))
}
}
KeyExprFormat::Ros2Dds => {
#[cfg(feature = "ros2dds")]
{
ros2dds::Ros2DdsFormatter::topic_key_expr(entity)
}
#[cfg(not(feature = "ros2dds"))]
{
Err(zenoh::Error::from("ros2dds format not enabled"))
}
}
}
}
pub fn liveliness_key_expr(
&self,
entity: &EndpointEntity,
zid: &ZenohId,
) -> Result<LivelinessKE> {
match self {
KeyExprFormat::RmwZenoh => {
#[cfg(feature = "rmw-zenoh")]
{
rmw_zenoh::RmwZenohFormatter::liveliness_key_expr(entity, zid)
}
#[cfg(not(feature = "rmw-zenoh"))]
{
Err(zenoh::Error::from("rmw-zenoh format not enabled"))
}
}
KeyExprFormat::Ros2Dds => {
#[cfg(feature = "ros2dds")]
{
ros2dds::Ros2DdsFormatter::liveliness_key_expr(entity, zid)
}
#[cfg(not(feature = "ros2dds"))]
{
Err(zenoh::Error::from("ros2dds format not enabled"))
}
}
}
}
pub fn node_liveliness_key_expr(&self, entity: &NodeEntity) -> Result<LivelinessKE> {
match self {
KeyExprFormat::RmwZenoh => {
#[cfg(feature = "rmw-zenoh")]
{
rmw_zenoh::RmwZenohFormatter::node_liveliness_key_expr(entity)
}
#[cfg(not(feature = "rmw-zenoh"))]
{
Err(zenoh::Error::from("rmw-zenoh format not enabled"))
}
}
KeyExprFormat::Ros2Dds => {
#[cfg(feature = "ros2dds")]
{
ros2dds::Ros2DdsFormatter::node_liveliness_key_expr(entity)
}
#[cfg(not(feature = "ros2dds"))]
{
Err(zenoh::Error::from("ros2dds format not enabled"))
}
}
}
}
pub fn parse_liveliness(&self, ke: &KeyExpr) -> Result<Entity> {
match self {
KeyExprFormat::RmwZenoh => {
#[cfg(feature = "rmw-zenoh")]
{
rmw_zenoh::RmwZenohFormatter::parse_liveliness(ke)
}
#[cfg(not(feature = "rmw-zenoh"))]
{
Err(zenoh::Error::from("rmw-zenoh format not enabled"))
}
}
KeyExprFormat::Ros2Dds => {
#[cfg(feature = "ros2dds")]
{
ros2dds::Ros2DdsFormatter::parse_liveliness(ke)
}
#[cfg(not(feature = "ros2dds"))]
{
Err(zenoh::Error::from("ros2dds format not enabled"))
}
}
}
}
pub fn encode_qos(&self, qos: &QosProfile, keyless: bool) -> String {
match self {
KeyExprFormat::RmwZenoh => {
#[cfg(feature = "rmw-zenoh")]
{
rmw_zenoh::RmwZenohFormatter::encode_qos(qos, keyless)
}
#[cfg(not(feature = "rmw-zenoh"))]
{
String::new()
}
}
KeyExprFormat::Ros2Dds => {
#[cfg(feature = "ros2dds")]
{
ros2dds::Ros2DdsFormatter::encode_qos(qos, keyless)
}
#[cfg(not(feature = "ros2dds"))]
{
String::new()
}
}
}
}
pub fn decode_qos(&self, s: &str) -> Result<(bool, QosProfile)> {
match self {
KeyExprFormat::RmwZenoh => {
#[cfg(feature = "rmw-zenoh")]
{
rmw_zenoh::RmwZenohFormatter::decode_qos(s)
}
#[cfg(not(feature = "rmw-zenoh"))]
{
Err(zenoh::Error::from("rmw-zenoh format not enabled"))
}
}
KeyExprFormat::Ros2Dds => {
#[cfg(feature = "ros2dds")]
{
ros2dds::Ros2DdsFormatter::decode_qos(s)
}
#[cfg(not(feature = "ros2dds"))]
{
Err(zenoh::Error::from("ros2dds format not enabled"))
}
}
}
}
}
pub trait KeyExprFormatter {
const ESCAPE_CHAR: char;
const ADMIN_SPACE: &'static str;
fn topic_key_expr(entity: &EndpointEntity) -> Result<TopicKE>;
fn liveliness_key_expr(entity: &EndpointEntity, zid: &ZenohId) -> Result<LivelinessKE>;
fn node_liveliness_key_expr(entity: &NodeEntity) -> Result<LivelinessKE>;
fn parse_liveliness(ke: &KeyExpr) -> Result<Entity>;
fn mangle_name(name: &str) -> String {
name.replace('/', &Self::ESCAPE_CHAR.to_string())
}
fn demangle_name(name: &str) -> String {
name.replace(Self::ESCAPE_CHAR, "/")
}
fn encode_qos(qos: &QosProfile, keyless: bool) -> String;
fn decode_qos(s: &str) -> Result<(bool, QosProfile)>;
}