Browse Source

Instead of going to unwrap() directly, capture the Event

This way, we can actually slowly feed the parser with content and it
will slowly process the elements.
master
Julio Biason 6 years ago
parent
commit
974cc88a9f
  1. 55
      src/main.rs

55
src/main.rs

@ -141,6 +141,29 @@ fn close_tag(current_state: State, tag: &str) -> State {
} }
} }
fn parse_element(element:Event, state: State) -> State {
match element {
Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()),
Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
Event::Characters(data) => {
match state.tag {
Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
Some(CurrentTag::ResourceData) => state.with_data(
data.into_bytes()
.iter()
.filter(|&x| *x != 13 && *x != 10) // remove new lines, it is base 64, after all
.map(|&x| x)
.collect(),
),
Some(CurrentTag::Content) => state.with_content(data.into_bytes()),
Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
_ => state,
}
}
_ => state,
}
}
fn main() { fn main() {
let args: Vec<_> = env::args().collect(); let args: Vec<_> = env::args().collect();
if args.len() != 2 { if args.len() != 2 {
@ -162,30 +185,12 @@ fn main() {
let mut parser = Parser::new(); let mut parser = Parser::new();
parser.feed_str(&contents); parser.feed_str(&contents);
parser.fold(State::new(), { parser.fold(State::new(), { |state: State, parsing_result| {
|state: State, element| { println!("State: {:?}", state);
println!("State: {:?}", state);
match parsing_result {
match element.unwrap() { Ok(element) => parse_element(element, state),
Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()), Err(_) => state,
Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
Event::Characters(data) => {
match state.tag {
Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
Some(CurrentTag::ResourceData) => state.with_data(
data.into_bytes()
.iter()
.filter(|&x| *x != 13 && *x != 10) // remove new lines, it is base 64, after all
.map(|&x| x)
.collect(),
),
Some(CurrentTag::Content) => state.with_content(data.into_bytes()),
Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
_ => state,
}
}
_ => state,
}
} }
}); }});
} }

Loading…
Cancel
Save