check questions before push
This commit is contained in:
parent
69f355c470
commit
fc1dcc199e
21
src/main.rs
21
src/main.rs
@ -55,6 +55,23 @@ struct Context {
|
|||||||
last_tag: String,
|
last_tag: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check questions before push
|
||||||
|
trait PushIfValid {
|
||||||
|
fn is_valid(&self) -> bool;
|
||||||
|
fn push_if_valid(&mut self, value: json::JsonValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PushIfValid for json::JsonValue {
|
||||||
|
fn is_valid(&self) -> bool {
|
||||||
|
self.has_key("Вопрос") && self.has_key("Ответ")
|
||||||
|
}
|
||||||
|
fn push_if_valid(&mut self, value: json::JsonValue) {
|
||||||
|
if value.is_valid() {
|
||||||
|
self.push(value).unwrap_or(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
fn new() -> Context {
|
fn new() -> Context {
|
||||||
Context {
|
Context {
|
||||||
@ -157,7 +174,7 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
|
|||||||
ctx.cur_scope = DataScope::QuestionContent;
|
ctx.cur_scope = DataScope::QuestionContent;
|
||||||
// store prev question before reading new
|
// store prev question before reading new
|
||||||
if ctx.have_new_question {
|
if ctx.have_new_question {
|
||||||
ctx.questions.push(ctx.cur_question.clone()).unwrap();
|
ctx.questions.push_if_valid(ctx.cur_question.clone());
|
||||||
}
|
}
|
||||||
// prepare to read new question data with cur_question_pre values
|
// prepare to read new question data with cur_question_pre values
|
||||||
ctx.cur_question = ctx.cur_question_pre.clone();
|
ctx.cur_question = ctx.cur_question_pre.clone();
|
||||||
@ -191,7 +208,7 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er
|
|||||||
// 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() {
|
||||||
ctx.cur_question[&ctx.cur_tag] = ctx.cur_content.join("\n").into();
|
ctx.cur_question[&ctx.cur_tag] = ctx.cur_content.join("\n").into();
|
||||||
ctx.questions.push(ctx.cur_question.clone()).unwrap();
|
ctx.questions.push_if_valid(ctx.cur_question.clone());
|
||||||
ctx.have_new_question = false;
|
ctx.have_new_question = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user