1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Data structure for managing named parameters.

use std::any::Any;
use std::collections::HashMap;

/// A collection of named parameters.
pub struct Options {
    map: HashMap<String, Box<Any>>,
}

impl Options {
    /// Create a new collection of named parameters.
    #[inline]
    pub fn new() -> Options {
        Options { map: HashMap::new() }
    }

    /// Get the value of a parameter.
    #[inline]
    pub fn get<T: Any + Clone>(&self, name: &str) -> Option<T> {
        self.map.get(name).and_then(|ref value| value.downcast_ref::<T>())
                          .and_then(|value| Some(value.clone()))
    }

    /// Get the value of a parameter by reference.
    #[inline]
    pub fn get_ref<T: Any>(&self, name: &str) -> Option<&T> {
        self.map.get(name).and_then(|ref value| value.downcast_ref::<T>())
    }

    /// Set the value of a parameter.
    #[inline]
    pub fn set<'l, T: Any>(&'l mut self, name: &str, value: T) -> &'l mut Options {
        self.map.insert(name.to_string(), Box::new(value));
        self
    }
}

#[cfg(test)]
mod tests {
    use Options;

    #[test]
    fn get() {
        macro_rules! test(
            ($options:expr, $name:expr, $value:expr, $kind:ty) => (
                assert_eq!($options.get::<$kind>($name).unwrap(), $value)
            );
        );

        let options = setup();
        test!(options, "a", 42, i32);
        test!(options, "b", true, bool);
        test!(options, "c", "Hi, there!", &str);
        test!(options, "d", "Hello, world!".to_string(), String);
    }

    #[test]
    fn get_ref() {
        macro_rules! test(
            ($options:expr, $name:expr, $value:expr, $kind:ty) => (
                assert_eq!($options.get_ref::<$kind>($name).unwrap(), $value)
            );
        );

        let options = setup();
        test!(options, "a", &42, i32);
        test!(options, "b", &true, bool);
        test!(options, "c", &"Hi, there!", &str);
        test!(options, "d", "Hello, world!", String);
    }

    fn setup() -> Options {
        let mut options = Options::new();

        options.set("a", 42)
               .set("b", true)
               .set("c", "Hi, there!")
               .set("d", "Hello, world!".to_string());

        options
    }
}