Skip to content

Commit 76ec0e0

Browse files
committed
Improve error handling around PointFieldDatatype
1 parent 79e6444 commit 76ec0e0

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

crates/utils/re_mcap/src/parsers/ros2msg/definitions/sensor_msgs.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,9 @@ pub struct Imu {
110110
}
111111

112112
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
113-
#[serde(from = "u8", into = "u8")]
113+
#[serde(try_from = "u8", into = "u8")]
114114
#[repr(u8)]
115115
pub enum PointFieldDatatype {
116-
/// Does not exist in original spec.
117-
/// <https://docs.ros.org/en/noetic/api/sensor_msgs/html/msg/PointField.html>
118-
Unknown = 0,
119116
Int8 = 1,
120117
UInt8 = 2,
121118
Int16 = 3,
@@ -126,9 +123,11 @@ pub enum PointFieldDatatype {
126123
Float64 = 8,
127124
}
128125

129-
impl From<u8> for PointFieldDatatype {
130-
fn from(value: u8) -> Self {
131-
match value {
126+
impl TryFrom<u8> for PointFieldDatatype {
127+
type Error = u8;
128+
129+
fn try_from(value: u8) -> Result<Self, Self::Error> {
130+
Ok(match value {
132131
1 => Self::Int8,
133132
2 => Self::UInt8,
134133
3 => Self::Int16,
@@ -137,8 +136,8 @@ impl From<u8> for PointFieldDatatype {
137136
6 => Self::UInt32,
138137
7 => Self::Float32,
139138
8 => Self::Float64,
140-
_ => Self::Unknown,
141-
}
139+
other => Err(other)?,
140+
})
142141
}
143142
}
144143

crates/utils/re_mcap/src/parsers/ros2msg/sensor_msgs/point_cloud_2.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ impl PointCloud2MessageParser {
9090

9191
fn builder_from_datatype(datatype: PointFieldDatatype) -> Box<dyn ArrayBuilder> {
9292
match datatype {
93-
PointFieldDatatype::Unknown => unreachable!(),
9493
PointFieldDatatype::Int8 => Box::new(Int8Builder::new()),
9594
PointFieldDatatype::UInt8 => Box::new(UInt8Builder::new()),
9695
PointFieldDatatype::Int16 => Box::new(Int16Builder::new()),
@@ -105,7 +104,6 @@ fn builder_from_datatype(datatype: PointFieldDatatype) -> Box<dyn ArrayBuilder>
105104
fn access(data: &[u8], datatype: PointFieldDatatype, is_big_endian: bool) -> std::io::Result<f32> {
106105
let mut rdr = Cursor::new(data);
107106
match (is_big_endian, datatype) {
108-
(_, PointFieldDatatype::Unknown) => Ok(0f32), // Not in the original spec.
109107
(_, PointFieldDatatype::UInt8) => rdr.read_u8().map(|x| x as f32),
110108
(_, PointFieldDatatype::Int8) => rdr.read_i8().map(|x| x as f32),
111109
(true, PointFieldDatatype::Int16) => rdr.read_i16::<BigEndian>().map(|x| x as f32),
@@ -126,9 +124,6 @@ fn access(data: &[u8], datatype: PointFieldDatatype, is_big_endian: bool) -> std
126124
impl From<PointFieldDatatype> for DataType {
127125
fn from(value: PointFieldDatatype) -> Self {
128126
match value {
129-
// Not part of the MCAP spec
130-
// // https://docs.ros.org/en/noetic/api/sensor_msgs/html/msg/PointField.html
131-
PointFieldDatatype::Unknown => unreachable!(),
132127
PointFieldDatatype::Int8 => Self::Int8,
133128
PointFieldDatatype::UInt8 => Self::UInt8,
134129
PointFieldDatatype::Int16 => Self::Int16,
@@ -215,7 +210,6 @@ fn add_field_value(
215210
) -> anyhow::Result<()> {
216211
let mut rdr = Cursor::new(data);
217212
match field.datatype {
218-
PointFieldDatatype::Unknown => unreachable!(),
219213
PointFieldDatatype::Int8 => {
220214
let builder = builder
221215
.as_any_mut()

0 commit comments

Comments
 (0)