diff --git a/src/xml.rs b/src/xml.rs index dd8b762..c033a71 100644 --- a/src/xml.rs +++ b/src/xml.rs @@ -1529,6 +1529,23 @@ impl Cells { } } + /// Decodes a data array for types. + /// + /// These can be specified as u8 or some other integer type. + /// This logic is encapsulated in this function. + fn get_type_codes( + buf: model::IOBuffer, + ) -> std::result::Result, ValidationError> { + use num_traits::FromPrimitive; + let type_codes = buf + .cast_into::() + .ok_or_else(|| ValidationError::InvalidDataFormat)?; + type_codes + .into_iter() + .map(|x| model::CellType::from_u8(x).ok_or_else(|| ValidationError::InvalidCellType(x))) + .collect() + } + /// Given the expected number of elements and an optional appended data, /// converts this `Topo` struct into a `mode::VertexNumbers` type. pub fn into_model_cells( @@ -1537,15 +1554,7 @@ impl Cells { appended: Option<&AppendedData>, ei: EncodingInfo, ) -> std::result::Result { - use num_traits::FromPrimitive; - - let type_codes: Option> = self.types.into_io_buffer(l, appended, ei)?.into(); - let type_codes = type_codes.ok_or_else(|| ValidationError::InvalidDataFormat)?; - let types: std::result::Result, ValidationError> = type_codes - .into_iter() - .map(|x| model::CellType::from_u8(x).ok_or_else(|| ValidationError::InvalidCellType(x))) - .collect(); - let types = types?; + let types = Self::get_type_codes(self.types.into_io_buffer(l, appended, ei)?)?; let offsets: Option> = self.offsets.into_io_buffer(l, appended, ei)?.cast_into(); let offsets = offsets.ok_or_else(|| ValidationError::InvalidDataFormat)?;