Skip to content

Commit

Permalink
fix: deserialization error when using the toml crate (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Cornaz <[email protected]>
  • Loading branch information
tguichaoua and jcornaz authored Jul 1, 2023
1 parent 2b26891 commit c318bca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_yaml = { version = "0.9.22", default-features = false }
rstest = { version = "0.17.0", default-features = false }
bevy = { version = "0.10.1", default-features = false, features = ["bevy_asset", "bevy_winit", "bevy_render", "bevy_sprite", "bevy_core_pipeline", "png", "x11", "dynamic_linking"] }
anyhow = "1.0"
toml = "0.7.5"

[build-dependencies]
rustc_version = "0.4.0"
Expand Down
45 changes: 41 additions & 4 deletions src/animation/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ impl<'de> Deserialize<'de> for FrameDto {
.map_err(|_| de::Error::invalid_value(Unexpected::Unsigned(v), &self))
}

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
v.try_into()
.map(|index| FrameDto {
index,
duration: None,
})
.map_err(|_| de::Error::invalid_value(Unexpected::Signed(v), &self))
}

fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
Expand Down Expand Up @@ -204,9 +216,12 @@ mod tests {
)]
animation: Animation,
) {
let serialized: String = serde_yaml::to_string(&animation).unwrap();
let deserialized: Animation = serde_yaml::from_str(&serialized).unwrap();
assert_eq!(animation, deserialized);
let yaml: String = serde_yaml::to_string(&animation).unwrap();
let from_yaml: Animation = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(animation, from_yaml);
let toml: String = toml::to_string(&animation).unwrap();
let from_toml: Animation = toml::from_str(&toml).unwrap();
assert_eq!(animation, from_toml);
}

#[test]
Expand Down Expand Up @@ -423,7 +438,7 @@ mod tests {
}

#[test]
fn same_duration_for_all_frames_short_hand() {
fn same_duration_for_all_frames_short_hand_yaml() {
// given
let content = "
frame_duration: 100
Expand All @@ -443,4 +458,26 @@ mod tests {
]
);
}

#[test]
fn same_duration_for_all_frames_short_hand_toml() {
// given
let content = "
frame_duration = 100
frames = [0, 1, 2]
";

// when
let animation: Animation = toml::from_str(content).unwrap();

// then
assert_eq!(
animation.frames,
vec![
Frame::new(0, Duration::from_millis(100)),
Frame::new(1, Duration::from_millis(100)),
Frame::new(2, Duration::from_millis(100)),
]
);
}
}

0 comments on commit c318bca

Please sign in to comment.