iters in parse_file

This commit is contained in:
Dmitry Belyaev 2019-07-27 11:23:35 +03:00
parent ce92ec5e87
commit ebae59dcb0
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -115,17 +115,14 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
};
let mut ctx = &mut context;
for line in reader.lines() {
// ignore empty lines
let line_str = String::from(line.unwrap().trim());
let line_s = &line_str;
if line_s.is_empty() {
continue;
}
// find keywords
reader
.lines()
.map(|line| String::from(line.unwrap().trim()))
.filter(|line| !line.is_empty()) // ignore empty lines
.for_each(|line| {
match patterns
.iter()
.find(|&&pattern| line_s.starts_with(pattern) && line_s.ends_with(':'))
.iter() // find keyword
.find(|&&pattern| line.starts_with(pattern) && line.ends_with(':'))
{
Some(pattern) => {
use KeywordType::*;
@ -137,7 +134,7 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
// remember question id
if let Some(QuestionStart) = ctx.cur_keyword_type {
ctx.cur_question_pre["id"] = line_s.replace(':', "").as_str().into();
ctx.cur_question_pre["id"] = line.replace(':', "").as_str().into();
};
// apply accumulated content when new keyword found
@ -180,10 +177,10 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
}
None => {
// accumulate content if line is not a keyword
ctx.cur_content.push(String::from(line_s));
}
ctx.cur_content.push(line);
}
}
});
// finish reading last question
if ctx.have_new_question && !ctx.cur_content.is_empty() {