add way to send Texture and Texture Arrays

This commit is contained in:
CodeSteak 2019-04-24 18:45:01 +02:00
parent aa55b5948e
commit ca4fde0b50
1 changed files with 43 additions and 1 deletions

View File

@ -7,6 +7,8 @@ pub enum Package {
JsonNull,
JsonFalse,
JsonTrue,
JsonTexture(Texture),
JsonTextureArray(Vec<Texture>),
Json(Command),
Binary(Vec<u8>),
Error(u16, String),
@ -156,7 +158,22 @@ impl<R: Read + Sized, W: Write + Sized> Connection<R, W> {
Ok(None) => {
return Ok(Package::JsonNull);
}
_ => (), // else try other
}
// try single texture
match serde_json::from_slice::<Texture>(&payload[..]) {
Ok(texture) => {
return Ok(Package::JsonTexture(texture));
}
_ => (), // else try other
}
// try texture vec
match serde_json::from_slice::<Vec<Texture>>(&payload[..]) {
Ok(textures) => {
return Ok(Package::JsonTextureArray(textures));
}
_ => (), // else try other
}
@ -182,6 +199,10 @@ impl<R: Read + Sized, W: Write + Sized> Connection<R, W> {
Package::JsonNull => self.send_json(&Option::<bool>::None),
Package::JsonTrue => self.send_json(&true),
Package::JsonFalse => self.send_json(&false),
Package::JsonTexture(texture) => self.send_json(texture),
Package::JsonTextureArray(textures) => self.send_json(textures),
Package::Json(cmd) => self.send_json(cmd),
Package::Binary(bin) => self.send_binary(bin),
@ -189,7 +210,7 @@ impl<R: Read + Sized, W: Write + Sized> Connection<R, W> {
}
}
fn send_json<J: 'static + Serialize + Sized>(&mut self, msg: &J) -> Result<()> {
fn send_json<J: 'static + Serialize>(&mut self, msg: &J) -> Result<()> {
let w = &mut self.writer;
let payload = serde_json::to_vec(msg).unwrap();
@ -234,6 +255,21 @@ mod test {
use crate::model::*;
// Utility fn
fn demo_texture() -> Texture {
const HASH_BYTES: [u8; 32] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
];
Texture {
id: "d32e1f80-a17a-4dd7-8ed7-c3a2de1de1c9".to_string(),
name: "texture.png".to_string(),
tags: vec!["Wood".to_string(), "Hair".to_string()],
format: TextureFormat::PNG,
resolution: (512, 512),
texture_hash: Sha256(HASH_BYTES),
}
}
fn test_read_back(pkg: &Package) {
let dummy_read_data = [255u8];
@ -377,6 +413,12 @@ mod test {
test_read_back(&Package::JsonFalse);
test_read_back(&Package::JsonTrue);
test_read_back(&Package::JsonTexture(demo_texture()));
test_read_back(&Package::JsonTextureArray(vec![
demo_texture(),
demo_texture(),
]));
test_read_back(&Package::Json(Command::Pong {}));
test_read_back(&Package::Error(500, "I bims 1 Error".to_string()));