more high-level parsing

This commit is contained in:
kud1ing 2016-12-10 20:32:42 +01:00
parent 21f8b53241
commit 98d01abf50

View File

@ -6,6 +6,80 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;
/// The content of the README.md file.
#[derive(Debug)]
struct ReadmeContent<'a> {
/// The header Markdown events.
header: Vec<Event<'a>>,
}
impl<'a> ReadmeContent<'a> {
fn new() -> ReadmeContent<'a> {
ReadmeContent {
header: Vec::new()
}
}
}
/// The state of the README.md file high-level parser.
#[derive(Debug, PartialEq)]
enum ReadmeParsingState {
Header,
TableOfContent,
Content,
Footer,
Finished,
}
fn parse_header(parser: &mut Parser, content: &mut ReadmeContent) -> ReadmeParsingState {
// TODO
println!("TODO: `parse_header()`");
// Iterate over the Markdown entities.
/*
while let Some(event) = parser.next() {
match event {
Event::Start(tag) => println!("start: {:?}", tag),
Event::End(tag) => println!("end: {:?}", tag),
Event::Text(text) => println!("text: {:?}", text),
_ => (),
}
}
*/
ReadmeParsingState::TableOfContent
}
fn parse_toc(parser: &mut Parser, content: &mut ReadmeContent) -> ReadmeParsingState {
// TODO
println!("TODO: `parse_toc()`");
ReadmeParsingState::Content
}
fn parse_content(parser: &mut Parser, content: &mut ReadmeContent) -> ReadmeParsingState {
// TODO
println!("TODO: `parse_content()`");
ReadmeParsingState::Footer
}
fn parse_footer(parser: &mut Parser, content: &mut ReadmeContent) -> ReadmeParsingState {
// TODO
println!("TODO: `parse_footer()`");
ReadmeParsingState::Finished
}
fn main() {
let readme_file_path = Path::new("README.md");
@ -17,23 +91,26 @@ fn main() {
Ok(file) => file,
};
let mut readme_file_content = String::new();
let mut markdown_string = String::new();
// Try to read the README.md file in a string.
if let Err(why) = file.read_to_string(&mut readme_file_content) {
if let Err(why) = file.read_to_string(&mut markdown_string) {
panic!("couldn't read {}: {}", display, why.description());
}
// Parse the Markdown string.
let mut parser = Parser::new(&readme_file_content);
// Parser for the Markdownstring.
let mut parser = Parser::new(&markdown_string);
// Iterate over the Markdown entities.
while let Some(event) = parser.next() {
match event {
Event::Start(tag) => println!("start: {:?}", tag),
Event::End(tag) => println!("end: {:?}", tag),
Event::Text(text) => println!("text: {:?}", text),
_ => (),
let mut content = ReadmeContent::new();
let mut state = ReadmeParsingState::Header;
while state != ReadmeParsingState::Finished {
match state {
ReadmeParsingState::Header => state = parse_header(&mut parser, &mut content),
ReadmeParsingState::TableOfContent => state = parse_toc(&mut parser, &mut content),
ReadmeParsingState::Content => state = parse_content(&mut parser, &mut content),
ReadmeParsingState::Footer => state = parse_footer(&mut parser, &mut content),
ReadmeParsingState::Finished => state = ReadmeParsingState::Finished,
}
}
}