iters in parse_file
This commit is contained in:
parent
ce92ec5e87
commit
ebae59dcb0
125
src/main.rs
125
src/main.rs
@ -115,75 +115,72 @@ 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;
|
match patterns
|
||||||
}
|
.iter() // find keyword
|
||||||
// find keywords
|
.find(|&&pattern| line.starts_with(pattern) && line.ends_with(':'))
|
||||||
match patterns
|
{
|
||||||
.iter()
|
Some(pattern) => {
|
||||||
.find(|&&pattern| line_s.starts_with(pattern) && line_s.ends_with(':'))
|
use KeywordType::*;
|
||||||
{
|
|
||||||
Some(pattern) => {
|
|
||||||
use KeywordType::*;
|
|
||||||
|
|
||||||
ctx.last_keyword_type = ctx.cur_keyword_type;
|
ctx.last_keyword_type = ctx.cur_keyword_type;
|
||||||
ctx.last_tag = ctx.cur_tag.clone();
|
ctx.last_tag = ctx.cur_tag.clone();
|
||||||
ctx.cur_keyword_type = Some(keyword_type(&pattern));
|
ctx.cur_keyword_type = Some(keyword_type(&pattern));
|
||||||
ctx.cur_tag = pattern.replace(' ', "").replace(':', "");
|
ctx.cur_tag = pattern.replace(' ', "").replace(':', "");
|
||||||
|
|
||||||
// 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
|
||||||
match ctx.last_keyword_type {
|
match ctx.last_keyword_type {
|
||||||
Some(Global) => {
|
Some(Global) => {
|
||||||
ctx.cur_scope = DataScope::Global;
|
ctx.cur_scope = DataScope::Global;
|
||||||
ctx.data[&ctx.last_tag] = ctx.cur_content.join("\n").into()
|
ctx.data[&ctx.last_tag] = ctx.cur_content.join("\n").into()
|
||||||
}
|
|
||||||
Some(QuestionPre) => {
|
|
||||||
ctx.cur_scope = DataScope::QuestionPre;
|
|
||||||
ctx.cur_question_pre[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
|
||||||
}
|
|
||||||
Some(QuestionStart) => {
|
|
||||||
ctx.cur_scope = DataScope::QuestionContent;
|
|
||||||
// store prev question before reading new
|
|
||||||
if ctx.have_new_question {
|
|
||||||
ctx.questions.push(ctx.cur_question.clone()).unwrap();
|
|
||||||
}
|
}
|
||||||
// prepare for read new question data with cur_question_pre values
|
Some(QuestionPre) => {
|
||||||
ctx.cur_question = ctx.cur_question_pre.clone();
|
ctx.cur_scope = DataScope::QuestionPre;
|
||||||
// ctx.cur_question_pre = json::JsonValue::new_object(); // uncomment => forget pre at new question
|
ctx.cur_question_pre[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
||||||
ctx.cur_question[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
}
|
||||||
ctx.have_new_question = true;
|
Some(QuestionStart) => {
|
||||||
}
|
ctx.cur_scope = DataScope::QuestionContent;
|
||||||
Some(QuestionContent) => {
|
// store prev question before reading new
|
||||||
ctx.cur_question[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
if ctx.have_new_question {
|
||||||
}
|
ctx.questions.push(ctx.cur_question.clone()).unwrap();
|
||||||
Some(CurrentScope) => {
|
}
|
||||||
// match value to store data
|
// prepare for read new question data with cur_question_pre values
|
||||||
(match ctx.cur_scope {
|
ctx.cur_question = ctx.cur_question_pre.clone();
|
||||||
DataScope::Global => &mut ctx.data,
|
// ctx.cur_question_pre = json::JsonValue::new_object(); // uncomment => forget pre at new question
|
||||||
DataScope::QuestionPre => &mut ctx.cur_question_pre,
|
ctx.cur_question[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
||||||
DataScope::QuestionContent => &mut ctx.cur_question,
|
ctx.have_new_question = true;
|
||||||
})[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
}
|
||||||
}
|
Some(QuestionContent) => {
|
||||||
_ => (), //None or Ignore
|
ctx.cur_question[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
||||||
};
|
}
|
||||||
// clear content
|
Some(CurrentScope) => {
|
||||||
ctx.cur_content.clear();
|
// match value to store data
|
||||||
|
(match ctx.cur_scope {
|
||||||
|
DataScope::Global => &mut ctx.data,
|
||||||
|
DataScope::QuestionPre => &mut ctx.cur_question_pre,
|
||||||
|
DataScope::QuestionContent => &mut ctx.cur_question,
|
||||||
|
})[&ctx.last_tag] = ctx.cur_content.join("\n").into();
|
||||||
|
}
|
||||||
|
_ => (), //None or Ignore
|
||||||
|
};
|
||||||
|
// clear content
|
||||||
|
ctx.cur_content.clear();
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// accumulate content if line is not a keyword
|
||||||
|
ctx.cur_content.push(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
});
|
||||||
// accumulate content if line is not a keyword
|
|
||||||
ctx.cur_content.push(String::from(line_s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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