Module figment::util::vec_tuple_map

source ·
Expand description

A helper to serialize and deserialize a map as a vector of (key, value) pairs.

use figment::{Figment, util::map};
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    #[serde(with = "figment::util::vec_tuple_map")]
    pairs: Vec<(String, usize)>
}

let map = map!["key" => 1, "value" => 100, "name" => 20];
let c: Config = Figment::from(("pairs", map)).extract().unwrap();
assert_eq!(c.pairs.len(), 3);

let mut pairs = c.pairs;
pairs.sort_by_key(|(_, v)| *v);

assert_eq!(pairs[0], ("key".into(), 1));
assert_eq!(pairs[1], ("name".into(), 20));
assert_eq!(pairs[2], ("value".into(), 100));

Functions