From 378aca7b5997037c2ac1f0aa303bdbc4c63078a0 Mon Sep 17 00:00:00 2001 From: John Hughes Date: Tue, 30 Jan 2024 17:45:26 +0100 Subject: [PATCH 1/2] Flexible public codegen APIs --- roslibrust_codegen/src/lib.rs | 33 ++++++++++++++++++++++++++++++++- roslibrust_codegen/src/utils.rs | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/roslibrust_codegen/src/lib.rs b/roslibrust_codegen/src/lib.rs index c059e197..40f52beb 100644 --- a/roslibrust_codegen/src/lib.rs +++ b/roslibrust_codegen/src/lib.rs @@ -362,12 +362,20 @@ pub fn find_and_generate_ros_messages_without_ros_package_path( search_paths: Vec, ) -> Result<(TokenStream, Vec), Error> { let (messages, services, actions) = find_and_parse_ros_messages(&search_paths)?; - if messages.is_empty() && services.is_empty() { // I'm considering this an error for now, but I could see this one being debateable // As it stands there is not good way for us to manually produce a warning, so I'd rather fail loud bail!("Failed to find any services or messages while generating ROS message definitions, paths searched: {search_paths:?}"); } + tokenize_messages_and_services(messages, services, actions) +} + +/// Generates source code and list of depnendent file system paths +fn tokenize_messages_and_services( + messages: Vec, + services: Vec, + actions: Vec, +) -> Result<(TokenStream, Vec), Error> { let (messages, services) = resolve_dependency_graph(messages, services)?; let msg_iter = messages.iter().map(|m| m.parsed.path.clone()); let srv_iter = services.iter().map(|s| s.parsed.path.clone()); @@ -377,6 +385,29 @@ pub fn find_and_generate_ros_messages_without_ros_package_path( Ok((source, dependent_paths)) } +/// Generates struct definitions and implementations for message and service files +/// in the given packages. +pub fn generate_ros_messages_for_packages( + packages: Vec, +) -> Result<(TokenStream, Vec), Error> { + let msg_paths = packages + .iter() + .flat_map(|package| { + utils::get_message_files(&package).map(|msgs| { + msgs.into_iter() + .map(|msg| (package.clone(), msg)) + .collect::>() + }) + }) + .flatten() + .collect(); + let (messages, services, actions) = parse_ros_files(msg_paths)?; + if messages.is_empty() && services.is_empty() { + bail!("Failed to find any services or messages while generating ROS message definitions, packages searched: {packages:?}") + } + tokenize_messages_and_services(messages, services, actions) +} + /// Searches a list of paths for ROS packages to find their associated message /// and service files, parsing and performing dependency resolution on those /// it finds. Returns a map of PACKAGE_NAME/MESSAGE_NAME strings to message file diff --git a/roslibrust_codegen/src/utils.rs b/roslibrust_codegen/src/utils.rs index 867d36ae..ff45e30c 100644 --- a/roslibrust_codegen/src/utils.rs +++ b/roslibrust_codegen/src/utils.rs @@ -63,7 +63,7 @@ pub fn crawl>(search_paths: &[P]) -> Vec { packages } -fn packages_from_path(mut path: PathBuf, depth: u16) -> io::Result> { +pub fn packages_from_path(mut path: PathBuf, depth: u16) -> io::Result> { let mut found_packages = vec![]; if depth == 0 { From eafdd5257b085baa41a019cfa8a342843a5871a3 Mon Sep 17 00:00:00 2001 From: John Hughes Date: Thu, 15 Feb 2024 14:12:08 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d291403d..e0970c0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - The build.rs example in example_package now correctly informs cargo of filesystem dependencies -- The `advertise_serveice` method in `rosbridge/client.rs` now accepts closures +- The `advertise_serveice` method in `rosbridge/client.rs` now accepts closures +- Expose additional methods useful for custom cases not using package manifests or standard ROS2 setups ### Fixed