Fix cargo clippy warnings

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/9/head
trivernis 2 years ago
parent 4d5b3766d3
commit 85213d09a3
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -3,10 +3,6 @@ use crate::models::version::Version;
use crate::{DataError, DataResult}; use crate::{DataError, DataResult};
use std::sync::Arc; 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 { pub struct Protocol {
version: Arc<Version>, version: Arc<Version>,
} }
@ -15,6 +11,8 @@ impl Protocol {
pub fn new(version: Arc<Version>) -> Self { pub fn new(version: Arc<Version>) -> Self {
Self { version } Self { version }
} }
/// Returns the protocol information for the current version
pub fn get_protocol(&self) -> DataResult<crate::models::protocol::Protocol> { pub fn get_protocol(&self) -> DataResult<crate::models::protocol::Protocol> {
let content = get_version_specific_file(&self.version, PROTOCOL_FILE)?; let content = get_version_specific_file(&self.version, PROTOCOL_FILE)?;
serde_json::from_str(&content).map_err(DataError::from) serde_json::from_str(&content).map_err(DataError::from)

@ -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; 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] #[test]
pub fn simple_test() { pub fn test_get_protocol() {
let versions = get_test_versions(); let versions = get_test_versions();
for x in versions { for x in versions {
if VERSIONS_TO_SKIP.contains(&x.minecraft_version.as_str()) {
continue;
}
let arc = Arc::new(x); let arc = Arc::new(x);
let protocol = Protocol::new(arc.clone()); let protocol = Protocol::new(arc.clone());
let protocol1 = protocol.get_protocol();
match protocol1 {
Ok(data) => {
} if let Err(e) = protocol.get_protocol() {
Err(error) => { panic!(
panic!("Minecraft Version {} could not be parsed into a Protocol object: {}", arc.minecraft_version, error); "Minecraft Version {} could not be parsed into a Protocol object: {}",
} arc.minecraft_version, e
);
} }
} }
} }

@ -1,12 +1,12 @@
pub mod types;
pub mod packet_mapper; pub mod packet_mapper;
pub mod types;
use std::borrow::Cow; pub use packet_mapper::{PacketMapper, PacketMapperSwitch, PacketSwitch};
use std::fmt;
use serde::{de, Deserialize, Deserializer};
use serde::de::{MapAccess, Visitor}; use serde::de::{MapAccess, Visitor};
use serde::{de, Deserialize, Deserializer};
use serde_json::Value; 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}; pub use types::{BitField, NativeType, PacketDataType, PacketDataTypes};
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
@ -29,33 +29,24 @@ pub struct PacketGrouping {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum DataTypeReference { pub enum DataTypeReference {
Simple(String), Simple(String),
Complex { Complex { name: String, properties: Value },
name: String,
properties: Value,
},
} }
impl Into<PacketDataType> for DataTypeReference { impl Into<PacketDataType> for DataTypeReference {
fn into(self) -> PacketDataType { fn into(self) -> PacketDataType {
let (name, properties) = match self { let (name, properties) = match self {
DataTypeReference::Simple(simple) => { DataTypeReference::Simple(simple) => (simple, Value::Null),
(simple, Value::Null) DataTypeReference::Complex { name, properties } => (name, properties),
}
DataTypeReference::Complex { name, properties } => {
(name, properties)
}
}; };
PacketDataType::new(name.as_str(), Cow::Borrowed(&properties)). PacketDataType::new(name.as_str(), Cow::Borrowed(&properties))
or_else(|| { .or_else(|| {
let option = NativeType::new(name.as_str(), Cow::Borrowed(&properties)); let option = NativeType::new(name.as_str(), Cow::Borrowed(&properties));
option.map(PacketDataType::Native) option.map(PacketDataType::Native)
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| PacketDataType::Other {
PacketDataType::Other {
name: Some(name.into()), name: Some(name.into()),
value: properties, value: properties,
}
}) })
} }
} }
@ -72,7 +63,6 @@ pub struct PacketTypes {
pub types: Vec<Packet>, pub types: Vec<Packet>,
} }
impl<'de> Deserialize<'de> for PacketTypes { impl<'de> Deserialize<'de> for PacketTypes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -96,31 +86,42 @@ impl<'de> Deserialize<'de> for PacketTypes {
let mut packets = Vec::new(); let mut packets = Vec::new();
let mut packet_mapper = None; let mut packet_mapper = None;
let value = map.next_value::<Value>()?; let value = map.next_value::<Value>()?;
if let Value::Object(obj) = value { if let Value::Object(obj) = value {
for (key, value) in obj.into_iter() { for (key, value) in obj.into_iter() {
if key.eq("packet") { if key.eq("packet") {
if let Value::Array(mut array) = value { if let Value::Array(mut array) = value {
let value = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; let value = array.pop().ok_or_else(|| {
let value: PacketMapperSwitch = serde_json::from_value(value).map_err(de::Error::custom)?; de::Error::missing_field("missing content")
})?;
let value: PacketMapperSwitch =
serde_json::from_value(value)
.map_err(de::Error::custom)?;
packet_mapper = Some(value); packet_mapper = Some(value);
} else { } else {
return Err(de::Error::custom("Invalid Packet Mapper")); return Err(de::Error::custom("Invalid Packet Mapper"));
} }
} else if let Value::Array( array) = value { } else if let Value::Array(array) = value {
let value1 = Value::Array(vec![Value::String(key.clone()), Value::Array(array)]); let value1 = Value::Array(vec![
Value::String(key.clone()),
Value::Array(array),
]);
let inner_type = types::build_inner_type(value1); let inner_type = types::build_inner_type(value1);
packets.push(Packet { packets.push(Packet {
name: key, name: key,
data: *inner_type, data: *inner_type,
}); });
} else { } 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 { return Ok(PacketTypes {
packet_mapper, packet_mapper,
types: packets, types: packets,

@ -1,7 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto; use std::convert::TryInto;
use std::fmt; use std::fmt;
use std::iter::Map;
use std::num::ParseIntError; use std::num::ParseIntError;
use serde::de::{SeqAccess, Visitor}; use serde::de::{SeqAccess, Visitor};
@ -45,7 +44,6 @@ pub struct PacketMapperSwitch {
pub switch: PacketSwitch, pub switch: PacketSwitch,
} }
impl<'de> Deserialize<'de> for PacketMapperSwitch { impl<'de> Deserialize<'de> for PacketMapperSwitch {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -67,16 +65,24 @@ impl<'de> Deserialize<'de> for PacketMapperSwitch {
let mut switch = None; let mut switch = None;
while let Some(value) = seq.next_element::<Value>()? { while let Some(value) = seq.next_element::<Value>()? {
if let Value::Object(mut value) = value { 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 { if let Value::Array(mut array) = value {
let value = array.pop().ok_or_else(|| de::Error::missing_field("missing content"))?; let value = array
let key = array.pop().ok_or_else(|| de::Error::missing_field("missing key"))?; .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 let Value::String(key) = key {
if key.eq("mapper") { 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); mapper = Some(value);
} else if key.eq("switch") { } 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); switch = Some(value);
} else { } else {
return Err(de::Error::custom("unknown key")); return Err(de::Error::custom("unknown key"));

Loading…
Cancel
Save