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 serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Protocol { pub struct Protocol {
handshaking: ProtocolState, handshaking: ProtocolState,
status: ProtocolState, status: ProtocolState,
@ -11,128 +11,91 @@ pub struct Protocol {
game: ProtocolState, game: ProtocolState,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ProtocolState { pub struct ProtocolState {
to_client: ProtocolTypes, to_client: ProtocolData,
to_server: ProtocolTypes, to_server: ProtocolData,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
struct ProtocolTypes { struct ProtocolData {
types: HashMap<String, Vec<Container>>, types: HashMap<String, Vec<Data>>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
enum Container { enum Data {
Type(String), Type(String),
Data(Vec<ContainerData>), Container(Vec<Container>),
} Mapper {
#[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 {
#[serde(rename = "type")] #[serde(rename = "type")]
mappings_type: String, mappings_type: String,
mappings: HashMap<String, String>, mappings: HashMap<String, String>,
},
Switch(Switch),
List(Box<List>),
Bitfield(Vec<BitField>),
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
struct Switch { #[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")] #[serde(rename = "compareTo")]
compare_to: String, 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)] #[derive(Debug, Deserialize)]
mod tests { #[serde(untagged)]
use crate::data::input::*; enum List {
use std::collections::HashMap; Empty {
#[serde(rename = "countType")]
#[test] count_type: String,
fn test_to_json() {
let mut types = HashMap::new();
let data = vec![
ContainerData {
name: "protocolVersion".to_string(),
container_type: ContainerDataType::Type("varint".to_string()),
}, },
ContainerData { Value {
name: "serverHost".to_string(), #[serde(rename = "countType")]
container_type: ContainerDataType::Type("string".to_string()), count_type: String,
#[serde(rename = "type")]
list_type: Data,
}, },
ContainerData { Array {
name: "serverPort".to_string(), #[serde(rename = "countType")]
container_type: ContainerDataType::Type("u16".to_string()), count_type: String,
#[serde(rename = "type")]
list_type: Vec<Data>,
}, },
ContainerData { }
name: "nextState".to_string(),
container_type: ContainerDataType::Type("varint".to_string()), #[derive(Debug, Deserialize)]
}, struct BitField {
]; name: String,
size: usize,
types.insert( signed: bool,
"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);
}
} }