diff --git a/protocol-generator/src/data/input.rs b/protocol-generator/src/data/input.rs index bd6b091..5918a30 100644 --- a/protocol-generator/src/data/input.rs +++ b/protocol-generator/src/data/input.rs @@ -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>, +#[derive(Debug, Deserialize)] +struct ProtocolData { + types: HashMap>, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Deserialize)] +#[serde(untagged)] +enum Data { + Type(String), + Container(Vec), + Mapper { + #[serde(rename = "type")] + mappings_type: String, + mappings: HashMap, + }, + Switch(Switch), + List(Box), + Bitfield(Vec), +} + +#[derive(Debug, Deserialize)] #[serde(untagged)] enum Container { - Type(String), - Data(Vec), + Value { + name: String, + #[serde(rename = "type")] + data: Data, + }, + Array { + name: Option, + #[serde(rename = "type")] + data: Vec, + }, } -#[derive(Debug, Serialize, Deserialize)] -struct ContainerData { - name: String, - #[serde(rename = "type")] - container_type: ContainerDataType, -} - -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Deserialize)] #[serde(untagged)] -enum ContainerDataType { - Type(String), - Mapper(String, Mappings), - Switch(String, Switch), +enum Switch { + Empty { + #[serde(rename = "compareTo")] + compare_to: String, + }, + Value { + #[serde(rename = "compareTo")] + compare_to: String, + fields: HashMap, + }, + List { + #[serde(rename = "compareTo")] + compare_to: String, + fields: HashMap>, + }, } -#[derive(Debug, Serialize, Deserialize)] -struct Mappings { - #[serde(rename = "type")] - mappings_type: String, - mappings: HashMap, +#[derive(Debug, Deserialize)] +#[serde(untagged)] +enum List { + Empty { + #[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, + }, } -#[derive(Debug, Serialize, Deserialize)] -struct Switch { - #[serde(rename = "compareTo")] - compare_to: String, - fields: HashMap, -} - -#[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); - } +#[derive(Debug, Deserialize)] +struct BitField { + name: String, + size: usize, + signed: bool, }