uritemplate/
templatevar.rs

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::collections::HashMap;

/// TemplateVar represents the value of a template variable, which can be a
/// scalar (a simple string), a list of strings, or an associative array of
/// strings.
///
/// Normally, it is not necessary to use this class unless you are implementing
/// the `IntoTemplateVar` trait for your own classes.
#[derive(Clone)]
pub enum TemplateVar {
    /// A simple string such as `"foo"`
    Scalar(String),
    /// A list of strings such as `["foo", "bar"]`
    List(Vec<String>),
    /// An associative array of strings, such as
    /// `[("key1", "value1"), ("key2", "value2")]`
    AssociativeArray(Vec<(String, String)>),
}

/// IntoTemplateVar represents any type that can be converted into a TemplateVar
/// for use as the value of a template variable, such as a `String`,
/// `Vec<String>`, or `Vec<(String, String)>`. Default implementations are
/// available for those three types, as well as the `&str` versions.
///
/// Example
/// -------
/// Here is an example of implementing `IntoTemplateVar` for your own `Address`
/// struct. Note that you should probably implement the trait for a reference to
/// your struct, not the actual struct, unless you intend to move the value
/// efficiently.
///
/// ```ignore
/// struct Address {
///     city: String,
///     state: String
/// }
///
/// impl <'a> IntoTemplateVar for &'a Address {
///     fn into_template_var(self) -> TemplateVar {
///         TemplateVar::AssociativeArray(vec![
///             ("city".to_string(), self.city.clone()),
///             ("state".to_string(), self.state.clone())
///         ])
///     }
/// }
/// ```
///
/// Now, `Address` variables can be set as `UriTemplate` variables.
///
/// ```ignore
/// let address = Address {
///     city: "Los Angelos".to_string(),
///     state: "California".to_string()
/// };
///
/// let uri = UriTemplate::new("http://example.com/view{?address*}")
///     .set("address", &address)
///     .build();
///
/// assert_eq!(
///     uri,
///     "http://example.com/view?city=Los%20Angelos&state=California"
/// );
/// ```
pub trait IntoTemplateVar {
    fn into_template_var(self) -> TemplateVar;
}

impl IntoTemplateVar for TemplateVar {
    fn into_template_var(self) -> TemplateVar {
        self.clone()
    }
}

impl<'a> IntoTemplateVar for &'a str {
    fn into_template_var(self) -> TemplateVar {
        TemplateVar::Scalar(self.to_string())
    }
}

impl IntoTemplateVar for String {
    fn into_template_var(self) -> TemplateVar {
        TemplateVar::Scalar(self)
    }
}

impl<'a> IntoTemplateVar for &'a [String] {
    fn into_template_var(self) -> TemplateVar {
        let mut vec = Vec::new();
        for s in self {
            vec.push(s.clone());
        }
        TemplateVar::List(vec)
    }
}

impl IntoTemplateVar for Vec<String> {
    fn into_template_var(self) -> TemplateVar {
        TemplateVar::List(self)
    }
}

impl<'a, 'b> IntoTemplateVar for &'a [&'b str] {
    fn into_template_var(self) -> TemplateVar {
        let mut vec = Vec::new();
        for s in self {
            vec.push(s.to_string());
        }
        TemplateVar::List(vec)
    }
}

impl<'a> IntoTemplateVar for &'a [(String, String)] {
    fn into_template_var(self) -> TemplateVar {
        let mut vec = Vec::new();
        for s in self {
            vec.push(s.clone());
        }
        TemplateVar::AssociativeArray(vec)
    }
}

impl IntoTemplateVar for Vec<(String, String)> {
    fn into_template_var(self) -> TemplateVar {
        TemplateVar::AssociativeArray(self)
    }
}

impl<'a, 'b, 'c> IntoTemplateVar for &'a [(&'b str, &'c str)] {
    fn into_template_var(self) -> TemplateVar {
        let mut vec = Vec::new();
        for s in self {
            vec.push((s.0.to_string(), s.1.to_string()));
        }
        TemplateVar::AssociativeArray(vec)
    }
}

impl<'a> IntoTemplateVar for &'a HashMap<String, String> {
    fn into_template_var(self) -> TemplateVar {
        let mut vec = Vec::new();
        for (k, v) in self {
            vec.push((k.clone(), v.clone()));
        }
        TemplateVar::AssociativeArray(vec)
    }
}

impl<'a, 'b, 'c> IntoTemplateVar for &'a HashMap<&'b str, &'c str> {
    fn into_template_var(self) -> TemplateVar {
        let mut vec = Vec::new();
        for (k, v) in self {
            vec.push((k.to_string(), v.to_string()));
        }
        TemplateVar::AssociativeArray(vec)
    }
}

macro_rules! array_impls {
    ($($N:expr)+) => {$(
        impl <'a> IntoTemplateVar for &'a [String; $N] {
            fn into_template_var(self) -> TemplateVar {
                self[..].into_template_var()
            }
        }

        impl <'a, 'b> IntoTemplateVar for &'a[&'b str; $N] {
            fn into_template_var(self) -> TemplateVar {
                self[..].into_template_var()
            }
        }

        impl <'a> IntoTemplateVar for &'a [(String, String); $N] {
            fn into_template_var(self) -> TemplateVar {
                self[..].into_template_var()
            }
        }

        impl <'a, 'b, 'c> IntoTemplateVar for &'a[(&'b str, &'c str); $N] {
            fn into_template_var(self) -> TemplateVar {
                self[..].into_template_var()
            }
        }
    )+}
}

array_impls!(0 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);