diff --git a/src/main.rs b/src/main.rs index 95841c4..418f6f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,6 +70,24 @@ struct Context { last_tag: String, } +impl Context { + fn new() -> Context { + Context { + data: json::JsonValue::new_object(), + questions: json::JsonValue::new_array(), + cur_keyword_type: None, + cur_question: json::JsonValue::new_object(), + cur_question_pre: json::JsonValue::new_object(), + cur_tag: String::new(), + cur_content: Vec::<String>::new(), + cur_scope: DataScope::Global, + have_new_question: false, + last_keyword_type: None, + last_tag: String::new(), + } + } +} + fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Error>> { let buf = io::BufReader::new(file); let reader = TextReader::new(buf, KOI8_R, DecoderTrap::Ignore); @@ -99,20 +117,8 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<std::error::Er "Комментарий:", "Комментарии:", ]; - // init context - let mut context = Context { - data: json::JsonValue::new_object(), - questions: json::JsonValue::new_array(), - cur_keyword_type: None, - cur_question: json::JsonValue::new_object(), - cur_question_pre: json::JsonValue::new_object(), - cur_tag: String::new(), - cur_content: Vec::<String>::new(), - cur_scope: DataScope::Global, - have_new_question: false, - last_keyword_type: None, - last_tag: String::new(), - }; + + let mut context = Context::new(); let mut ctx = &mut context; reader