diff --git a/src/api/protocol.rs b/src/api/protocol.rs index 65358c6..177873e 100644 --- a/src/api/protocol.rs +++ b/src/api/protocol.rs @@ -3,10 +3,6 @@ use crate::models::version::Version; use crate::{DataError, DataResult}; use std::sync::Arc; - -/// Will not parse versions `21w07a`, `20w14a`, and `20w13b` -/// These snapshot versions have incompatible data types for the tags packet. - pub struct Protocol { version: Arc, } @@ -15,6 +11,8 @@ impl Protocol { pub fn new(version: Arc) -> Self { Self { version } } + + /// Returns the protocol information for the current version pub fn get_protocol(&self) -> DataResult { let content = get_version_specific_file(&self.version, PROTOCOL_FILE)?; serde_json::from_str(&content).map_err(DataError::from) diff --git a/src/api/tests/protocol.rs b/src/api/tests/protocol.rs index 7eda49c..fb42e33 100644 --- a/src/api/tests/protocol.rs +++ b/src/api/tests/protocol.rs @@ -1,29 +1,21 @@ -use std::collections::HashMap; -use std::convert::TryInto; -use crate::api::protocol::Protocol; -use crate::api::tests::get_test_versions; -use crate::models::protocol::{PacketDataType}; use std::sync::Arc; -pub const VERSIONS_TO_SKIP: [&str; 3] = ["21w07a", "20w14a", "20w13b"]; +use crate::api::protocol::Protocol; +use crate::api::tests::get_test_versions; #[test] -pub fn simple_test() { +pub fn test_get_protocol() { let versions = get_test_versions(); + for x in versions { - if VERSIONS_TO_SKIP.contains(&x.minecraft_version.as_str()) { - continue; - } let arc = Arc::new(x); let protocol = Protocol::new(arc.clone()); - let protocol1 = protocol.get_protocol(); - match protocol1 { - Ok(data) => { - } - Err(error) => { - panic!("Minecraft Version {} could not be parsed into a Protocol object: {}", arc.minecraft_version, error); - } + if let Err(e) = protocol.get_protocol() { + panic!( + "Minecraft Version {} could not be parsed into a Protocol object: {}", + arc.minecraft_version, e + ); } } -} \ No newline at end of file +} diff --git a/src/models/protocol/mod.rs b/src/models/protocol/mod.rs index 8eab803..c8b95ef 100644 --- a/src/models/protocol/mod.rs +++ b/src/models/protocol/mod.rs @@ -1,12 +1,12 @@ -pub mod types; pub mod packet_mapper; +pub mod types; -use std::borrow::Cow; -use std::fmt; -use serde::{de, Deserialize, Deserializer}; +pub use packet_mapper::{PacketMapper, PacketMapperSwitch, PacketSwitch}; use serde::de::{MapAccess, Visitor}; +use serde::{de, Deserialize, Deserializer}; use serde_json::Value; -pub use packet_mapper::{PacketMapper, PacketSwitch, PacketMapperSwitch}; +use std::borrow::Cow; +use std::fmt; pub use types::{BitField, NativeType, PacketDataType, PacketDataTypes}; #[derive(Deserialize, Debug, Clone)] @@ -29,33 +29,24 @@ pub struct PacketGrouping { #[derive(Debug, Clone)] pub enum DataTypeReference { Simple(String), - Complex { - name: String, - properties: Value, - }, + Complex { name: String, properties: Value }, } impl Into for DataTypeReference { fn into(self) -> PacketDataType { let (name, properties) = match self { - DataTypeReference::Simple(simple) => { - (simple, Value::Null) - } - DataTypeReference::Complex { name, properties } => { - (name, properties) - } + DataTypeReference::Simple(simple) => (simple, Value::Null), + DataTypeReference::Complex { name, properties } => (name, properties), }; - PacketDataType::new(name.as_str(), Cow::Borrowed(&properties)). - or_else(|| { + PacketDataType::new(name.as_str(), Cow::Borrowed(&properties)) + .or_else(|| { let option = NativeType::new(name.as_str(), Cow::Borrowed(&properties)); option.map(PacketDataType::Native) }) - .unwrap_or_else(|| { - PacketDataType::Other { - name: Some(name.into()), - value: properties, - } + .unwrap_or_else(|| PacketDataType::Other { + name: Some(name.into()), + value: properties, }) } } @@ -72,11 +63,10 @@ pub struct PacketTypes { pub types: Vec, } - impl<'de> Deserialize<'de> for PacketTypes { fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { struct PacketTypesVisitor; @@ -88,39 +78,50 @@ impl<'de> Deserialize<'de> for PacketTypes { } fn visit_map(self, mut map: A) -> Result - where - A: MapAccess<'de>, + where + A: MapAccess<'de>, { while let Some(key) = map.next_key::()? { if key.eq("types") { let mut packets = Vec::new(); let mut packet_mapper = None; let value = map.next_value::()?; + if let Value::Object(obj) = value { for (key, value) in obj.into_iter() { if key.eq("packet") { if let Value::Array(mut array) = value { - let value = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; - let value: PacketMapperSwitch = serde_json::from_value(value).map_err(de::Error::custom)?; + let value = array.pop().ok_or_else(|| { + de::Error::missing_field("missing content") + })?; + let value: PacketMapperSwitch = + serde_json::from_value(value) + .map_err(de::Error::custom)?; packet_mapper = Some(value); } else { return Err(de::Error::custom("Invalid Packet Mapper")); } - } else if let Value::Array( array) = value { - let value1 = Value::Array(vec![Value::String(key.clone()), Value::Array(array)]); + } else if let Value::Array(array) = value { + let value1 = Value::Array(vec![ + Value::String(key.clone()), + Value::Array(array), + ]); let inner_type = types::build_inner_type(value1); - packets.push(Packet { - name: key, - data: *inner_type, - }); - + packets.push(Packet { + name: key, + data: *inner_type, + }); } else { - return Err(de::Error::custom(format!("Invalid Packet Expected Array {}", key))); + return Err(de::Error::custom(format!( + "Invalid Packet Expected Array {}", + key + ))); } } } - let packet_mapper = packet_mapper.ok_or_else(|| de::Error::missing_field("packet_mapper"))?; + let packet_mapper = packet_mapper + .ok_or_else(|| de::Error::missing_field("packet_mapper"))?; return Ok(PacketTypes { packet_mapper, types: packets, diff --git a/src/models/protocol/packet_mapper.rs b/src/models/protocol/packet_mapper.rs index 0649fe6..15936b4 100644 --- a/src/models/protocol/packet_mapper.rs +++ b/src/models/protocol/packet_mapper.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::convert::TryInto; use std::fmt; -use std::iter::Map; use std::num::ParseIntError; use serde::de::{SeqAccess, Visitor}; @@ -45,11 +44,10 @@ pub struct PacketMapperSwitch { pub switch: PacketSwitch, } - impl<'de> Deserialize<'de> for PacketMapperSwitch { fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { struct PacketMapperSwitchVisitor; @@ -60,23 +58,31 @@ impl<'de> Deserialize<'de> for PacketMapperSwitch { formatter.write_str("Expected a sequence") } fn visit_seq(self, mut seq: A) -> Result - where - A: SeqAccess<'de>, + where + A: SeqAccess<'de>, { let mut mapper = None; let mut switch = None; while let Some(value) = seq.next_element::()? { if let Value::Object(mut value) = value { - let value = value.remove("type").ok_or_else(|| de::Error::missing_field("type"))?; + let value = value + .remove("type") + .ok_or_else(|| de::Error::missing_field("type"))?; if let Value::Array(mut array) = value { - let value = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; - let key = array.pop().ok_or_else(|| de::Error::missing_field("missing key"))?; + let value = array + .pop() + .ok_or_else(|| de::Error::missing_field("missing content"))?; + let key = array + .pop() + .ok_or_else(|| de::Error::missing_field("missing key"))?; if let Value::String(key) = key { if key.eq("mapper") { - let value: PacketMapper = serde_json::from_value(value).map_err(de::Error::custom)?; + let value: PacketMapper = + serde_json::from_value(value).map_err(de::Error::custom)?; mapper = Some(value); } else if key.eq("switch") { - let value: PacketSwitch = serde_json::from_value(value).map_err(de::Error::custom)?; + let value: PacketSwitch = + serde_json::from_value(value).map_err(de::Error::custom)?; switch = Some(value); } else { return Err(de::Error::custom("unknown key"));