Crates.io | map2struct |
lib.rs | map2struct |
version | 0.1.0 |
source | src |
created_at | 2024-04-14 22:00:12.4448 |
updated_at | 2024-04-14 22:00:12.4448 |
description | A library for converting string-string hashmaps to structs. |
homepage | https://github.com/jmaarleveld/map2struct |
repository | https://github.com/jmaarleveld/map2struct |
max_upload_size | |
id | 1208567 |
size | 3,812 |
Convert HashMap<String, String>
values directly into structs,
with optional type conversion per field.
Provides one main trait and derive macro,
named Map2Struct
.
use std::collections::HashMap;
use map2struct::Map2Struct;
#[derive(Map2Struct)]
struct Person {
name: String,
age: u32,
}
let mut map = HashMap::new();
map.insert("name".to_string(), "John".to_string());
map.insert("age".to_string(), "30".to_string());
let person = Person::from_map(map).expect("Parsing failed");
assert_eq!(person.name, "John");
assert_eq!(person.age, 30);
Fields are parsed using the .parse
method of String
values.
The following validations steps are performed: