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 conventions of ROS 2 on DDS, for rustorb-dds: what a DDS program
//! needs to be seen by ROS 2 (Jazzy) as ROS 2 nodes, and to talk with
//! them with types known only at run time.
//!
//! - [`names`]: ROS 2 names (topics, services, nodes, namespaces) checked
//!   and expanded, and their DDS names: `rt/`, `rq/...Request`,
//!   `rr/...Reply`, `pkg::msg::dds_::Name_`.
//! - [`msg`]: the `.msg` and `.srv` files parsed.
//! - [`TypeLibrary`]: interface types found on a search path (and
//!   `AMENT_PREFIX_PATH`), as [`MessageType`] and [`ServiceType`]: the
//!   fields, the DDS type as a [`DynamicType`](rustorb_xtypes::dynamic::DynamicType),
//!   the description and the RIHS01 type hash of REP-2011
//!   ([`type_description`]).
//! - [`Value`]: messages as trees of named fields, to and from
//!   DynamicData, checked against their types.
//! - [`QosProfile`]: the ROS 2 QoS profiles by name.
//! - [`UserData`]: the `key=value;` entries ROS 2 puts in USER_DATA
//!   (`typehash=RIHS01_...;`).
//! - [`RosParticipant`] and [`Node`]: nodes announced on
//!   `ros_discovery_info`, with publishers and subscriptions whose
//!   USER_DATA has the type hash; and the writer of a received sample
//!   ([`RosParticipant::writer_of`]).
//!
//! ROS 2 is used as a black box: what it puts on DDS, seen with its tools
//! and in captures, and its documented conventions (REP-2011, the
//! design articles). No ROS 2 code is used or needed; the `.msg` files
//! are read where they are installed.
//!
//! ```no_run
//! use rustorb_dds_ros2::{QosProfile, RosParticipant, TypeLibrary, UserData, Value};
//!
//! let types = TypeLibrary::with_dirs(["msgs"]);
//! let string = types.message("std_msgs/msg/String")?;
//! let ros = RosParticipant::new(0)?;
//! let node = ros.create_node("/", "talker")?;
//! let publisher =
//!     node.create_publisher("chatter", &string, &QosProfile::DEFAULT, &UserData::new())?;
//! publisher.publish(&Value::Struct(vec![(
//!     "data".into(),
//!     Value::String("Hello World".into()),
//! )]))?;
//! # Ok::<(), rustorb_dds_ros2::Error>(())
//! ```

pub mod library;
pub mod msg;
pub mod names;
pub mod node;
pub mod qos;
pub mod type_description;
pub mod user_data;
pub mod value;

use std::fmt;

pub use library::{MessageType, ServiceType, TypeLibrary};
pub use node::{Node, RosParticipant, RosPublisher, RosSubscription};
pub use qos::QosProfile;
pub use type_description::TypeHash;
pub use user_data::UserData;
pub use value::Value;

/// Errors of this crate.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// A ROS 2 name that breaks the rules, with the reason.
    Name(String),
    /// A `.msg` or `.srv` file, or a value in it, that cannot be parsed.
    Parse(String),
    /// No definition of a type.
    NotFound(String),
    /// A file that could not be read.
    Io(String),
    /// A value that does not fit its type.
    Value(String),
    /// DDS failed.
    Dds(rustorb_dds::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Name(e) => write!(f, "ROS 2 name: {e}"),
            Error::Parse(e) => write!(f, "interface definition: {e}"),
            Error::NotFound(e) => write!(f, "type not found: {e}"),
            Error::Io(e) => write!(f, "{e}"),
            Error::Value(e) => write!(f, "value: {e}"),
            Error::Dds(e) => write!(f, "DDS: {e}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Dds(e) => Some(e),
            _ => None,
        }
    }
}

impl From<rustorb_dds::Error> for Error {
    fn from(e: rustorb_dds::Error) -> Error {
        Error::Dds(e)
    }
}

/// The result type of this crate.
pub type Result<T> = std::result::Result<T, Error>;