Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

412 impl error to param errors #418

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rclrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["Esteve Fernandez <esteve@apache.org>", "Nikolai Morin <nnmmgit@gmail
edition = "2021"
license = "Apache-2.0"
description = "A ROS 2 client library for developing robotics applications in Rust"
rust-version = "1.63"

[lib]
path = "src/lib.rs"
Expand All @@ -28,12 +27,13 @@ libloading = { version = "0.8", optional = true }

# Needed for the Message trait, among others
rosidl_runtime_rs = "0.4"
serde = { version = "1.0.210", optional = true }

[dev-dependencies]
# Needed for e.g. writing yaml files in tests
tempfile = "3.3.0"
# Needed for publisher and subscriber tests
test_msgs = {version = "*"}
test_msgs = { version = "*" }
# Needed for parameter service tests
tokio = { version = "*", features = ["rt", "time", "macros"] }

Expand All @@ -46,6 +46,7 @@ cfg-if = "1.0.0"
[features]
default = []
dyn_msg = ["ament_rs", "libloading"]
serde = ["dep:serde"]
# This feature is solely for the purpose of being able to generate documetation without a ROS installation
# The only intended usage of this feature is for docs.rs builders to work, and is not intended to be used by end users
generate_docs = ["rosidl_runtime_rs/generate_docs"]
Expand Down
20 changes: 10 additions & 10 deletions rclrs/src/node/graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
env,
ffi::{CStr, CString},
slice,
};
Expand Down Expand Up @@ -478,22 +479,21 @@ mod tests {
//
// 99 and 98 are just chosen as arbitrary valid domain ID values. There is
// otherwise nothing special about either value.
let domain_id: usize = std::env::var("ROS_DOMAIN_ID")
.ok()
.and_then(|value| value.parse().ok())
.map(|value: usize| if value != 99 { 99 } else { 98 })
.unwrap_or(99);

let context =
let domain_id = env::var("ROS_DOMAIN_ID")
.map_err(|e| format!("Failed to parse ROS_DOMAIN_ID: {}", e))
.and_then(|val| val.parse::<usize>().map_err(|e| format!("{}", e)))
.map(|id| if id != 99 { 99 } else { 98 })
.expect("Error setting domain_id");
let context: Context =
Context::new_with_options([], InitOptions::new().with_domain_id(Some(domain_id)))
.unwrap();
.unwrap_or_else(|error| panic!("Failed to create context: {}", error));

let node_name = "test_publisher_names_and_types";
let node = Node::new(&context, node_name).unwrap();
// Test that the graph has no publishers
let names_and_topics = node
.get_publisher_names_and_types_by_node(node_name, "")
.unwrap();

.unwrap_or_else(|error| panic!("Failed to get publisher names and types: {}", error));
assert_eq!(names_and_topics.len(), 0);

let num_publishers = node.count_publishers("/test").unwrap();
Expand Down
34 changes: 31 additions & 3 deletions rclrs/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::vendor::rcl_interfaces::msg::rmw::{ParameterType, ParameterValue as R
use crate::{call_string_getter_with_rcl_node, rcl_bindings::*, Node, RclrsError};
use std::{
collections::{btree_map::Entry, BTreeMap},
fmt::Debug,
fmt::{self, Debug, Display},
marker::PhantomData,
sync::{Arc, Mutex, RwLock, Weak},
};
Expand Down Expand Up @@ -643,6 +643,34 @@ pub enum DeclarationError {
/// An invalid range was provided to a parameter declaration (i.e. lower bound > higher bound).
InvalidRange,
}
impl Display for DeclarationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DeclarationError::AlreadyDeclared => write!(
f,
"Parameter was already declared and a new declaration was attempted"
),
DeclarationError::NoValueAvailable => write!(
f,
"Parameter was declared as non optional but no value was available"
),
DeclarationError::OverrideValueTypeMismatch => {
write!(f, "The override value that was provided has the wrong type")
}
DeclarationError::PriorValueTypeMismatch => write!(
f,
"The value that the parameter was already set to has the wrong type"
),
DeclarationError::InitialValueOutOfRange => {
write!(f, "The initial value that was selected is out of range")
}
DeclarationError::InvalidRange => write!(
f,
"An invalid range was provided to a parameter declaration"
),
}
}
}

