|
|
@ -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,11 +63,10 @@ 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
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct PacketTypesVisitor;
|
|
|
|
struct PacketTypesVisitor;
|
|
|
|
|
|
|
|
|
|
|
@ -88,39 +78,50 @@ impl<'de> Deserialize<'de> for PacketTypes {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
where
|
|
|
|
A: MapAccess<'de>,
|
|
|
|
A: MapAccess<'de>,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
while let Some(key) = map.next_key::<String>()? {
|
|
|
|
while let Some(key) = map.next_key::<String>()? {
|
|
|
|
if key.eq("types") {
|
|
|
|
if key.eq("types") {
|
|
|
|
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,
|
|
|
|