structmap
Procedural macro crate for converting between Rust struct
types and associative containers.
use BTreeMap;
// converting between a struct like ...
// ... and a BTreeMap like ...
let somedata_hm: = new;
This removes the need to pattern match on attributes and keys when making a conversion.
This was largely inspired by previous work done by @Ameobea, but extends on it much further to support conversion both ways, generic value types, and Rust 2018 conventions.
Usage
In your Cargo.toml
file, include the crate as so:
[]
= "0.1"
Now let's demonstrate conversion! Note that your struct
type should extend the Default
trait for type conversion to account for uninitialized attributes.
structmap supports conversion between two types of map aliases:
StringMap
- Strings for both keys and values. Conversion is supported only one-way at the moment from struct to BTreeMap, but not the other way around.GenericMap
- Generic serde-styleValue
s as values. Conversion is supported both ways, but limited.
Map to Struct
use FromMap;
use FromMap;
Struct to Map
use ;
use ToMap;
use BTreeMap;
// impl Default ...
Need a different key name when converting from a struct
to a map container? Use #[rename]
for
struct attributes!
use ToMap;
use ToMap;
Contributions
All complex types, include dynamic arrays, Option
s, Result
s and data structures are not yet
supported (which you can help implement!).
Feel free to let me know if there are any outstanding features that should be implemented!
Big thanks to the following contributors: