Rework input json parser

This commit is contained in:
Vladislavs Golubs 2021-02-06 23:18:39 +03:00
parent 3229d02418
commit ab4d40e482

View File

@ -2,7 +2,7 @@ use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct Protocol {
handshaking: ProtocolState,
status: ProtocolState,
@ -11,128 +11,91 @@ pub struct Protocol {
game: ProtocolState,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolState {
to_client: ProtocolTypes,
to_server: ProtocolTypes,
to_client: ProtocolData,
to_server: ProtocolData,
}
#[derive(Debug, Serialize, Deserialize)]
struct ProtocolTypes {
types: HashMap<String, Vec<Container>>,
#[derive(Debug, Deserialize)]
struct ProtocolData {
types: HashMap<String, Vec<Data>>,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Container {
enum Data {
Type(String),
Data(Vec<ContainerData>),
}
#[derive(Debug, Serialize, Deserialize)]
struct ContainerData {
name: String,
#[serde(rename = "type")]
container_type: ContainerDataType,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum ContainerDataType {
Type(String),
Mapper(String, Mappings),
Switch(String, Switch),
}
#[derive(Debug, Serialize, Deserialize)]
struct Mappings {
Container(Vec<Container>),
Mapper {
#[serde(rename = "type")]
mappings_type: String,
mappings: HashMap<String, String>,
},
Switch(Switch),
List(Box<List>),
Bitfield(Vec<BitField>),
}
#[derive(Debug, Serialize, Deserialize)]
struct Switch {
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Container {
Value {
name: String,
#[serde(rename = "type")]
data: Data,
},
Array {
name: Option<String>,
#[serde(rename = "type")]
data: Vec<Data>,
},
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Switch {
Empty {
#[serde(rename = "compareTo")]
compare_to: String,
fields: HashMap<String, String>,
},
Value {
#[serde(rename = "compareTo")]
compare_to: String,
fields: HashMap<String, Data>,
},
List {
#[serde(rename = "compareTo")]
compare_to: String,
fields: HashMap<String, Vec<Data>>,
},
}
#[cfg(test)]
mod tests {
use crate::data::input::*;
use std::collections::HashMap;
#[test]
fn test_to_json() {
let mut types = HashMap::new();
let data = vec![
ContainerData {
name: "protocolVersion".to_string(),
container_type: ContainerDataType::Type("varint".to_string()),
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum List {
Empty {
#[serde(rename = "countType")]
count_type: String,
},
ContainerData {
name: "serverHost".to_string(),
container_type: ContainerDataType::Type("string".to_string()),
Value {
#[serde(rename = "countType")]
count_type: String,
#[serde(rename = "type")]
list_type: Data,
},
ContainerData {
name: "serverPort".to_string(),
container_type: ContainerDataType::Type("u16".to_string()),
Array {
#[serde(rename = "countType")]
count_type: String,
#[serde(rename = "type")]
list_type: Vec<Data>,
},
ContainerData {
name: "nextState".to_string(),
container_type: ContainerDataType::Type("varint".to_string()),
},
];
types.insert(
"packet_set_protocol".to_owned(),
vec![
Container::Type("container".to_owned()),
Container::Data(data),
],
);
let mut mappings = HashMap::new();
mappings.insert("0x00".to_owned(), "set_protocol".to_owned());
mappings.insert("0xfe".to_owned(), "legacy_server_list_ping".to_owned());
let data2 = vec![ContainerData {
name: "name".to_string(),
container_type: ContainerDataType::Mapper(
"mapper".to_owned(),
Mappings {
mappings_type: "varint".to_string(),
mappings,
},
),
}];
// _type: ContainerDataType::Mapper(vec![
// Mapper::Type("mapper".to_owned()),
// Mapper::Data("varint".to_owned(), mappings),
// ]),
types.insert(
"packet".to_owned(),
vec![
Container::Type("container".to_owned()),
Container::Data(data2),
],
);
// let protocol = Protocol {
// handshaking: ProtocolState {
// to_server: ProtocolTypes { types },
// },
// };
//
// let j = serde_json::to_string(&protocol).unwrap();
//
// println!("{}", j);
//
// assert_eq!("", j);
}
}
#[derive(Debug, Deserialize)]
struct BitField {
name: String,
size: usize,
signed: bool,
}