uritemplate/
templatevar.rs1use std::collections::HashMap;
2
3#[derive(Clone)]
10pub enum TemplateVar {
11 Scalar(String),
13 List(Vec<String>),
15 AssociativeArray(Vec<(String, String)>),
18}
19
20pub trait IntoTemplateVar {
66 fn into_template_var(self) -> TemplateVar;
67}
68
69impl IntoTemplateVar for TemplateVar {
70 fn into_template_var(self) -> TemplateVar {
71 self.clone()
72 }
73}
74
75impl<'a> IntoTemplateVar for &'a str {
76 fn into_template_var(self) -> TemplateVar {
77 TemplateVar::Scalar(self.to_string())
78 }
79}
80
81impl IntoTemplateVar for String {
82 fn into_template_var(self) -> TemplateVar {
83 TemplateVar::Scalar(self)
84 }
85}
86
87impl<'a> IntoTemplateVar for &'a [String] {
88 fn into_template_var(self) -> TemplateVar {
89 let mut vec = Vec::new();
90 for s in self {
91 vec.push(s.clone());
92 }
93 TemplateVar::List(vec)
94 }
95}
96
97impl IntoTemplateVar for Vec<String> {
98 fn into_template_var(self) -> TemplateVar {
99 TemplateVar::List(self)
100 }
101}
102
103impl<'a, 'b> IntoTemplateVar for &'a [&'b str] {
104 fn into_template_var(self) -> TemplateVar {
105 let mut vec = Vec::new();
106 for s in self {
107 vec.push(s.to_string());
108 }
109 TemplateVar::List(vec)
110 }
111}
112
113impl<'a> IntoTemplateVar for &'a [(String, String)] {
114 fn into_template_var(self) -> TemplateVar {
115 let mut vec = Vec::new();
116 for s in self {
117 vec.push(s.clone());
118 }
119 TemplateVar::AssociativeArray(vec)
120 }
121}
122
123impl IntoTemplateVar for Vec<(String, String)> {
124 fn into_template_var(self) -> TemplateVar {
125 TemplateVar::AssociativeArray(self)
126 }
127}
128
129impl<'a, 'b, 'c> IntoTemplateVar for &'a [(&'b str, &'c str)] {
130 fn into_template_var(self) -> TemplateVar {
131 let mut vec = Vec::new();
132 for s in self {
133 vec.push((s.0.to_string(), s.1.to_string()));
134 }
135 TemplateVar::AssociativeArray(vec)
136 }
137}
138
139impl<'a> IntoTemplateVar for &'a HashMap<String, String> {
140 fn into_template_var(self) -> TemplateVar {
141 let mut vec = Vec::new();
142 for (k, v) in self {
143 vec.push((k.clone(), v.clone()));
144 }
145 TemplateVar::AssociativeArray(vec)
146 }
147}
148
149impl<'a, 'b, 'c> IntoTemplateVar for &'a HashMap<&'b str, &'c str> {
150 fn into_template_var(self) -> TemplateVar {
151 let mut vec = Vec::new();
152 for (k, v) in self {
153 vec.push((k.to_string(), v.to_string()));
154 }
155 TemplateVar::AssociativeArray(vec)
156 }
157}
158
159macro_rules! array_impls {
160 ($($N:expr)+) => {$(
161 impl <'a> IntoTemplateVar for &'a [String; $N] {
162 fn into_template_var(self) -> TemplateVar {
163 self[..].into_template_var()
164 }
165 }
166
167 impl <'a, 'b> IntoTemplateVar for &'a[&'b str; $N] {
168 fn into_template_var(self) -> TemplateVar {
169 self[..].into_template_var()
170 }
171 }
172
173 impl <'a> IntoTemplateVar for &'a [(String, String); $N] {
174 fn into_template_var(self) -> TemplateVar {
175 self[..].into_template_var()
176 }
177 }
178
179 impl <'a, 'b, 'c> IntoTemplateVar for &'a[(&'b str, &'c str); $N] {
180 fn into_template_var(self) -> TemplateVar {
181 self[..].into_template_var()
182 }
183 }
184 )+}
185}
186
187array_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
188 26 27 28 29 30 31 32);