rustorb-dds-ros2 0.4.0

The ROS 2 conventions on DDS for rustorb-dds: names, interface types from .msg/.srv, RIHS01 type hashes, node discovery
Documentation
//! The QoS profiles of ROS 2 by name, as DDS QoS.
//!
//! The values are those the ROS 2 documentation ("Quality of Service
//! settings") gives the profiles of `rmw`: reliability, durability and
//! history depth. Deadline, lifespan and liveliness are left at the DDS
//! defaults (infinite, automatic), as the profiles leave them.

use rustorb_dds::{DataReaderQos, DataWriterQos, DurabilityKind, History, Reliability};

use crate::{Error, Result};

/// A ROS 2 QoS profile: what ROS 2 sets of the DDS QoS.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QosProfile {
    /// RELIABLE or BEST_EFFORT.
    pub reliable: bool,
    /// TRANSIENT_LOCAL or VOLATILE.
    pub transient_local: bool,
    /// KEEP_LAST(depth), or KEEP_ALL for `None`.
    pub depth: Option<usize>,
}

impl QosProfile {
    /// `default` (`rmw_qos_profile_default`): RELIABLE, VOLATILE,
    /// KEEP_LAST(10).
    pub const DEFAULT: QosProfile = QosProfile {
        reliable: true,
        transient_local: false,
        depth: Some(10),
    };
    /// `sensor_data`: BEST_EFFORT, VOLATILE, KEEP_LAST(5).
    pub const SENSOR_DATA: QosProfile = QosProfile {
        reliable: false,
        transient_local: false,
        depth: Some(5),
    };
    /// `services_default`: RELIABLE, VOLATILE, KEEP_LAST(10).
    pub const SERVICES_DEFAULT: QosProfile = QosProfile::DEFAULT;
    /// `parameters`: RELIABLE, VOLATILE, KEEP_LAST(1000).
    pub const PARAMETERS: QosProfile = QosProfile {
        reliable: true,
        transient_local: false,
        depth: Some(1000),
    };
    /// `parameter_events`: RELIABLE, VOLATILE, KEEP_LAST(1000).
    pub const PARAMETER_EVENTS: QosProfile = QosProfile::PARAMETERS;
    /// `system_default`: the middleware's defaults; here those of DDS for
    /// a writer (RELIABLE, VOLATILE, KEEP_LAST(1)).
    pub const SYSTEM_DEFAULT: QosProfile = QosProfile {
        reliable: true,
        transient_local: false,
        depth: Some(1),
    };
    /// `rosout`: RELIABLE, TRANSIENT_LOCAL, KEEP_LAST(1000) (ROS 2 also
    /// sets a lifespan of 10 s, which is not set here).
    pub const ROSOUT: QosProfile = QosProfile {
        reliable: true,
        transient_local: true,
        depth: Some(1000),
    };
    /// `clock`: BEST_EFFORT, VOLATILE, KEEP_LAST(1).
    pub const CLOCK: QosProfile = QosProfile {
        reliable: false,
        transient_local: false,
        depth: Some(1),
    };
    /// What `ros2 topic pub --qos-profile ... --qos-durability transient_local`
    /// and latched topics (`/tf_static`, `/map`) use: RELIABLE,
    /// TRANSIENT_LOCAL, KEEP_LAST(1).
    pub const TRANSIENT_LOCAL: QosProfile = QosProfile {
        reliable: true,
        transient_local: true,
        depth: Some(1),
    };

    /// The profile of a name: `default`, `sensor_data`, `services_default`,
    /// `parameters`, `parameter_events`, `system_default`, `rosout`,
    /// `clock`, or `transient_local`.
    pub fn from_name(name: &str) -> Result<QosProfile> {
        Ok(match name {
            "default" => QosProfile::DEFAULT,
            "sensor_data" => QosProfile::SENSOR_DATA,
            "services_default" => QosProfile::SERVICES_DEFAULT,
            "parameters" => QosProfile::PARAMETERS,
            "parameter_events" => QosProfile::PARAMETER_EVENTS,
            "system_default" => QosProfile::SYSTEM_DEFAULT,
            "rosout" => QosProfile::ROSOUT,
            "clock" => QosProfile::CLOCK,
            "transient_local" => QosProfile::TRANSIENT_LOCAL,
            _ => {
                return Err(Error::Name(format!(
                    "QoS profile {name:?}: use default, sensor_data, services_default, \
                     parameters, parameter_events, system_default, rosout, clock or \
                     transient_local"
                )));
            }
        })
    }

    /// This profile with KEEP_LAST(`depth`).
    pub fn with_depth(mut self, depth: usize) -> QosProfile {
        self.depth = Some(depth);
        self
    }

    fn parts(&self) -> (Reliability, DurabilityKind, History) {
        (
            if self.reliable {
                Reliability::reliable()
            } else {
                Reliability::best_effort()
            },
            if self.transient_local {
                DurabilityKind::TransientLocal
            } else {
                DurabilityKind::Volatile
            },
            match self.depth {
                Some(d) => History::KeepLast(d),
                None => History::KeepAll,
            },
        )
    }

    /// The QoS of a writer, with USER_DATA `user_data`.
    pub fn writer_qos(&self, user_data: Vec<u8>) -> DataWriterQos {
        let (reliability, durability, history) = self.parts();
        DataWriterQos {
            reliability,
            durability,
            history,
            user_data,
            ..DataWriterQos::default()
        }
    }

    /// The QoS of a reader, with USER_DATA `user_data`.
    pub fn reader_qos(&self, user_data: Vec<u8>) -> DataReaderQos {
        let (reliability, durability, history) = self.parts();
        DataReaderQos {
            reliability,
            durability,
            history,
            user_data,
            ..DataReaderQos::default()
        }
    }
}

impl Default for QosProfile {
    fn default() -> Self {
        QosProfile::DEFAULT
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn names() {
        assert_eq!(
            QosProfile::from_name("default").unwrap(),
            QosProfile::DEFAULT
        );
        let s = QosProfile::from_name("sensor_data").unwrap();
        assert!(!s.reliable);
        assert_eq!(s.reader_qos(Vec::new()).history, History::KeepLast(5));
        let t = QosProfile::from_name("transient_local").unwrap();
        assert_eq!(
            t.writer_qos(b"x".to_vec()).durability,
            DurabilityKind::TransientLocal
        );
        assert!(QosProfile::from_name("best_available").is_err());
        assert_eq!(QosProfile::DEFAULT.with_depth(3).depth, Some(3));
    }
}