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)]
enum Data {
Type(String),
Container(Vec<Container>),
Mapper {
#[serde(rename = "type")]
mappings_type: String,
mappings: HashMap<String, String>,
},
Switch(Switch),
List(Box<List>),
Bitfield(Vec<BitField>),
}
#[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
enum Container { enum Container {
Type(String), Value {
Data(Vec<ContainerData>), name: String,
#[serde(rename = "type")]
data: Data,
},
Array {
name: Option<String>,
#[serde(rename = "type")]
data: Vec<Data>,
},
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
struct ContainerData {
name: String,
#[serde(rename = "type")]
container_type: ContainerDataType,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
enum ContainerDataType { enum Switch {
Type(String), Empty {
Mapper(String, Mappings), #[serde(rename = "compareTo")]
Switch(String, Switch), compare_to: String,
},
Value {
#[serde(rename = "compareTo")]
compare_to: String,
fields: HashMap<String, Data>,
},
List {
#[serde(rename = "compareTo")]
compare_to: String,
fields: HashMap<String, Vec<Data>>,
},
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
struct Mappings { #[serde(untagged)]
#[serde(rename = "type")] enum List {
mappings_type: String, Empty {
mappings: HashMap<String, String>, #[serde(rename = "countType")]
count_type: String,
},
Value {
#[serde(rename = "countType")]
count_type: String,
#[serde(rename = "type")]
list_type: Data,
},
Array {
#[serde(rename = "countType")]
count_type: String,
#[serde(rename = "type")]
list_type: Vec<Data>,
},
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
struct Switch { struct BitField {
#[serde(rename = "compareTo")] name: String,
compare_to: String, size: usize,
fields: HashMap<String, String>, signed: bool,
}
#[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()),
},
ContainerData {
name: "serverHost".to_string(),
container_type: ContainerDataType::Type("string".to_string()),
},
ContainerData {
name: "serverPort".to_string(),
container_type: ContainerDataType::Type("u16".to_string()),
},
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);
}
} }