Improves NativeType::Switch

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

@ -1,6 +1,6 @@
use crate::api::protocol::Protocol; use crate::api::protocol::Protocol;
use crate::api::tests::get_test_versions; use crate::api::tests::get_test_versions;
use crate::models::protocol::PacketDataType; use crate::models::protocol::{NativeType, PacketDataType};
use std::sync::Arc; use std::sync::Arc;
use crate::DataResult; use crate::DataResult;
@ -12,9 +12,7 @@ pub fn simple_test() {
let protocol = Protocol::new(arc.clone()); let protocol = Protocol::new(arc.clone());
let protocol1 = protocol.get_protocol(); let protocol1 = protocol.get_protocol();
match protocol1 { match protocol1 {
Ok(_) => { Ok(_) => {}
}
Err(error) => { Err(error) => {
println!("{:?} On Version {}", error, arc.minecraft_version); println!("{:?} On Version {}", error, arc.minecraft_version);
} }

@ -11,6 +11,13 @@ pub struct BitField {
pub signed: bool, pub signed: bool,
} }
#[derive(Debug)]
pub enum SwitchType {
Packet(String),
Type(Box<PacketDataType>),
Unknown(Value),
}
/// These data types should be available in every version. /// These data types should be available in every version.
/// However, they won't break anything if not present /// However, they won't break anything if not present
/// This can also be known as the Native Types /// This can also be known as the Native Types
@ -48,7 +55,7 @@ pub enum NativeType {
Container(Vec<(String, Box<PacketDataType>)>), Container(Vec<(String, Box<PacketDataType>)>),
Switch { Switch {
compare_to: String, compare_to: String,
fields: HashMap<String, String>, fields: HashMap<String, SwitchType>,
default: Option<String>, default: Option<String>,
}, },
Void, Void,
@ -184,7 +191,19 @@ impl NativeType {
Some( Some(
fields fields
.into_iter() .into_iter()
.map(|(k, v)| (k, v.as_str().unwrap_or_default().to_string())) .map(|(k, v)| {
if let Value::String(value) = v {
if value.starts_with("packet") {
(k, SwitchType::Packet(value))
} else {
(k, SwitchType::Type(build_inner_type(Value::String(value))))
}
} else if let Value::Array(array) = v {
(k, SwitchType::Type(build_inner_type(Value::Array(array))))
} else {
(k, SwitchType::Unknown(v))
}
})
.collect(), .collect(),
) )
} else { } else {

Loading…
Cancel
Save