Value::to_string on the String type added "" around the value. Also added a get_name value to the Native Type.

pull/5/head
Wyatt Herkamp 2 years ago
parent d76dc0d344
commit d2e23ae118

@ -4,7 +4,7 @@ use serde_json::Value;
use std::borrow::{Cow}; use std::borrow::{Cow};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Deserialize,Debug)] #[derive(Deserialize, Debug)]
pub struct BitField { pub struct BitField {
pub name: String, pub name: String,
pub size: i64, pub size: i64,
@ -152,7 +152,7 @@ impl NativeType {
.map(|v| { .map(|v| {
if let Value::Object(mut obj) = v { if let Value::Object(mut obj) = v {
if let Some(name) = obj.remove("name") { if let Some(name) = obj.remove("name") {
let name = name.to_string(); let name = name.as_str().unwrap().to_string();
let inner_type = obj.remove("type").unwrap_or_default(); let inner_type = obj.remove("type").unwrap_or_default();
let inner_type = build_inner_type(inner_type); let inner_type = build_inner_type(inner_type);
(name, inner_type) (name, inner_type)
@ -176,7 +176,7 @@ impl NativeType {
"switch" => { "switch" => {
if let Value::Object(mut layout) = layout.into_owned() { if let Value::Object(mut layout) = layout.into_owned() {
return Some(NativeType::Switch { return Some(NativeType::Switch {
compare_to: layout.remove("compareTo").unwrap().to_string(), compare_to: layout.remove("compareTo").unwrap().as_str().unwrap_or_default().to_string(),
fields: layout fields: layout
.remove("fields") .remove("fields")
.and_then(|v| { .and_then(|v| {
@ -184,7 +184,7 @@ impl NativeType {
Some( Some(
fields fields
.into_iter() .into_iter()
.map(|(k, v)| (k, v.to_string())) .map(|(k, v)| (k, v.as_str().unwrap_or_default().to_string()))
.collect(), .collect(),
) )
} else { } else {
@ -192,7 +192,7 @@ impl NativeType {
} }
}) })
.unwrap_or_default(), .unwrap_or_default(),
default: layout.remove("default").map(|v| v.to_string()), default: layout.remove("default").map(|v| v.as_str().unwrap_or_default().to_string()),
}); });
} }
None None
@ -224,6 +224,36 @@ impl NativeType {
_ => None, _ => None,
} }
} }
pub fn get_name(&self) -> &str {
match self {
NativeType::Bool => "bool",
NativeType::U8 => "u8",
NativeType::U16 => "u16",
NativeType::U32 => "u32",
NativeType::U64 => "u64",
NativeType::I8 => "i8",
NativeType::I16 => "i16",
NativeType::I32 => "i32",
NativeType::I64 => "i64",
NativeType::F32 => "f32",
NativeType::F64 => "f64",
NativeType::Uuid => "uuid",
NativeType::Option(_) => "option",
NativeType::EntityMetadataLoop { .. } => "entityMetadataLoop",
NativeType::TopBitSetTerminatedArray(_) => "topbitsetterminatedarray",
NativeType::BitField(_) => "bitfield",
NativeType::Container(_) => "container",
NativeType::Switch { .. } => "switch",
NativeType::Array { .. } => "array",
NativeType::Void => "void",
NativeType::RestBuffer => "restbuffer",
NativeType::NBT => "nbt",
NativeType::OptionalNBT => "optionalnbt",
NativeType::VarInt => { "varint" }
NativeType::PString { .. } => { "pstring" }
NativeType::Buffer { .. } => { "buffer" }
}
}
} }
#[inline] #[inline]
@ -253,13 +283,13 @@ fn build_inner_type(value: Value) -> Box<PacketDataType> {
Box::new(value) Box::new(value)
} else { } else {
Box::new(PacketDataType::Other( Box::new(PacketDataType::Other(
key.to_string(), key.clone(),
inner_value.into_owned(), inner_value.into_owned(),
)) ))
} }
} else { } else {
Box::new(PacketDataType::Other( Box::new(PacketDataType::Other(
key.to_string(), key.as_str().unwrap_or_default().to_string(),
inner_value.into_owned(), inner_value.into_owned(),
)) ))
} }
@ -267,6 +297,7 @@ fn build_inner_type(value: Value) -> Box<PacketDataType> {
v => Box::new(PacketDataType::Other(String::new(), v)), v => Box::new(PacketDataType::Other(String::new(), v)),
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum PacketDataType { pub enum PacketDataType {
// Just a pure native type // Just a pure native type
@ -274,7 +305,12 @@ pub enum PacketDataType {
// It was marked as "native" however, this enum does not have it // It was marked as "native" however, this enum does not have it
UnknownNativeType(String), UnknownNativeType(String),
// This type is built from a native type // This type is built from a native type
Built(NativeType), Built {
// The name of the built type
name: String,
// The value of the built type
value: NativeType,
},
Other(String, Value), Other(String, Value),
} }
@ -295,20 +331,26 @@ impl PacketDataType {
return if let Some(type_) = return if let Some(type_) =
NativeType::new(&inner_type_name, Cow::Borrowed(&inner_type_values)) NativeType::new(&inner_type_name, Cow::Borrowed(&inner_type_values))
{ {
Some(PacketDataType::Built(type_)) Some(PacketDataType::Built {
name: key.to_string(),
value: type_,
})
} else { } else {
Some(PacketDataType::Other(inner_type_name, inner_type_values)) Some(PacketDataType::Other(inner_type_name, inner_type_values))
}; };
} }
None None
} }
_ => None, v => {
return Some(PacketDataType::Other(key.to_string(), v));
}
} }
} else { } else {
None None
} }
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct PacketDataTypes { pub struct PacketDataTypes {
pub types: Vec<PacketDataType>, pub types: Vec<PacketDataType>,

Loading…
Cancel
Save