From fc1dcc199ee806b1b5088e866e729d2ef24b812c Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 30 Jul 2019 21:15:20 +0300 Subject: [PATCH] check questions before push --- src/main.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 909839b..b0da7ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,23 @@ struct Context { 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 { fn new() -> Context { Context { @@ -157,7 +174,7 @@ fn parse_file(file: impl io::Read) -> Result Result