diff --git a/Cargo.toml b/Cargo.toml index ea46fa5..5108f95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +cookie-factory = "0.3.1" +indoc = "0.3.5" +nom = "5.1.1" +yaserde = "0.3.15" +yaserde_derive = "0.3.15" +xml-rs = "0.8.2" diff --git a/src/bc/bc_xml.rs b/src/bc/bc_xml.rs new file mode 100644 index 0000000..ae6f4ee --- /dev/null +++ b/src/bc/bc_xml.rs @@ -0,0 +1,139 @@ +// YaSerde currently macro-expands names like __type__value from type_ +#![allow(non_snake_case)] + +use std::io::{Read, Write}; +// YaSerde is currently naming the traits and the derive macros identically +use yaserde_derive::{YaDeserialize, YaSerialize}; +use yaserde::{YaDeserialize, YaSerialize}; +// YaSerde currently needs this imported +#[allow(pub_use_of_private_extern_crate)] +use yaserde::log; + +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] +#[yaserde(rename="body")] +struct Body { + #[yaserde(rename="Encryption")] + encryption: Option, + #[yaserde(rename="LoginUser")] + login_user: Option, + #[yaserde(rename="LoginNet")] + login_net: Option, +} + +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] +struct Encryption { + #[yaserde(attribute)] + pub version: String, + #[yaserde(rename="type")] + pub type_: String, + pub nonce: String, +} + +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] +#[yaserde(rename_all="camelCase")] +struct LoginUser { + #[yaserde(attribute)] + pub version: String, + #[yaserde(rename="userName")] + pub user_name: String, + pub password: String, + #[yaserde(rename="userVer")] + pub user_ver: u32, +} + +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] +#[yaserde(rename_all="camelCase")] +struct LoginNet { + #[yaserde(attribute)] + pub version: String, + #[yaserde(rename="type")] + pub type_: String, + #[yaserde(rename="udpPort")] + pub udp_port: u16, +} + +#[test] +fn test_encryption_deser() { + let sample = indoc!(r#" + + + + md5 + 9E6D1FCB9E69846D + + "#); + let b: Body = yaserde::de::from_str(sample).unwrap(); + let enc = b.encryption.unwrap(); + + assert_eq!(enc.version, "1.1"); + assert_eq!(enc.nonce, "9E6D1FCB9E69846D"); + assert_eq!(enc.type_, "md5"); +} + +#[test] +fn test_login_deser() { + let sample = indoc!(r#" + + + + 9F07915E819A076E2E14169830769D6 + 8EFECD610524A98390F118D2789BE3B + 1 + + + LAN + 0 + + "#); + let b: Body = yaserde::de::from_str(sample).unwrap(); + let login_user = b.login_user.unwrap(); + let login_net = b.login_net.unwrap(); + + assert_eq!(login_user.version, "1.1"); + assert_eq!(login_user.user_name, "9F07915E819A076E2E14169830769D6"); + assert_eq!(login_user.password, "8EFECD610524A98390F118D2789BE3B"); + assert_eq!(login_user.user_ver, 1); + + assert_eq!(login_net.version, "1.1"); + assert_eq!(login_net.type_, "LAN"); + assert_eq!(login_net.udp_port, 0); +} + +#[test] +fn test_login_ser() { + let sample = indoc!(r#" + + + + 9F07915E819A076E2E14169830769D6 + 8EFECD610524A98390F118D2789BE3B + 1 + + + LAN + 0 + + "#); + + let b = Body { + login_user: Some(LoginUser { + version: "1.1".to_string(), + user_name: "9F07915E819A076E2E14169830769D6".to_string(), + password: "8EFECD610524A98390F118D2789BE3B".to_string(), + user_ver: 1, + }), + login_net: Some(LoginNet { + version: "1.1".to_string(), + type_: "LAN".to_string(), + udp_port: 0, + }), + ..Body::default() + }; + + let b2: Body = yaserde::de::from_str(sample).unwrap(); + let b3: Body = yaserde::de::from_str(&yaserde::ser::to_string(&b).unwrap()).unwrap(); + + assert_eq!(b, b2); + assert_eq!(b, b3); + assert_eq!(b2, b3); +} diff --git a/src/bc/mod.rs b/src/bc/mod.rs new file mode 100644 index 0000000..f906a24 --- /dev/null +++ b/src/bc/mod.rs @@ -0,0 +1,6 @@ +#[allow(dead_code)] // while I'm still fleshing this out + +mod model; +mod bc_xml; + +pub use model::Bc; diff --git a/src/bc/model.rs b/src/bc/model.rs new file mode 100644 index 0000000..20a2b9a --- /dev/null +++ b/src/bc/model.rs @@ -0,0 +1,11 @@ +pub enum Bc { + LegacyMsg(LegacyMsg), + ModernMsg(ModernMsg), + Unknown, +} + +pub struct ModernMsg { + +} + +pub enum LegacyMsg {} diff --git a/src/main.rs b/src/main.rs index e7a11a9..dedea24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +#[cfg(test)] #[macro_use] +extern crate indoc; + +mod bc; + fn main() { println!("Hello, world!"); }