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
//! Prints the RIHS01 type hash rustorb-dds-ros2 computes for interface
//! types, one line `pkg/msg/Name RIHS01_...` each, to compare with the
//! hashes ROS 2 ships (interop/ros2.sh).
//!
//! ```text
//! ros2_typehash all          every .msg and .srv of AMENT_PREFIX_PATH
//! ros2_typehash builtin      the definitions built into the crate
//! ros2_typehash TYPE...      the types named (pkg/msg/Name, pkg/srv/Name)
//! ```
//!
//! For a service, it prints the service and its request, response and
//! event. A type that fails is printed `pkg/msg/Name ERROR <reason>`, and
//! the exit status is then 1.

use std::path::Path;

use rustorb_dds_ros2::{TypeLibrary, names::InterfaceKind, names::TypeName};

const BUILTIN: &[&str] = &[
    "builtin_interfaces/msg/Time",
    "service_msgs/msg/ServiceEventInfo",
    "rmw_dds_common/msg/Gid",
    "rmw_dds_common/msg/NodeEntitiesInfo",
    "rmw_dds_common/msg/ParticipantEntitiesInfo",
];

/// The interfaces in the `share` directories of the library.
fn all(types: &TypeLibrary) -> Vec<String> {
    let mut names = Vec::new();
    for share in types.dirs() {
        let Ok(packages) = std::fs::read_dir(share) else {
            continue;
        };
        for p in packages.flatten() {
            let package = p.file_name().to_string_lossy().to_string();
            for kind in ["msg", "srv"] {
                let Ok(files) = std::fs::read_dir(p.path().join(kind)) else {
                    continue;
                };
                for f in files.flatten() {
                    let path = f.path();
                    if path.extension().and_then(|e| e.to_str()) != Some(kind) {
                        continue;
                    }
                    if let Some(stem) = Path::new(&path).file_stem() {
                        names.push(format!("{package}/{kind}/{}", stem.to_string_lossy()));
                    }
                }
            }
        }
    }
    names.sort();
    names.dedup();
    names
}

fn main() {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let (types, names) = match args.first().map(String::as_str) {
        Some("builtin") => (
            TypeLibrary::new(),
            BUILTIN.iter().map(|s| s.to_string()).collect(),
        ),
        Some("all") => {
            let t = TypeLibrary::with_dirs::<&str>([]);
            let names = all(&t);
            (t, names)
        }
        Some(_) => (TypeLibrary::with_dirs::<&str>([]), args),
        None => {
            eprintln!("usage: ros2_typehash all | builtin | TYPE...");
            std::process::exit(2);
        }
    };
    let mut failed = false;
    for name in names {
        let kind = TypeName::parse(&name).map(|t| t.kind);
        let r = match kind {
            Ok(InterfaceKind::Service) => types.service(&name).map(|s| {
                vec![
                    (s.name().to_string(), s.type_hash()),
                    (s.request().name().to_string(), s.request().type_hash()),
                    (s.response().name().to_string(), s.response().type_hash()),
                    (s.event().name().to_string(), s.event().type_hash()),
                ]
            }),
            _ => types
                .message(&name)
                .map(|m| vec![(m.name().to_string(), m.type_hash())]),
        };
        match r {
            Ok(lines) => {
                for (n, h) in lines {
                    println!("{n} {h}");
                }
            }
            Err(e) => {
                println!("{name} ERROR {e}");
                failed = true;
            }
        }
    }
    if failed {
        std::process::exit(1);
    }
}