Add initial implementation of parsing Baichuan XML

This commit is contained in:
George Hilliard committed 2020-04-16 21:52:14 -05:00
1 parent eaad3c0060
commit a166f72b92
5 files changed
+167

No files matched your search

+6
View File
@@ -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"
+139
View File
@@ -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<Encryption>,
#[yaserde(rename="LoginUser")]
login_user: Option<LoginUser>,
#[yaserde(rename="LoginNet")]
login_net: Option<LoginNet>,
}
#[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#"
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<Encryption version="1.1">
<type>md5</type>
<nonce>9E6D1FCB9E69846D</nonce>
</Encryption>
</body>"#);
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#"
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<LoginUser version="1.1">
<userName>9F07915E819A076E2E14169830769D6</userName>
<password>8EFECD610524A98390F118D2789BE3B</password>
<userVer>1</userVer>
</LoginUser>
<LoginNet version="1.1">
<type>LAN</type>
<udpPort>0</udpPort>
</LoginNet>
</body>"#);
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#"
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<LoginUser version="1.1">
<userName>9F07915E819A076E2E14169830769D6</userName>
<password>8EFECD610524A98390F118D2789BE3B</password>
<userVer>1</userVer>
</LoginUser>
<LoginNet version="1.1">
<type>LAN</type>
<udpPort>0</udpPort>
</LoginNet>
</body>"#);
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);
}
+6
View File
@@ -0,0 +1,6 @@
#[allow(dead_code)] // while I'm still fleshing this out
mod model;
mod bc_xml;
pub use model::Bc;
+11
View File
@@ -0,0 +1,11 @@
pub enum Bc {
LegacyMsg(LegacyMsg),
ModernMsg(ModernMsg),
Unknown,
}
pub struct ModernMsg {
}
pub enum LegacyMsg {}
+5
View File
@@ -1,3 +1,8 @@
#[cfg(test)] #[macro_use]
extern crate indoc;
mod bc;
fn main() {
println!("Hello, world!");
}