ros_message/field_info.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
use crate::{DataType, Error, MessagePath, Result, Value};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::fmt::Formatter;
use std::hash::{Hash, Hasher};
/// Represents all possible variants of a message field
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FieldCase {
/// Field of a single item.
///
/// Examples: `float32`, `geometry_msgs/Point`.
Unit,
/// Field of an arbitrary length array.
///
/// Examples: `float32[]`, `geometry_msgs/Point[]`.
Vector,
/// Field of a fixed length array.
///
/// The contained number is the array length.
///
/// Examples: `float32[64]`, `geometry_msgs/Point[10]`.
Array(usize),
/// Field describing a constant value.
///
/// The contained `String` is the unparsed value.
///
/// Example: `float32 FOO=123.4`.
Const(String),
}
#[derive(Clone, Debug)]
struct Uncompared<T> {
inner: T,
}
impl<T> Hash for Uncompared<T> {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
impl<T> PartialEq for Uncompared<T> {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl<T> Eq for Uncompared<T> {}
/// Full description of one field in a `msg` or `srv` file.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(into = "FieldInfoSerde")]
#[serde(try_from = "FieldInfoSerde")]
pub struct FieldInfo {
datatype: DataType,
name: String,
case: FieldCase,
const_value: Uncompared<Option<Value>>,
}
impl fmt::Display for FieldInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.case {
FieldCase::Unit => write!(f, "{} {}", self.datatype, self.name),
FieldCase::Vector => write!(f, "{}[] {}", self.datatype, self.name),
FieldCase::Array(l) => write!(f, "{}[{}] {}", self.datatype, l, self.name),
FieldCase::Const(val) => write!(f, "{} {}={}", self.datatype, self.name, val),
}
}
}
impl FieldInfo {
/// Create a field of the provided type, name and variant.
///
/// # Errors
///
/// An error will be returned if the data type cannot be parsed, or const data is invalid.
///
/// # Examples
///
/// ```
/// # use ros_message::{DataType, FieldInfo, FieldCase};
/// #
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let field = FieldInfo::new("int16", "foo", FieldCase::Vector)?;
///
/// assert_eq!(field.name(), "foo");
/// assert_eq!(field.datatype(), &DataType::I16);
/// assert_eq!(field.case(), &FieldCase::Vector);
/// assert_eq!(format!("{}", field), "int16[] foo");
/// # Ok(())
/// # }
/// ```
///
/// ```
/// # use ros_message::{FieldInfo, FieldCase};
/// assert!(FieldInfo::new("bad/field/type", "foo", FieldCase::Vector).is_err());
/// ```
pub fn new(datatype: &str, name: impl Into<String>, case: FieldCase) -> Result<FieldInfo> {
Self::evaluate(datatype.try_into()?, name.into(), case)
}
fn evaluate(datatype: DataType, name: String, case: FieldCase) -> Result<FieldInfo> {
let const_value = match &case {
FieldCase::Const(raw_value) => Some(
match &datatype {
DataType::Bool => Some(Value::Bool(raw_value != "0")),
DataType::I8(_) => raw_value.parse().ok().map(Value::I8),
DataType::I16 => raw_value.parse().ok().map(Value::I16),
DataType::I32 => raw_value.parse().ok().map(Value::I32),
DataType::I64 => raw_value.parse().ok().map(Value::I64),
DataType::U8(_) => raw_value.parse().ok().map(Value::U8),
DataType::U16 => raw_value.parse().ok().map(Value::U16),
DataType::U32 => raw_value.parse().ok().map(Value::U32),
DataType::U64 => raw_value.parse().ok().map(Value::U64),
DataType::F32 => raw_value.parse().ok().map(Value::F32),
DataType::F64 => raw_value.parse().ok().map(Value::F64),
DataType::String => Some(Value::String(raw_value.clone())),
DataType::Time
| DataType::Duration
| DataType::LocalMessage(_)
| DataType::GlobalMessage(_) => None,
}
.ok_or_else(|| Error::BadConstant {
name: name.clone(),
datatype: format!("{}", datatype),
value: raw_value.into(),
})?,
),
FieldCase::Unit | FieldCase::Vector | FieldCase::Array(_) => None,
};
Ok(FieldInfo {
datatype,
name,
case,
const_value: Uncompared { inner: const_value },
})
}
/// Returns the data type of the field.
pub fn datatype(&self) -> &DataType {
&self.datatype
}
/// Returns the name of the field.
pub fn name(&self) -> &str {
&self.name
}
/// Returns the case of the field.
pub fn case(&self) -> &FieldCase {
&self.case
}
/// Returns the stored value if a constant field.
pub fn const_value(&self) -> Option<&Value> {
self.const_value.inner.as_ref()
}
/// Returns true if the field contains a constant value.
///
/// # Examples
///
/// ```
/// # use ros_message::{DataType, FieldInfo, FieldCase};
/// #
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// assert!(!FieldInfo::new("int16", "foo", FieldCase::Vector)?.is_constant());
/// assert!(FieldInfo::new("int16", "foo", FieldCase::Const("12".into()))?.is_constant());
/// # Ok(())
/// # }
/// ```
pub fn is_constant(&self) -> bool {
matches!(self.case, FieldCase::Const(..))
}
/// Returns the representation of the data type when constructing the MD5 sum.
///
/// For built in types, it is the same as the message row, but with consistent whitespace.
///
/// For message types, the type is replaced with the message's MD5 sum,
/// which is passed in via the `hashes` argument.
///
/// The `package` argument should be the package that the current message is in, to resolve
/// global paths of local message dependencies.
///
/// # Errors
///
/// An error will be returned if a message we depend upon is missing.
///
/// # Examples
///
/// ```
/// # use ros_message::{FieldInfo, FieldCase};
/// # use std::convert::TryInto;
/// # use std::collections::HashMap;
/// #
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut hashes = HashMap::new();
/// hashes.insert("foo/Header".try_into()?, "wrong_header".into());
/// hashes.insert("std_msgs/Header".try_into()?, "123".into());
/// hashes.insert("geometry_msgs/Position".try_into()?, "345".into());
/// hashes.insert("foo/Position".try_into()?, "678".into());
///
/// assert_eq!(
/// FieldInfo::new("int16", "foo", FieldCase::Unit)?.md5_string("foo", &hashes)?,
/// "int16 foo",
/// );
/// assert_eq!(
/// FieldInfo::new("float64", "foo", FieldCase::Vector)?.md5_string("foo", &hashes)?,
/// "float64[] foo",
/// );
/// assert_eq!(
/// FieldInfo::new("byte", "foo", FieldCase::Array(12))?.md5_string("foo", &hashes)?,
/// "byte[12] foo",
/// );
/// assert_eq!(
/// FieldInfo::new("byte", "FOO", FieldCase::Const("12".into()))?.md5_string("foo", &hashes)?,
/// "byte FOO=12",
/// );
/// assert_eq!(
/// FieldInfo::new("Header", "foo", FieldCase::Unit)?.md5_string("foo", &hashes)?,
/// "123 foo",
/// );
/// assert_eq!(
/// FieldInfo::new("Position", "foo", FieldCase::Vector)?.md5_string("foo", &hashes)?,
/// "678 foo",
/// );
/// assert_eq!(
/// FieldInfo::new("geometry_msgs/Position", "foo", FieldCase::Array(12))?.md5_string("foo", &hashes)?,
/// "345 foo",
/// );
/// assert!(
/// FieldInfo::new("other_msgs/Position", "foo", FieldCase::Unit)?
/// .md5_string("foo", &hashes)
/// .is_err(),
/// );
/// # Ok(())
/// # }
/// ```
pub fn md5_string(
&self,
package: &str,
hashes: &HashMap<MessagePath, String>,
) -> Result<String> {
let datatype = self.datatype.md5_str(package, hashes)?;
Ok(match (self.datatype.is_builtin(), &self.case) {
(_, FieldCase::Const(v)) => format!("{} {}={}", datatype, self.name, v),
(false, _) | (_, &FieldCase::Unit) => format!("{} {}", datatype, self.name),
(true, &FieldCase::Vector) => format!("{}[] {}", datatype, self.name),
(true, &FieldCase::Array(l)) => format!("{}[{}] {}", datatype, l, self.name),
})
}
/// Returns true if this is a header field.
///
/// The header field is special, being a unit value of type `std_msgs/Header`
/// and named `header`. Also in this special case, the package can be elided,
/// even if we're not in the same package.
///
/// If any of those requirements are not met, it is not a header field.
///
/// The field is special because ROS channel publishers are allowed to populate it with
/// the node and publisher specific data.
///
/// # Examples
///
/// ```
/// # use ros_message::{FieldInfo, FieldCase};
/// #
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// assert!(FieldInfo::new("Header", "header", FieldCase::Unit)?.is_header());
/// assert!(FieldInfo::new("std_msgs/Header", "header", FieldCase::Unit)?.is_header());
/// assert!(!FieldInfo::new("Header", "header", FieldCase::Vector)?.is_header());
/// assert!(!FieldInfo::new("Header", "header", FieldCase::Array(5))?.is_header());
/// assert!(FieldInfo::new("Header", "header", FieldCase::Const("12".into())).is_err());
/// assert!(!FieldInfo::new("Header", "some_field", FieldCase::Unit)?.is_header());
/// # Ok(())
/// # }
/// ```
pub fn is_header(&self) -> bool {
if self.case != FieldCase::Unit || self.name != "header" {
return false;
}
match &self.datatype {
DataType::GlobalMessage(msg) => msg.package() == "std_msgs" && msg.name() == "Header",
_ => false,
}
}
}
#[derive(Serialize, Deserialize)]
struct FieldInfoSerde {
datatype: DataType,
name: String,
case: FieldCase,
}
impl TryFrom<FieldInfoSerde> for FieldInfo {
type Error = Error;
fn try_from(src: FieldInfoSerde) -> Result<Self> {
Self::evaluate(src.datatype, src.name, src.case)
}
}
impl From<FieldInfo> for FieldInfoSerde {
fn from(src: FieldInfo) -> Self {
Self {
datatype: src.datatype,
name: src.name,
case: src.case,
}
}
}