iters in parse_file
This commit is contained in:
parent
ce92ec5e87
commit
ebae59dcb0
23
src/main.rs
23
src/main.rs
@ -115,17 +115,14 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
|
|||||||
};
|
};
|
||||||
let mut ctx = &mut context;
|
let mut ctx = &mut context;
|
||||||
|
|
||||||
for line in reader.lines() {
|
reader
|
||||||
// ignore empty lines
|
.lines()
|
||||||
let line_str = String::from(line.unwrap().trim());
|
.map(|line| String::from(line.unwrap().trim()))
|
||||||
let line_s = &line_str;
|
.filter(|line| !line.is_empty()) // ignore empty lines
|
||||||
if line_s.is_empty() {
|
.for_each(|line| {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// find keywords
|
|
||||||
match patterns
|
match patterns
|
||||||
.iter()
|
.iter() // find keyword
|
||||||
.find(|&&pattern| line_s.starts_with(pattern) && line_s.ends_with(':'))
|
.find(|&&pattern| line.starts_with(pattern) && line.ends_with(':'))
|
||||||
{
|
{
|
||||||
Some(pattern) => {
|
Some(pattern) => {
|
||||||
use KeywordType::*;
|
use KeywordType::*;
|
||||||
@ -137,7 +134,7 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
|
|||||||
|
|
||||||
// remember question id
|
// remember question id
|
||||||
if let Some(QuestionStart) = ctx.cur_keyword_type {
|
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
|
// 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 => {
|
None => {
|
||||||
// accumulate content if line is not a keyword
|
// 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
|
// finish reading last question
|
||||||
if ctx.have_new_question && !ctx.cur_content.is_empty() {
|
if ctx.have_new_question && !ctx.cur_content.is_empty() {
|
||||||
|
Loading…
Reference in New Issue
Block a user