implement serde (se, de) thingy for sha256, textureformat, texture
This commit is contained in:
50
server/texture-sync-server/src/model/texture_format.rs
Normal file
50
server/texture-sync-server/src/model/texture_format.rs
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user