binary_type_cast
A Rust crate for simplifying the process of parsing binary file data into various Rust data types using the TypeCast macro.
Under heavy development!
TODO:
- Create Tests
- Better Error Handling
Overview
The macro is designed to simplify parsing data records in a binary file via usage of a text from a description file, in the example, a desc.xml file. The following demonstrates how to define a custom enum called DataTypes and use the TypeCast macro.
Usage
Define a custom enum
In this example define DataTypes, and use the cast attribute for each variant.
This will automatically generate a DataTypesCast enum:
Note:
Any Enum that utilizes the TypeCast will simply have Cast appended to the name. This:
will generate the following at compile time:
The attribute macro #[derive(Clone, Debug, Serialize, Deserialize)] is hardcoded above the enum {}Cast at the moment. If there is significant demand, the macro can be altered to include only those defined on the parent enum.
Code Generation
Okay, but why not just define the DataTypesCast or ExampleEnumCast and skip the attribute nonsense?
Because the following are automatically generated:
If the parent enum has a large number of variants, this would be extremely tedious to type out.
Next Steps
Define your data structures and functions for parsing the binary data.
In the hashmapped_fields example, DataRecord is a struct that contains a HashMap of field names and their parsed values. RecordDescs is a struct that deserializes description information from the desc.xml whose descriptions are then used to parse the data.dat file into a DataRecord. The example prints out the parsed data in the DataTypesCast variants and then extracts and prints the values output from a match statement and output individually using try_into.