impl<'a> Parameters<'a> {
/// Tries to read a parameter of the requested type.
Expand All @@ -668,9 +696,9 @@ impl<'a> Parameters<'a> {
/// Returns:
/// * `Ok(())` if setting was successful.
/// * [`Err(DeclarationError::TypeMismatch)`] if the type of the requested value is different
/// from the parameter's type.
/// from the parameter's type.
/// * [`Err(DeclarationError::OutOfRange)`] if the requested value is out of the parameter's
/// range.
/// range.
/// * [`Err(DeclarationError::ReadOnly)`] if the parameter is read only.
pub fn set<T: ParameterVariant>(
&self,
Expand Down
2 changes: 1 addition & 1 deletion rclrs/src/parameter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl From<()> for ParameterRanges {
/// Usually only one of these ranges will be applied, but all have to be stored since:
///
/// * A dynamic parameter can change its type at runtime, in which case a different range could be
/// applied.
/// applied.
/// * Introspection through service calls requires all the ranges to be reported to the user.
#[derive(Clone, Debug, Default)]
pub struct ParameterRanges {
Expand Down
30 changes: 15 additions & 15 deletions rclrs/src/subscription/message_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ use crate::rcl_bindings::*;
/// To quote the `rmw` documentation:
///
/// > The identifier uniquely identifies the publisher for the local context, but
/// it will not necessarily be the same identifier given in other contexts or processes
/// for the same publisher.
/// Therefore the identifier will uniquely identify the publisher within your application
/// but may disagree about the identifier for that publisher when compared to another
/// application.
/// Even with this limitation, when combined with the publisher sequence number it can
/// uniquely identify a message within your local context.
/// Publisher GIDs generated by the RMW implementation could collide at some point, in which
/// case it is not possible to distinguish which publisher sent the message.
/// The details of how GIDs are generated are RMW implementation dependent.
/// > it will not necessarily be the same identifier given in other contexts or processes
/// > for the same publisher.
/// > Therefore the identifier will uniquely identify the publisher within your application
/// > but may disagree about the identifier for that publisher when compared to another
/// > application.
/// > Even with this limitation, when combined with the publisher sequence number it can
/// > uniquely identify a message within your local context.
/// > Publisher GIDs generated by the RMW implementation could collide at some point, in which
/// > case it is not possible to distinguish which publisher sent the message.
/// > The details of how GIDs are generated are RMW implementation dependent.
///
/// > It is possible the the RMW implementation needs to reuse a publisher GID,
/// due to running out of unique identifiers or some other constraint, in which case
/// the RMW implementation may document what happens in that case, but that
/// behavior is not defined here.
/// However, this should be avoided, if at all possible, by the RMW implementation,
/// and should be unlikely to happen in practice.
/// > due to running out of unique identifiers or some other constraint, in which case
/// > the RMW implementation may document what happens in that case, but that
/// > behavior is not defined here.
/// > However, this should be avoided, if at all possible, by the RMW implementation,
/// > and should be unlikely to happen in practice.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PublisherGid {
/// Bytes identifying a publisher in the RMW implementation.
Expand Down
2 changes: 1 addition & 1 deletion rclrs/src/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl WaitSet {
///
/// - Passing a wait set with no wait-able items in it will return an error.
/// - The timeout must not be so large so as to overflow an `i64` with its nanosecond
/// representation, or an error will occur.
/// representation, or an error will occur.
///
/// This list is not comprehensive, since further errors may occur in the `rmw` or `rcl` layers.
///
Expand Down
Loading