[][src]Crate guzzle

This crate defines Guzzle and its custom derive.

Example:

use guzzle::Guzzle;

#[derive(Default, Guzzle)]
struct Location {
    #[guzzle(keys = ["lng"])]
    lng: String,
    #[guzzle(keys = ["lat"])]
    lat: String,
}

let test_data = vec![
    ("lng", "51.5074° N".to_string()),
    ("lat", "0.1278° W".to_string()),
    ("some-other-key", "some-other-key".to_string()),
];

let mut location = Location::default();

let remaining_data: Vec<(&str, String)> = test_data
    .into_iter()
    .filter_map(|v| location.guzzle(v))
    .collect();

assert_eq!(location.lng, "51.5074° N".to_string());
assert_eq!(location.lat, "0.1278° W".to_string());

assert_eq!(remaining_data, [("some-other-key", "some-other-key".to_string())]);

However, the names of your keys may not match your struct names. To map those values, use the guzzle attribute macro:

use guzzle::Guzzle;

#[derive(Default, Guzzle)]
struct Location {
    #[guzzle(keys = ["longitude", "lng"])]
    lng: String,
    #[guzzle(keys = ["latitude", "lat"])]
    lat: String,
}

let test_data = vec![
    ("longitude", "51.5074° N".to_string()),
    ("lat", "0.1278° W".to_string()),
    ("some-other-key", "some-other-key".to_string()),
];

let mut location = Location::default();

let remaining_data: Vec<(&str, String)> = test_data
    .into_iter()
    .filter_map(|v| location.guzzle(v))
    .collect();

assert_eq!(location.lng, "51.5074° N".to_string());
assert_eq!(location.lat, "0.1278° W".to_string());

assert_eq!(remaining_data, [("some-other-key", "some-other-key".to_string())]);

Traits

Guzzle