structmap
Procedural macro crate for converting between Rust struct
types and associative containers.
// 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:
[dependencies]
structmap = "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 ToMap;
use ToMap;
// impl Default ...
Need a different key name when converting from a struct
to a map container? Use #[rename]
for
struct attributes!
Limitations
At the current moment, Value
only supports encapsulate basic primitive types, which includes the
following:
i32
andu32
numeralsbool
sString
s and&str
s
All other types, include dynamic arrays, Option
s, Result
s and complex structures are not yet
supported (which you can help implement).
Contributions
This is still a WIP crate, and will mostly be used for personal projects, but feel free to let me know if there are any outstanding features that should be implemented!