pub trait IntoTemplateVar {
// Required method
fn into_template_var(self) -> TemplateVar;
}
Expand description
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.
ⓘ
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.
ⓘ
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"
);