Skip to content

Commit

Permalink
Merge pull request #149 from fernride/flexible_public_apis
Browse files Browse the repository at this point in the history
Flexible public codegen APIs
  • Loading branch information
Carter12s authored Feb 16, 2024
2 parents 599332b + edc21df commit 90ff4ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_service` method in `rosbridge/client.rs` now accepts closures
- The `advertise_service` method in `rosbridge/client.rs` now accepts closures
- Expose additional methods useful for custom cases not using package manifests or standard ROS2 setups

### Fixed

Expand Down
33 changes: 32 additions & 1 deletion roslibrust_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,20 @@ pub fn find_and_generate_ros_messages_without_ros_package_path(
search_paths: Vec<PathBuf>,
) -> Result<(TokenStream, Vec<PathBuf>), 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<ParsedMessageFile>,
services: Vec<ParsedServiceFile>,
actions: Vec<ParsedActionFile>,
) -> Result<(TokenStream, Vec<PathBuf>), 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());
Expand All @@ -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<Package>,
) -> Result<(TokenStream, Vec<PathBuf>), 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::<Vec<_>>()
})
})
.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
Expand Down
2 changes: 1 addition & 1 deletion roslibrust_codegen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn crawl<P: AsRef<Path>>(search_paths: &[P]) -> Vec<Package> {
packages
}

fn packages_from_path(mut path: PathBuf, depth: u16) -> io::Result<Vec<Package>> {
pub fn packages_from_path(mut path: PathBuf, depth: u16) -> io::Result<Vec<Package>> {
let mut found_packages = vec![];

if depth == 0 {
Expand Down

0 comments on commit 90ff4ce

Please sign in to comment.