diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs index 723fd32bf..92adce7ea 100644 --- a/examples/todomvc/src/main.rs +++ b/examples/todomvc/src/main.rs @@ -16,6 +16,16 @@ enum Filter { Completed, } +impl<'a> Into 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 { let flt = filter.clone(); html! {
  • - + { filter }
  • diff --git a/src/html.rs b/src/html.rs index 57a0c9ead..eb3354b00 100644 --- a/src/html.rs +++ b/src/html.rs @@ -165,3 +165,26 @@ impl From for KeyData { } } +#[derive(Debug)] +pub struct Href { + link: String, +} + +impl From 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() + } +} + diff --git a/src/macros.rs b/src/macros.rs index fe1c8b9b6..7677b2b2a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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)*) }