Expand description

A parser for .dat battle result files generated by the game World of Tanks

Simple Example

use wot_datfile_parser::DatFileParser;

let file = std::fs::read("input_files/WOT_1_20_0_0/10277381675613523.dat").unwrap();

// You must construct the parser first as it needs to
// do some initialization to parse the datfiles.
// You can then use this same parser to parse any number of datfiles
let parser = DatFileParser::new();

// The parser generates a Battle struct
let battle = parser.parse(&file).unwrap();

assert_eq!(
    &battle.common["teamHealth"],
    &serde_json::json!({ "1": 25130, "2": 24820 })
);
assert_eq!(&battle.common["duration"], &serde_json::json!(417));

// Battle implements serde::Serialize and serde::Deserialize.
// So, you can use other data formats as well.
// Here we will convert it to json and print it:
let battle_as_json = serde_json::to_string_pretty(&battle).unwrap();

println!("{battle_as_json}");

Advanced Example

If you need to change how some serde_pickle::Value are converted to serde_json::Value, you can intercept it and provide your own implementation:

use wot_datfile_parser::{DatFileParser, Intercept};

let file = std::fs::read("input_files/WOT_1_20_0_0/10277381675613523.dat").unwrap();

let parser = DatFileParser::new();

// We use the following closure to change how a serde_pickle::Value is
// converted to serde_json::Value. We can also use it to log any errors
// in the datfile_parser(by matching the Failed variant)
let intercept_fn = |intercept, _original_value| {
    use Intercept::*;
    match intercept {
        Success(field, _) | NotPresent(field, _) |
        ManuallyParsed(field, _) |  Failed(field, _, _) => {
            if field.name == "teamHealth" {
                // Here we can inspect the original_value and provide our own impl
                // for converting the serde_pickle::Value to serde_json::Value
                // But for this example, we will just return the following:
                serde_json::Value::String("My own parser for teamHealth".into())
            } else {
                intercept.original_result()
            }
        },
    }
};

// The parser generates a Battle struct
let battle = parser.parse_intercept(&file, intercept_fn).unwrap();

assert_eq!(
   &battle.common["teamHealth"],
   &serde_json::json!("My own parser for teamHealth")
);
assert_eq!(&battle.common["duration"], &serde_json::json!(417));

Re-exports

Structs

  • A .dat file is parsed into this data structure
  • A data structure that holds information about a field found in battle results.

Enums

Type Definitions

  • Intercept Function that allows you to manipulate how a particular value is parsed.