Skip to content

Commit

Permalink
Merge pull request #3 from DenisKolodin/add-strum
Browse files Browse the repository at this point in the history
Add strum
  • Loading branch information
therustmonk authored Dec 24, 2017
2 parents c592af8 + d2e770e commit 4f57fa7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 38 deletions.
50 changes: 50 additions & 0 deletions examples/todomvc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/todomvc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ version = "0.1.0"
authors = ["Denis Kolodin <deniskolodin@gmail.com>"]

[dependencies]
strum = "0.8.0"
strum_macros = "0.8.0"
yew = { path = "../.." }
70 changes: 32 additions & 38 deletions examples/todomvc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#![recursion_limit="128"]

extern crate strum;
#[macro_use]
extern crate strum_macros;
#[macro_use]
extern crate yew;

use strum::IntoEnumIterator;
use yew::html::*;

#[derive(Clone, PartialEq)]
#[derive(EnumIter, ToString, Clone, PartialEq)]
enum Filter {
All,
Active,
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 {
Expand All @@ -22,17 +36,6 @@ impl Filter {
}
}

impl ToString for Filter {
fn to_string(&self) -> String {
let name = match *self {
Filter::All => "All",
Filter::Active => "Active",
Filter::Completed => "Completed",
};
name.to_string()
}
}

struct Model {
entries: Vec<Entry>,
filter: Filter,
Expand Down Expand Up @@ -160,29 +163,17 @@ fn view(model: &Model) -> Html<Msg> {
</header>
<section class="main",>
<input class="toggle-all", type="checkbox", checked=model.is_all_completed(), onclick=|_| Msg::ToggleAll, />
{ view_entries(&model) }
<ul class="todo-list",>
{ for model.entries.iter().filter(|e| model.filter.fit(e)).enumerate().map(view_entry) }
</ul>
</section>
<footer class="footer",>
<span class="todo-count",>
<strong>{ model.total() }</strong>
{ " item(s) left" }
</span>
<ul class="filters",>
<li>
<a href="#/", onclick=|_| Msg::SetFilter(Filter::All),>
{ Filter::All }
</a>
</li>
<li>
<a href="#/active", onclick=|_| Msg::SetFilter(Filter::Active),>
{ Filter::Active }
</a>
</li>
<li>
<a href="#/completed", onclick=|_| Msg::SetFilter(Filter::Completed),>
{ Filter::Completed }
</a>
</li>
{ for Filter::iter().map(|flt| view_filter(&model, flt)) }
</ul>
<button class="clear-completed", onclick=|_| Msg::ClearCompleted,>
{ format!("Clear completed ({})", model.total_completed()) }
Expand All @@ -198,25 +189,28 @@ fn view(model: &Model) -> Html<Msg> {
}
}

fn view_filter(_model: &Model, filter: Filter) -> Html<Msg> {
let flt = filter.clone();
html! {
<li>
<a href=&flt, onclick=move |_| Msg::SetFilter(flt.clone()),>
{ filter }
</a>
</li>
}
}

fn view_input(model: &Model) -> Html<Msg> {
html! {
// You can use standard Rust comments. One line:
// <li></li>
<input class="new-todo",
placeholder="What needs to be done?",
value=&model.value,
oninput=|e: InputData| Msg::Update(e.value),
onkeypress=|e: KeyData| {
if e.key == "Enter" { Msg::Add } else { Msg::Nope }
}, />
}
}

fn view_entries(model: &Model) -> Html<Msg> {
html! {
<ul class="todo-list",>
{ for model.entries.iter().filter(|e| model.filter.fit(e)).enumerate().map(view_entry) }
// You can use standard Rust comments. One line:
// <li></li>
</ul>
/* Or multiline:
<ul>
<li></li>
Expand Down
23 changes: 23 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

6 changes: 6 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*) }
Expand Down

0 comments on commit 4f57fa7

Please sign in to comment.