Browse Source

Use defaults, so the structre can extend without changing too much code

master
Julio Biason 6 years ago
parent
commit
5fa2d10adb
  1. 18
      src/main.rs

18
src/main.rs

@ -8,29 +8,31 @@ enum CurrentTag {
Title Title
} }
#[derive(Default)]
struct State { struct State {
tag: Option<CurrentTag>, tag: Option<CurrentTag>,
title: String title: Option<String>
} }
impl State { impl State {
pub fn new() -> Self { pub fn new() -> Self {
Self { tag: None, title: String::from("") } Self { tag: None, title: None }
} }
pub fn change_title(self, title:&str) -> Self { pub fn change_title(self, title:&str) -> Self {
Self { tag:self.tag, Self { title: Some(title.to_string()),
title:title.to_string() } ..Default::default()
}
} }
pub fn change_tag(self, tag:CurrentTag) -> Self { pub fn change_tag(self, tag:CurrentTag) -> Self {
Self { tag: Some(tag), Self { tag: Some(tag),
title:self.title } ..Default::default() }
} }
pub fn remove_tag(self) -> Self { pub fn remove_tag(self) -> Self {
Self { tag: None, Self { tag: None,
title:self.title } ..Default::default() }
} }
} }
@ -60,6 +62,7 @@ fn main() {
Event::ElementStart(tag) => { Event::ElementStart(tag) => {
match tag.name.as_ref() { match tag.name.as_ref() {
"title" => state.change_tag(CurrentTag::Title), "title" => state.change_tag(CurrentTag::Title),
"note" => State::new(), // the start of a note resets everything
_ => state _ => state
} }
}, },
@ -71,7 +74,8 @@ fn main() {
match state.tag { match state.tag {
Some(CurrentTag::Title) => { Some(CurrentTag::Title) => {
println!("TITLE: {}", slugify(&data)); println!("TITLE: {}", slugify(&data));
state.change_title(&slugify(data)) let new_state = state.change_title(&slugify(&data));
new_state
}, },
_ => state _ => state
} }

Loading…
Cancel
Save