restructure package enum
This commit is contained in:
parent
68716569ea
commit
efdf28a52a
|
@ -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 \
|
||||
|
|
|
@ -6,16 +6,21 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
#[derive(Eq, PartialEq, Clone, Debug)]
|
||||
pub enum Package {
|
||||
JsonNull,
|
||||
JsonFalse,
|
||||
JsonTrue,
|
||||
JsonTexture(Texture),
|
||||
JsonTextureArray(Vec<Texture>),
|
||||
Json(Command),
|
||||
Json(JsonValue),
|
||||
Command(Command),
|
||||
Binary(Vec<u8>),
|
||||
Error(u16, String),
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug)]
|
||||
pub enum JsonValue {
|
||||
Null,
|
||||
True,
|
||||
False,
|
||||
Texture(Texture),
|
||||
TextureArray(Vec<Texture>),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
|
||||
pub enum Command {
|
||||
#[serde(rename = "ping")]
|
||||
|
@ -67,7 +72,7 @@ impl From<ProtocolError> for Package {
|
|||
impl From<ProtocolResult<Vec<Texture>>> for Package {
|
||||
fn from(item: ProtocolResult<Vec<Texture>>) -> 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<ProtocolResult<Vec<Texture>>> for Package {
|
|||
impl From<ProtocolResult<Option<Texture>>> for Package {
|
||||
fn from(item: ProtocolResult<Option<Texture>>) -> 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<R: Read + Sized, W: Write + Sized> Connection<R, W> {
|
|||
// try special packages first.
|
||||
match serde_json::from_slice::<Option<bool>>(&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<R: Read + Sized, W: Write + Sized> Connection<R, W> {
|
|||
// try single texture
|
||||
match serde_json::from_slice::<Texture>(&payload[..]) {
|
||||
Ok(texture) => {
|
||||
return Ok(Package::JsonTexture(texture));
|
||||
return Ok(Package::Json(JsonValue::Texture(texture)));
|
||||
}
|
||||
_ => (), // else try other
|
||||
}
|
||||
|
@ -225,7 +230,7 @@ impl<R: Read + Sized, W: Write + Sized> Connection<R, W> {
|
|||
// try texture vec
|
||||
match serde_json::from_slice::<Vec<Texture>>(&payload[..]) {
|
||||
Ok(textures) => {
|
||||
return Ok(Package::JsonTextureArray(textures));
|
||||
return Ok(Package::Json(JsonValue::TextureArray(textures)));
|
||||
}
|
||||
_ => (), // else try other
|
||||
}
|
||||
|
@ -237,7 +242,7 @@ impl<R: Read + Sized, W: Write + Sized> Connection<R, W> {
|
|||
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<R: Read + Sized, W: Write + Sized> Connection<R, W> {
|
|||
|
||||
pub fn send(&mut self, pkg: &Package) -> Result<()> {
|
||||
match pkg {
|
||||
Package::JsonNull => self.send_json(&Option::<bool>::None),
|
||||
Package::JsonTrue => self.send_json(&true),
|
||||
Package::JsonFalse => self.send_json(&false),
|
||||
Package::Json(JsonValue::Null) => self.send_json(&Option::<bool>::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()));
|
||||
|
||||
|
|
Loading…
Reference in New Issue