From cfe3e28eebb58c9fa7c8c1cee9b325125361f2e0 Mon Sep 17 00:00:00 2001 From: Egor Larionov Date: Sat, 1 Oct 2022 13:19:59 -0700 Subject: [PATCH] Cast cell types as integer when parsing xml Since some files can give non uint8 type cell types, we explicitly cast everything to the right type instead of expecting it to be given correctly. This helps parse files generated by meshio (See issue #21) --- src/xml.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) 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)?;