From c318bca8ae59fa025664f0903707d83077b7b23e Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com> Date: Sat, 1 Jul 2023 17:14:38 +0200 Subject: [PATCH] fix: deserialization error when using the toml crate (#122) Co-authored-by: Jonathan Cornaz --- Cargo.toml | 1 + src/animation/dto.rs | 45 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c6a803a..23355b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/animation/dto.rs b/src/animation/dto.rs index bc41def..312a62e 100644 --- a/src/animation/dto.rs +++ b/src/animation/dto.rs @@ -77,6 +77,18 @@ impl<'de> Deserialize<'de> for FrameDto { .map_err(|_| de::Error::invalid_value(Unexpected::Unsigned(v), &self)) } + fn visit_i64(self, v: i64) -> Result + 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(self, map: A) -> Result where A: MapAccess<'de>, @@ -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] @@ -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 @@ -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)), + ] + ); + } }