implement serde (se, de) thingy for sha256, textureformat, texture

This commit is contained in:
2019-04-23 16:28:28 +02:00
parent a6d8b0a057
commit 84efffb6c7
4 changed files with 145 additions and 7 deletions

View File

@ -0,0 +1,50 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub enum TextureFormat {
#[serde(rename = "png")]
PNG,
#[serde(rename = "jpeg")]
JPEG,
}
#[cfg(test)]
mod tests {
// Lol, I thought we would need custom code, like for Sha256, but it works out of the box :D
// Anyhow, left the Test in.
use super::*;
use serde_json;
#[test]
fn serialize() {
let format = serde_json::to_string_pretty(&TextureFormat::PNG).unwrap();
assert_eq!(&format, r#""png""#);
let format = serde_json::to_string_pretty(&TextureFormat::JPEG).unwrap();
assert_eq!(&format, r#""jpeg""#);
}
#[test]
fn deserialize() {
let format: TextureFormat = serde_json::from_str(r#""png""#).unwrap();
assert_eq!(format, TextureFormat::PNG);
let format: TextureFormat = serde_json::from_str(r#""jpeg""#).unwrap();
assert_eq!(format, TextureFormat::JPEG);
}
#[test]
fn fail_deserialize() {
assert_eq!(
serde_json::from_str::<'_, TextureFormat>(r#""notafmt""#).is_err(),
true
);
// Format must be lowercase!
assert_eq!(
serde_json::from_str::<'_, TextureFormat>(r#""PNG""#).is_err(),
true
);
}
}