#struct #hash-map #convert

map2struct

A library for converting string-string hashmaps to structs

1 unstable release

0.1.0 Apr 14, 2024

#2844 in Algorithms

Download history 42/week @ 2024-12-08 22/week @ 2024-12-15 2/week @ 2024-12-22 1/week @ 2024-12-29 1/week @ 2025-01-05 2/week @ 2025-01-12 3/week @ 2025-01-19 2/week @ 2025-01-26 6/week @ 2025-02-02 16/week @ 2025-02-09 26/week @ 2025-02-16 14/week @ 2025-02-23 43/week @ 2025-03-02 19/week @ 2025-03-09 8/week @ 2025-03-16

71 downloads per month

Apache-2.0

5KB

Simple Crate For Converting Hashmaps To Structs

Convert HashMap<String, String> values directly into structs, with optional type conversion per field.

Provides one main trait and derive macro, named Map2Struct.

Example

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:

  1. Check if all fields are present
  2. Check that no additional fields are present
  3. Check type conversions

Dependencies

~210–650KB
~15K SLoC