From efdf28a52a465f9c329d600dd0669f2b394d6d65 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Thu, 25 Apr 2019 23:30:50 +0200 Subject: [PATCH] restructure package enum --- .../src/protocol/implementation/mod.rs | 29 ++++---- .../implementation/protocol_connection.rs | 69 ++++++++++--------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/server/texture-sync-server/src/protocol/implementation/mod.rs b/server/texture-sync-server/src/protocol/implementation/mod.rs index 89cbda3..8903aee 100644 --- a/server/texture-sync-server/src/protocol/implementation/mod.rs +++ b/server/texture-sync-server/src/protocol/implementation/mod.rs @@ -49,29 +49,24 @@ where break; } - Package::JsonTrue - | Package::JsonFalse - | Package::JsonNull - | Package::JsonTexture(_) - | Package::JsonTextureArray(_) - | Package::Binary(_) => { + Package::Json(_) | Package::Binary(_) => { connection.send(&Package::Error(400, "Expected Command.".to_string()))?; break; } - Package::Json(Command::Ping {}) => { - connection.send(&Package::Json(Command::Pong {}))?; + Package::Command(Command::Ping {}) => { + connection.send(&Package::Command(Command::Pong {}))?; } - Package::Json(Command::Pong {}) => { + Package::Command(Command::Pong {}) => { // Ignore } - Package::Json(Command::Query { query }) => { + Package::Command(Command::Query { query }) => { connection.send(&Package::from(handler.query(&query[..])))?; } - Package::Json(Command::GetTexture { id, name }) => match (id, name) { + Package::Command(Command::GetTexture { id, name }) => match (id, name) { (Some(id), None) => { connection.send(&Package::from(handler.get_texture_by_id(&id)))?; } @@ -85,11 +80,11 @@ where } }, - Package::Json(Command::GetTextureData { texture_hash }) => { + Package::Command(Command::GetTextureData { texture_hash }) => { connection.send(&Package::from(handler.get_texture_file(texture_hash)))?; } - Package::Json(Command::GetTexturePreview { + Package::Command(Command::GetTexturePreview { texture_hash, desired_format, }) => { @@ -99,13 +94,13 @@ where } // TODO: use less nesting. - Package::Json(Command::ReplaceTexture { old, new }) => { + Package::Command(Command::ReplaceTexture { old, new }) => { match handler.replace_texture(old.clone(), new.clone(), None) { Ok(ReplaceTextureStatus::Ok) => { - connection.send(&Package::JsonTrue)?; + connection.send(&Package::Json(JsonValue::True))?; } Ok(ReplaceTextureStatus::NeedTextureData(hash)) => { - connection.send(&Package::Json(Command::GetTextureData { + connection.send(&Package::Command(Command::GetTextureData { texture_hash: hash, }))?; @@ -115,7 +110,7 @@ where match handler.replace_texture(old.clone(), new.clone(), Some(data)) { Ok(ReplaceTextureStatus::Ok) => { - connection.send(&Package::JsonTrue)?; + connection.send(&Package::Json(JsonValue::True))?; } Ok(ReplaceTextureStatus::NeedTextureData(hash)) => { panic!("Contract Violation: handler must not return NeedTextureData \ diff --git a/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs b/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs index f2205cd..85162f3 100644 --- a/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs +++ b/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs @@ -6,16 +6,21 @@ use serde::{Deserialize, Serialize}; #[derive(Eq, PartialEq, Clone, Debug)] pub enum Package { - JsonNull, - JsonFalse, - JsonTrue, - JsonTexture(Texture), - JsonTextureArray(Vec), - Json(Command), + Json(JsonValue), + Command(Command), Binary(Vec), Error(u16, String), } +#[derive(Eq, PartialEq, Clone, Debug)] +pub enum JsonValue { + Null, + True, + False, + Texture(Texture), + TextureArray(Vec), +} + #[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)] pub enum Command { #[serde(rename = "ping")] @@ -67,7 +72,7 @@ impl From for Package { impl From>> for Package { fn from(item: ProtocolResult>) -> Self { match item { - Ok(textures) => Package::JsonTextureArray(textures), + Ok(textures) => Package::Json(JsonValue::TextureArray(textures)), Err(err) => Package::from(err), } } @@ -76,8 +81,8 @@ impl From>> for Package { impl From>> for Package { fn from(item: ProtocolResult>) -> Self { match item { - Ok(Some(texture)) => Package::JsonTexture(texture), - Ok(None) => Package::JsonNull, + Ok(Some(texture)) => Package::Json(JsonValue::Texture(texture)), + Ok(None) => Package::Json(JsonValue::Null), Err(err) => Package::from(err), } } @@ -201,15 +206,15 @@ impl Connection { // try special packages first. match serde_json::from_slice::>(&payload[..]) { Ok(Some(true)) => { - return Ok(Package::JsonTrue); + return Ok(Package::Json(JsonValue::True)); } Ok(Some(false)) => { - return Ok(Package::JsonFalse); + return Ok(Package::Json(JsonValue::False)); } Ok(None) => { - return Ok(Package::JsonNull); + return Ok(Package::Json(JsonValue::Null)); } _ => (), // else try other } @@ -217,7 +222,7 @@ impl Connection { // try single texture match serde_json::from_slice::(&payload[..]) { Ok(texture) => { - return Ok(Package::JsonTexture(texture)); + return Ok(Package::Json(JsonValue::Texture(texture))); } _ => (), // else try other } @@ -225,7 +230,7 @@ impl Connection { // try texture vec match serde_json::from_slice::>(&payload[..]) { Ok(textures) => { - return Ok(Package::JsonTextureArray(textures)); + return Ok(Package::Json(JsonValue::TextureArray(textures))); } _ => (), // else try other } @@ -237,7 +242,7 @@ impl Connection { Error::new(ErrorKind::InvalidData, "Invalid JSON.") })?; - Ok(Package::Json(json)) + Ok(Package::Command(json)) } PACKAGE_TYPE_BIN => Ok(Package::Binary(payload)), _ => { @@ -249,14 +254,14 @@ impl Connection { pub fn send(&mut self, pkg: &Package) -> Result<()> { match pkg { - Package::JsonNull => self.send_json(&Option::::None), - Package::JsonTrue => self.send_json(&true), - Package::JsonFalse => self.send_json(&false), + Package::Json(JsonValue::Null) => self.send_json(&Option::::None), + Package::Json(JsonValue::True) => self.send_json(&true), + Package::Json(JsonValue::False) => self.send_json(&false), - Package::JsonTexture(texture) => self.send_json(texture), - Package::JsonTextureArray(textures) => self.send_json(textures), + Package::Json(JsonValue::Texture(texture)) => self.send_json(texture), + Package::Json(JsonValue::TextureArray(textures)) => self.send_json(textures), - Package::Json(cmd) => self.send_json(cmd), + Package::Command(cmd) => self.send_json(cmd), Package::Binary(bin) => self.send_binary(bin), Package::Error(code, msg) => self.send_error(*code, msg), @@ -412,7 +417,7 @@ mod test { assert_eq!( c.receive().unwrap(), - Package::Json(Command::Query { + Package::Command(Command::Query { query: vec!["Hallo".to_string(), "Welt!".to_string()], }) ); @@ -436,11 +441,11 @@ mod test { let mut c = Connection::new(&read_data[..], Vec::new()); - assert_eq!(c.receive().unwrap(), Package::JsonNull); + assert_eq!(c.receive().unwrap(), Package::Json(JsonValue::Null)); - assert_eq!(c.receive().unwrap(), Package::JsonTrue); + assert_eq!(c.receive().unwrap(), Package::Json(JsonValue::True)); - assert_eq!(c.receive().unwrap(), Package::JsonFalse); + assert_eq!(c.receive().unwrap(), Package::Json(JsonValue::False)); } #[test] @@ -462,17 +467,17 @@ mod test { #[test] fn writes() { - test_read_back(&Package::JsonNull); - test_read_back(&Package::JsonFalse); - test_read_back(&Package::JsonTrue); + test_read_back(&Package::Json(JsonValue::Null)); + test_read_back(&Package::Json(JsonValue::True)); + test_read_back(&Package::Json(JsonValue::False)); - test_read_back(&Package::JsonTexture(demo_texture())); - test_read_back(&Package::JsonTextureArray(vec![ + test_read_back(&Package::Json(JsonValue::Texture(demo_texture()))); + test_read_back(&Package::Json(JsonValue::TextureArray(vec![ demo_texture(), demo_texture(), - ])); + ]))); - test_read_back(&Package::Json(Command::Pong {})); + test_read_back(&Package::Command(Command::Pong {})); test_read_back(&Package::Error(500, "I bims 1 Error".to_string()));