question: split struct for binary/text serialize
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e4001ee69f
commit
bd67e3ee85
@ -10,7 +10,7 @@ use chgk_ledb_lib::db;
|
|||||||
use chgk_ledb_lib::questions;
|
use chgk_ledb_lib::questions;
|
||||||
use chgk_ledb_lib::source;
|
use chgk_ledb_lib::source;
|
||||||
|
|
||||||
use crate::questions::{Question, QuestionsConverter};
|
use crate::questions::{binary::Question, QuestionsConverter};
|
||||||
use crate::source::ReadSourceQuestionsBatches;
|
use crate::source::ReadSourceQuestionsBatches;
|
||||||
|
|
||||||
const ZIP_FILENAME: &str = "json.zip";
|
const ZIP_FILENAME: &str = "json.zip";
|
||||||
|
@ -16,7 +16,7 @@ use tokio::{fs, io};
|
|||||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
|
|
||||||
use chgk_ledb_lib::async_db;
|
use chgk_ledb_lib::async_db;
|
||||||
use chgk_ledb_lib::questions::Question;
|
use chgk_ledb_lib::questions::binary::Question;
|
||||||
use chgk_ledb_lib::questions::QuestionsConverterAsyncForStream;
|
use chgk_ledb_lib::questions::QuestionsConverterAsyncForStream;
|
||||||
use chgk_ledb_lib::source::ReadSourceQuestionsBatchesAsync;
|
use chgk_ledb_lib::source::ReadSourceQuestionsBatchesAsync;
|
||||||
|
|
||||||
|
@ -2,6 +2,87 @@ use serde_derive::{Deserialize, Serialize};
|
|||||||
|
|
||||||
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
pub struct BatchInfo {
|
pub struct BatchInfo {
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub filename: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub description: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub author: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub comment: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub url: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub date: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub processed_by: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub redacted_by: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub copyright: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub theme: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub kind: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub source: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub rating: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Question {
|
||||||
|
#[serde(default, skip_serializing_if = "u32_is_zero")]
|
||||||
|
pub num: u32,
|
||||||
|
pub id: String,
|
||||||
|
|
||||||
|
pub description: String,
|
||||||
|
pub answer: String,
|
||||||
|
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub author: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub comment: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub comment1: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub tour: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub url: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub date: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub processed_by: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub redacted_by: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub copyright: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub theme: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub kind: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub source: String,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
|
pub rating: String,
|
||||||
|
#[serde(default, skip_serializing_if = "BatchInfo::is_default")]
|
||||||
|
pub batch_info: BatchInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn u32_is_zero(num: &u32) -> bool {
|
||||||
|
*num == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BatchInfo {
|
||||||
|
pub fn is_default(&self) -> bool {
|
||||||
|
*self == BatchInfo::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod binary {
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct BatchInfo {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub filename: String,
|
pub filename: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@ -28,10 +109,10 @@ pub struct BatchInfo {
|
|||||||
pub source: String,
|
pub source: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub rating: String,
|
pub rating: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
pub struct Question {
|
pub struct Question {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub num: u32,
|
pub num: u32,
|
||||||
pub id: String,
|
pub id: String,
|
||||||
@ -65,11 +146,120 @@ pub struct Question {
|
|||||||
pub rating: String,
|
pub rating: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub batch_info: BatchInfo,
|
pub batch_info: BatchInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
use insta::assert_yaml_snapshot;
|
||||||
|
use serde_json::json;
|
||||||
|
pub fn sample_question() -> Question {
|
||||||
|
Question {
|
||||||
|
id: "Вопрос 1".into(),
|
||||||
|
description: "Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2".into(),
|
||||||
|
answer: "42".into(),
|
||||||
|
batch_info: BatchInfo {
|
||||||
|
description: "Тестовый".into(),
|
||||||
|
date: "00-000-2000".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_question_ser() {
|
||||||
|
assert_yaml_snapshot!(sample_question(), @r#"
|
||||||
|
---
|
||||||
|
num: 0
|
||||||
|
id: Вопрос 1
|
||||||
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
||||||
|
answer: "42"
|
||||||
|
author: ""
|
||||||
|
comment: ""
|
||||||
|
comment1: ""
|
||||||
|
tour: ""
|
||||||
|
url: ""
|
||||||
|
date: ""
|
||||||
|
processed_by: ""
|
||||||
|
redacted_by: ""
|
||||||
|
copyright: ""
|
||||||
|
theme: ""
|
||||||
|
kind: ""
|
||||||
|
source: ""
|
||||||
|
rating: ""
|
||||||
|
batch_info:
|
||||||
|
filename: ""
|
||||||
|
description: Тестовый
|
||||||
|
author: ""
|
||||||
|
comment: ""
|
||||||
|
url: ""
|
||||||
|
date: 00-000-2000
|
||||||
|
processed_by: ""
|
||||||
|
redacted_by: ""
|
||||||
|
copyright: ""
|
||||||
|
theme: ""
|
||||||
|
kind: ""
|
||||||
|
source: ""
|
||||||
|
rating: ""
|
||||||
|
|
||||||
|
"#);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_question_de() {
|
||||||
|
let question_from_json: Result<Question, _> = serde_json::from_value(json!({
|
||||||
|
"id": "Вопрос 1",
|
||||||
|
"description": "Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2",
|
||||||
|
"answer": "42",
|
||||||
|
"batch_info": {
|
||||||
|
"description": "Тестовый",
|
||||||
|
"date": "00-000-2000"
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
assert!(question_from_json.is_ok());
|
||||||
|
|
||||||
|
assert_yaml_snapshot!(question_from_json.unwrap(), @r#"
|
||||||
|
---
|
||||||
|
num: 0
|
||||||
|
id: Вопрос 1
|
||||||
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
||||||
|
answer: "42"
|
||||||
|
author: ""
|
||||||
|
comment: ""
|
||||||
|
comment1: ""
|
||||||
|
tour: ""
|
||||||
|
url: ""
|
||||||
|
date: ""
|
||||||
|
processed_by: ""
|
||||||
|
redacted_by: ""
|
||||||
|
copyright: ""
|
||||||
|
theme: ""
|
||||||
|
kind: ""
|
||||||
|
source: ""
|
||||||
|
rating: ""
|
||||||
|
batch_info:
|
||||||
|
filename: ""
|
||||||
|
description: Тестовый
|
||||||
|
author: ""
|
||||||
|
comment: ""
|
||||||
|
url: ""
|
||||||
|
date: 00-000-2000
|
||||||
|
processed_by: ""
|
||||||
|
redacted_by: ""
|
||||||
|
copyright: ""
|
||||||
|
theme: ""
|
||||||
|
kind: ""
|
||||||
|
source: ""
|
||||||
|
rating: ""
|
||||||
|
|
||||||
|
"#);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
||||||
pub mod convert_common {
|
pub mod convert_common {
|
||||||
use super::{BatchInfo, Question};
|
use super::binary::{BatchInfo, Question};
|
||||||
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
||||||
|
|
||||||
macro_rules! make {
|
macro_rules! make {
|
||||||
@ -117,7 +307,7 @@ pub mod convert_common {
|
|||||||
|
|
||||||
#[cfg(feature = "convert")]
|
#[cfg(feature = "convert")]
|
||||||
pub mod convert {
|
pub mod convert {
|
||||||
use super::Question;
|
use super::binary::Question;
|
||||||
use crate::source::SourceQuestionsBatch;
|
use crate::source::SourceQuestionsBatch;
|
||||||
|
|
||||||
pub trait QuestionsConverter {
|
pub trait QuestionsConverter {
|
||||||
@ -234,7 +424,7 @@ pub mod convert_async {
|
|||||||
use futures_core::stream::Stream;
|
use futures_core::stream::Stream;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
use super::Question;
|
use super::binary::Question;
|
||||||
use crate::source::SourceQuestionsBatch;
|
use crate::source::SourceQuestionsBatch;
|
||||||
|
|
||||||
pub struct QuestionsConverterAsync<T>
|
pub struct QuestionsConverterAsync<T>
|
||||||
@ -396,7 +586,6 @@ mod test {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use insta::assert_yaml_snapshot;
|
use insta::assert_yaml_snapshot;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
||||||
pub mod convert_common {
|
pub mod convert_common {
|
||||||
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
||||||
@ -442,37 +631,12 @@ mod test {
|
|||||||
fn test_question_ser() {
|
fn test_question_ser() {
|
||||||
assert_yaml_snapshot!(sample_question(), @r#"
|
assert_yaml_snapshot!(sample_question(), @r#"
|
||||||
---
|
---
|
||||||
num: 0
|
|
||||||
id: Вопрос 1
|
id: Вопрос 1
|
||||||
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
||||||
answer: "42"
|
answer: "42"
|
||||||
author: ""
|
|
||||||
comment: ""
|
|
||||||
comment1: ""
|
|
||||||
tour: ""
|
|
||||||
url: ""
|
|
||||||
date: ""
|
|
||||||
processed_by: ""
|
|
||||||
redacted_by: ""
|
|
||||||
copyright: ""
|
|
||||||
theme: ""
|
|
||||||
kind: ""
|
|
||||||
source: ""
|
|
||||||
rating: ""
|
|
||||||
batch_info:
|
batch_info:
|
||||||
filename: ""
|
|
||||||
description: Тестовый
|
description: Тестовый
|
||||||
author: ""
|
|
||||||
comment: ""
|
|
||||||
url: ""
|
|
||||||
date: 00-000-2000
|
date: 00-000-2000
|
||||||
processed_by: ""
|
|
||||||
redacted_by: ""
|
|
||||||
copyright: ""
|
|
||||||
theme: ""
|
|
||||||
kind: ""
|
|
||||||
source: ""
|
|
||||||
rating: ""
|
|
||||||
|
|
||||||
"#);
|
"#);
|
||||||
}
|
}
|
||||||
@ -491,37 +655,12 @@ mod test {
|
|||||||
|
|
||||||
assert_yaml_snapshot!(question_from_json.unwrap(), @r#"
|
assert_yaml_snapshot!(question_from_json.unwrap(), @r#"
|
||||||
---
|
---
|
||||||
num: 0
|
|
||||||
id: Вопрос 1
|
id: Вопрос 1
|
||||||
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
||||||
answer: "42"
|
answer: "42"
|
||||||
author: ""
|
|
||||||
comment: ""
|
|
||||||
comment1: ""
|
|
||||||
tour: ""
|
|
||||||
url: ""
|
|
||||||
date: ""
|
|
||||||
processed_by: ""
|
|
||||||
redacted_by: ""
|
|
||||||
copyright: ""
|
|
||||||
theme: ""
|
|
||||||
kind: ""
|
|
||||||
source: ""
|
|
||||||
rating: ""
|
|
||||||
batch_info:
|
batch_info:
|
||||||
filename: ""
|
|
||||||
description: Тестовый
|
description: Тестовый
|
||||||
author: ""
|
|
||||||
comment: ""
|
|
||||||
url: ""
|
|
||||||
date: 00-000-2000
|
date: 00-000-2000
|
||||||
processed_by: ""
|
|
||||||
redacted_by: ""
|
|
||||||
copyright: ""
|
|
||||||
theme: ""
|
|
||||||
kind: ""
|
|
||||||
source: ""
|
|
||||||
rating: ""
|
|
||||||
|
|
||||||
"#);
|
"#);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user