Skip to content

Commit

Permalink
Merge pull request #125 from shoebe/master
Browse files Browse the repository at this point in the history
Make SimdAabb deserializable in map-like formats like JSON, YAML
  • Loading branch information
sebcrozet authored Apr 15, 2023
2 parents b1493eb + dde3c77 commit 4e1f820
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/bounding_volume/simd_aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl serde::Serialize for SimdAabb {
.map(|e| array![|ii| e.extract(ii); SIMD_WIDTH]),
);

let mut simd_aabb = serializer.serialize_struct("simd_aabb", 2)?;
let mut simd_aabb = serializer.serialize_struct("SimdAabb", 2)?;
simd_aabb.serialize_field("mins", &mins)?;
simd_aabb.serialize_field("maxs", &maxs)?;
simd_aabb.end()
Expand All @@ -54,6 +54,13 @@ impl<'de> serde::Deserialize<'de> for SimdAabb {
{
struct Visitor {}

#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum Field {
Mins,
Maxs,
}

impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = SimdAabb;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
Expand All @@ -64,6 +71,37 @@ impl<'de> serde::Deserialize<'de> for SimdAabb {
)
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut mins: Option<Point<[Real; SIMD_WIDTH]>> = None;
let mut maxs: Option<Point<[Real; SIMD_WIDTH]>> = None;

while let Some(key) = map.next_key()? {
match key {
Field::Mins => {
if mins.is_some() {
return Err(serde::de::Error::duplicate_field("mins"));
}
mins = Some(map.next_value()?);
}
Field::Maxs => {
if maxs.is_some() {
return Err(serde::de::Error::duplicate_field("maxs"));
}
maxs = Some(map.next_value()?);
}
}
}

let mins = mins.ok_or_else(|| serde::de::Error::missing_field("mins"))?;
let maxs = maxs.ok_or_else(|| serde::de::Error::missing_field("maxs"))?;
let mins = mins.map(SimdReal::from);
let maxs = maxs.map(SimdReal::from);
Ok(SimdAabb { mins, maxs })
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
Expand All @@ -74,8 +112,8 @@ impl<'de> serde::Deserialize<'de> for SimdAabb {
let maxs: Point<[Real; SIMD_WIDTH]> = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
let mins = Point::from(mins.coords.map(|e| SimdReal::from(e)));
let maxs = Point::from(maxs.coords.map(|e| SimdReal::from(e)));
let mins = mins.map(SimdReal::from);
let maxs = maxs.map(SimdReal::from);
Ok(SimdAabb { mins, maxs })
}
}
Expand Down

0 comments on commit 4e1f820

Please sign in to comment.