Add conversion to Href

This commit is contained in:
Denis Kolodin 2017-12-24 12:26:23 +03:00
parent 89a580b646
commit d2e770e44d
3 changed files with 40 additions and 1 deletions

View File

@ -16,6 +16,16 @@ enum Filter {
Completed,
}
impl<'a> Into<Href> for &'a Filter {
fn into(self) -> Href {
match *self {
Filter::All => "#/".into(),
Filter::Active => "#/active".into(),
Filter::Completed => "#/completed".into(),
}
}
}
impl Filter {
fn fit(&self, entry: &Entry) -> bool {
match *self {
@ -183,7 +193,7 @@ fn view_filter(_model: &Model, filter: Filter) -> Html<Msg> {
let flt = filter.clone();
html! {
<li>
<a href="#/", onclick=move |_| Msg::SetFilter(flt.clone()),>
<a href=&flt, onclick=move |_| Msg::SetFilter(flt.clone()),>
{ filter }
</a>
</li>

View File

@ -165,3 +165,26 @@ impl<T: IKeyboardEvent> From<T> for KeyData {
}
}
#[derive(Debug)]
pub struct Href {
link: String,
}
impl From<String> for Href {
fn from(link: String) -> Self {
Href { link }
}
}
impl<'a> From<&'a str> for Href {
fn from(link: &'a str) -> Self {
Href { link: link.to_owned() }
}
}
impl ToString for Href {
fn to_string(&self) -> String {
self.link.to_owned()
}
}

View File

@ -49,6 +49,12 @@ macro_rules! html_impl {
$crate::macros::attach_listener(&mut $stack, Box::new(listener));
html_impl! { $stack ($($tail)*) }
};
// Attributes:
($stack:ident (href = $href:expr, $($tail:tt)*)) => {
let href: $crate::html::Href = $href.into();
$crate::macros::add_attribute(&mut $stack, "href", href);
html_impl! { $stack ($($tail)*) }
};
($stack:ident ($attr:ident = $val:expr, $($tail:tt)*)) => {
$crate::macros::add_attribute(&mut $stack, stringify!($attr), $val);
html_impl! { $stack ($($tail)*) }