From 0ae3c80dcb45d73868ed02f027853d7173e8f360 Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Mon, 15 May 2023 16:50:33 +0200 Subject: [PATCH] Extend the hack to generate constants for service and action types. --- r2r/src/msg_types.rs | 5 ++++ r2r_msg_gen/build.rs | 62 ++++++++++++++++++++++++++---------------- r2r_msg_gen/src/lib.rs | 9 ++++-- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/r2r/src/msg_types.rs b/r2r/src/msg_types.rs index dbd2557..012013c 100644 --- a/r2r/src/msg_types.rs +++ b/r2r/src/msg_types.rs @@ -779,5 +779,10 @@ mod tests { let gs = GoalStatus::default(); assert_eq!(gs.status, GoalStatus::STATUS_UNKNOWN as i8); + + use action_msgs::srv::CancelGoal; + let cgr = CancelGoal::Response::default(); + + assert_eq!(cgr.return_code, CancelGoal::Response::ERROR_NONE as i8); } } diff --git a/r2r_msg_gen/build.rs b/r2r_msg_gen/build.rs index f28445d..993bc1b 100644 --- a/r2r_msg_gen/build.rs +++ b/r2r_msg_gen/build.rs @@ -179,30 +179,19 @@ fn generate_bindings(bindgen_dir: &Path, msg_list: &[RosMsg]) { // find all lines which look suspiciosly like a constant. let mut constants: HashMap> = HashMap::new(); for msg in msg_list { - let prefix = &format!("pub const {}__{}__{}__", &msg.module, &msg.prefix, &msg.name); - let mut lines = str_bindings.lines(); - while let Some(line) = lines.next() { - if let Some(constant) = line.strip_prefix(prefix) { - if let Some((con, typ)) = constant.split_once(":") { - // These are generated automatically for arrays and strings, we don't need to expose them. - if con.ends_with("__MAX_SIZE") || con.ends_with("__MAX_STRING_SIZE") { - continue; - } - let key = format!("{}__{}__{}", msg.module, msg.prefix, msg.name); - if let Some((t, _)) = typ.split_once("=") { - constants.entry(key).or_default().push((con.to_string(), t.trim().to_string())); - } else if let Some(next_line) = lines.next() { - // type has moved down to the next line. (bindgen has a max line width) - if let Some((t, _)) = next_line.split_once("=") { - constants.entry(key).or_default().push((con.to_string(), t.trim().to_string())); - } else { - panic!("Code generation failure. Type not found in line! {}", next_line); - } - } else { - panic!("Code generation failure. Type not found in line! {}", line); - } - } + if msg.prefix == "srv" { + for s in &["Request", "Response"] { + let key = format!("{}__{}__{}_{}", &msg.module, &msg.prefix, &msg.name, s); + add_constants(&key, &str_bindings, &mut constants); } + } else if msg.prefix == "action" { + for s in &["Goal", "Result", "Feedback", "FeedbackMessage"] { + let key = format!("{}__{}__{}_{}", &msg.module, &msg.prefix, &msg.name, s); + add_constants(&key, &str_bindings, &mut constants); + } + } else { + let key = format!("{}__{}__{}", &msg.module, &msg.prefix, &msg.name); + add_constants(&key, &str_bindings, &mut constants); } } // generate a constant which holds all constants. @@ -228,6 +217,33 @@ fn generate_bindings(bindgen_dir: &Path, msg_list: &[RosMsg]) { .expect("Couldn't write bindings!"); } +fn add_constants(key: &str, bindings: &str, constants: &mut HashMap>) { + let mut lines = bindings.lines(); + while let Some(line) = lines.next() { + let prefix = format!("pub const {}__", key); + if let Some(constant) = line.strip_prefix(&prefix) { + if let Some((con, typ)) = constant.split_once(":") { + // These are generated automatically for arrays and strings, we don't need to expose them. + if con.ends_with("__MAX_SIZE") || con.ends_with("__MAX_STRING_SIZE") { + continue; + } + if let Some((t, _)) = typ.split_once("=") { + constants.entry(key.into()).or_default().push((con.to_string(), t.trim().to_string())); + } else if let Some(next_line) = lines.next() { + // type has moved down to the next line. (bindgen has a max line width) + if let Some((t, _)) = next_line.split_once("=") { + constants.entry(key.into()).or_default().push((con.to_string(), t.trim().to_string())); + } else { + panic!("Code generation failure. Type not found in line! {}", next_line); + } + } else { + panic!("Code generation failure. Type not found in line! {}", line); + } + } + } + } +} + fn run_dynlink(#[allow(unused_variables)] msg_list: &[RosMsg]) { #[cfg(not(feature = "doc-only"))] { diff --git a/r2r_msg_gen/src/lib.rs b/r2r_msg_gen/src/lib.rs index a4c9bd8..fa3d9dd 100644 --- a/r2r_msg_gen/src/lib.rs +++ b/r2r_msg_gen/src/lib.rs @@ -609,15 +609,18 @@ pub fn generate_rust_msg(module_: &str, prefix_: &str, name_: &str) -> String { for (c, typ) in constants { constant_strings.push(format!(" pub const {c}: {typ} = {key}__{c};")); } - let impl_constants = format!(" + let impl_constants = if constant_strings.is_empty() { + String::new() + } else { + format!(" #[allow(non_upper_case_globals)] impl {msgname} {{ {constants} }} ", msgname = name, - constants = constant_strings.join("\n") - ); + constants = constant_strings.join("\n")) + }; let module_str = format!(