543 lines
17 KiB
Rust
543 lines
17 KiB
Rust
use serde_derive::{Deserialize, Serialize};
|
|
use serde::ser::SerializeStruct;
|
|
|
|
use bitflags::bitflags;
|
|
|
|
bitflags! {
|
|
#[repr(transparent)]
|
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
pub struct BatchFlags: u16 {
|
|
const FILENAME = 1;
|
|
const DESCRIPTION = 1 << 1;
|
|
const AUTHOR = 1 << 2;
|
|
const COMMENT = 1 << 3;
|
|
const URL = 1 << 4;
|
|
const DATE = 1 << 5;
|
|
const PROCESSED = 1 << 6;
|
|
const REDACTED = 1 << 7;
|
|
const COPYRIGHT = 1 << 8;
|
|
const THEME = 1 << 9;
|
|
const KIND = 1 << 10;
|
|
const SOURCE = 1 << 11;
|
|
const RATING = 1 << 12;
|
|
}
|
|
}
|
|
|
|
bitflags! {
|
|
#[repr(transparent)]
|
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
pub struct QuestionFlags: u16 {
|
|
const NUM = 1;
|
|
const AUTHOR = 1 << 1;
|
|
const COMMENT = 1 << 2;
|
|
const COMMENT1 = 1 << 3;
|
|
const TOUR = 1 << 4;
|
|
const URL = 1 << 5;
|
|
const DATE = 1 << 6;
|
|
const PROCESSED_BY = 1 << 7;
|
|
const REDACTED_BY = 1 << 8;
|
|
const COPYRIGHT = 1 << 9;
|
|
const THEME = 1 << 10;
|
|
const KIND = 1 << 11;
|
|
const SOURCE = 1 << 12;
|
|
const RATING = 1 << 13;
|
|
const BATCH_INFO = 1 << 14;
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, Deserialize, PartialEq)]
|
|
pub struct BatchInfo {
|
|
#[serde(default)]
|
|
pub filename: String,
|
|
#[serde(default)]
|
|
pub description: String,
|
|
#[serde(default)]
|
|
pub author: String,
|
|
#[serde(default)]
|
|
pub comment: String,
|
|
#[serde(default)]
|
|
pub url: String,
|
|
#[serde(default)]
|
|
pub date: String,
|
|
#[serde(default)]
|
|
pub processed_by: String,
|
|
#[serde(default)]
|
|
pub redacted_by: String,
|
|
#[serde(default)]
|
|
pub copyright: String,
|
|
#[serde(default)]
|
|
pub theme: String,
|
|
#[serde(default)]
|
|
pub kind: String,
|
|
#[serde(default)]
|
|
pub source: String,
|
|
#[serde(default)]
|
|
pub rating: String,
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, 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,
|
|
}
|
|
|
|
impl BatchInfo {
|
|
pub fn is_default(&self) -> bool {
|
|
*self == BatchInfo::default()
|
|
}
|
|
}
|
|
|
|
macro_rules! count_string_fields {
|
|
(($self:ident, $flags:ident, $len:ident, $FlagsType:ident) <- {$($field:ident:$flag:ident),+} ) => {$(
|
|
$len += 1;
|
|
if !$self.$field.is_empty() {
|
|
$flags |= $FlagsType::$flag;
|
|
}
|
|
)+}
|
|
}
|
|
|
|
macro_rules! serialize_fields {
|
|
(($self:ident, $flags:ident, $state:ident, $FlagsType:ident) <- {$($field:ident:$flag:ident),+} ) => {$(
|
|
if $flags.intersects($FlagsType::$flag) {
|
|
$state.serialize_field(std::stringify!($field), &$self.$field)?;
|
|
} else {
|
|
$state.skip_field(std::stringify!($field))?;
|
|
}
|
|
)+}
|
|
}
|
|
|
|
impl serde::Serialize for BatchInfo {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: serde::Serializer,
|
|
{
|
|
let is_human_readable = serializer.is_human_readable();
|
|
let mut flags: BatchFlags = Default::default();
|
|
let mut len = 1; // (+flags)
|
|
|
|
count_string_fields!((self, flags, len, BatchFlags) <- {
|
|
filename: FILENAME, description: DESCRIPTION, author: AUTHOR, comment: COMMENT, url: URL, date: DATE,
|
|
processed_by: PROCESSED, copyright: COPYRIGHT, theme: THEME, kind: KIND, source: SOURCE, rating: RATING
|
|
});
|
|
|
|
let mut state = serializer.serialize_struct("BatchInfo", len)?;
|
|
|
|
if is_human_readable {
|
|
state.skip_field("_flags")?;
|
|
} else {
|
|
state.serialize_field("_flags", &flags.bits())?;
|
|
}
|
|
|
|
serialize_fields!((self, flags, state, BatchFlags) <- {
|
|
filename: FILENAME, description: DESCRIPTION, author: AUTHOR, comment: COMMENT, url: URL, date: DATE,
|
|
processed_by: PROCESSED, copyright: COPYRIGHT, theme: THEME, kind: KIND, source: SOURCE, rating: RATING
|
|
});
|
|
|
|
state.end()
|
|
}
|
|
}
|
|
|
|
impl serde::Serialize for Question {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: serde::Serializer,
|
|
{
|
|
let is_human_readable = serializer.is_human_readable();
|
|
let mut flags: QuestionFlags = Default::default();
|
|
let mut len = 6; //(_flags + id + description + answer + num + batch_info)
|
|
|
|
if self.num != 0 {
|
|
flags |= QuestionFlags::NUM;
|
|
}
|
|
|
|
if !self.batch_info.is_default() {
|
|
flags |= QuestionFlags::BATCH_INFO;
|
|
}
|
|
|
|
count_string_fields!((self, flags, len, QuestionFlags) <- {
|
|
author: AUTHOR, comment: COMMENT, comment1: COMMENT1, tour: TOUR, url: URL, date: DATE,
|
|
processed_by: PROCESSED_BY, redacted_by: REDACTED_BY, copyright: COPYRIGHT, theme: THEME,
|
|
kind: KIND, source: SOURCE, rating: RATING
|
|
});
|
|
|
|
let mut state = serializer.serialize_struct("Question", len)?;
|
|
|
|
if is_human_readable {
|
|
state.skip_field("_flags")?;
|
|
} else {
|
|
state.serialize_field("_flags", &flags.bits())?;
|
|
}
|
|
|
|
if flags.intersects(QuestionFlags::NUM) {
|
|
state.serialize_field("num", &self.num)?;
|
|
} else {
|
|
state.skip_field("num")?;
|
|
}
|
|
|
|
state.serialize_field("id", &self.id)?;
|
|
state.serialize_field("description", &self.description)?;
|
|
state.serialize_field("answer", &self.answer)?;
|
|
|
|
serialize_fields!((self, flags, state, QuestionFlags) <- {
|
|
author: AUTHOR, comment: COMMENT, comment1: COMMENT1, tour: TOUR, url: URL, date: DATE,
|
|
processed_by: PROCESSED_BY, redacted_by: REDACTED_BY, copyright: COPYRIGHT, theme: THEME,
|
|
kind: KIND, source: SOURCE, rating: RATING, batch_info: BATCH_INFO
|
|
});
|
|
|
|
if flags.intersects(QuestionFlags::BATCH_INFO) {
|
|
state.serialize_field("batch_info", &self.batch_info)?;
|
|
} else {
|
|
state.skip_field("batch_info")?;
|
|
}
|
|
|
|
state.end()
|
|
}
|
|
}
|
|
|
|
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
|
pub mod convert_common {
|
|
use super::{BatchInfo, Question};
|
|
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
|
|
|
macro_rules! make {
|
|
($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$(
|
|
$field: $src.$field
|
|
),+}};
|
|
($Target:ident; with defaults and by {$($field:ident),+}; from $src:expr) => {$Target {$(
|
|
$field: $src.$field
|
|
),+ ,..$Target::default()}}
|
|
}
|
|
|
|
impl From<SourceQuestion> for Question {
|
|
fn from(src: SourceQuestion) -> Self {
|
|
make! {Self; with defaults and by {
|
|
num, id, description, answer, author, comment, comment1, tour, url,
|
|
date, processed_by, redacted_by, copyright, theme, kind, source, rating
|
|
}; from src}
|
|
}
|
|
}
|
|
|
|
impl From<SourceQuestionsBatch> for BatchInfo {
|
|
fn from(src: SourceQuestionsBatch) -> Self {
|
|
make! {Self; by {
|
|
filename, description, author, comment, url, date,
|
|
processed_by, redacted_by, copyright, theme, kind, source, rating
|
|
}; from src}
|
|
}
|
|
}
|
|
|
|
impl From<SourceQuestionsBatch> for Vec<Question> {
|
|
fn from(src: SourceQuestionsBatch) -> Self {
|
|
let mut src = src;
|
|
let mut questions: Vec<SourceQuestion> = vec![];
|
|
std::mem::swap(&mut src.questions, &mut questions);
|
|
let mut result: Vec<Question> = questions.into_iter().map(|item| item.into()).collect();
|
|
let batch_info = BatchInfo::from(src);
|
|
result.iter_mut().for_each(|question| {
|
|
question.batch_info = batch_info.clone();
|
|
});
|
|
|
|
result
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "convert")]
|
|
pub mod convert {
|
|
use super::Question;
|
|
use crate::source::SourceQuestionsBatch;
|
|
|
|
pub trait QuestionsConverter {
|
|
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a>;
|
|
}
|
|
|
|
impl<T> QuestionsConverter for T
|
|
where
|
|
T: Iterator<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>,
|
|
{
|
|
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a> {
|
|
let iter = self
|
|
.filter(|(_, data)| data.is_ok())
|
|
.flat_map(|(filename, data)| {
|
|
let mut batch = data.unwrap();
|
|
batch.filename = filename;
|
|
let questions: Vec<Question> = batch.into();
|
|
questions
|
|
});
|
|
Box::new(iter)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use crate::questions::test::convert_common::sample_batch;
|
|
|
|
use super::*;
|
|
use insta::assert_yaml_snapshot;
|
|
use std::iter;
|
|
|
|
#[test]
|
|
fn test_convert() {
|
|
let mut source = iter::once((
|
|
String::from("test.json"),
|
|
Ok::<SourceQuestionsBatch, serde_json::Error>(sample_batch()),
|
|
));
|
|
let converted: Vec<_> = source.convert().collect();
|
|
assert_yaml_snapshot!(converted, @r#"
|
|
---
|
|
- id: Вопрос 1
|
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
|
answer: "42"
|
|
batch_info:
|
|
filename: test.json
|
|
description: Тестовый
|
|
date: 00-000-2000
|
|
- id: Вопрос 2
|
|
description: Зимой и летом одним цветом
|
|
answer: ёлка
|
|
batch_info:
|
|
filename: test.json
|
|
description: Тестовый
|
|
date: 00-000-2000
|
|
|
|
"#);
|
|
}
|
|
}
|
|
}
|
|
#[cfg(feature = "convert")]
|
|
pub use convert::QuestionsConverter;
|
|
|
|
#[cfg(feature = "convert_async")]
|
|
pub mod convert_async {
|
|
use futures::stream;
|
|
use futures_core::stream::Stream;
|
|
use futures_util::StreamExt;
|
|
|
|
use super::Question;
|
|
use crate::source::SourceQuestionsBatch;
|
|
|
|
pub struct QuestionsConverterAsync<T>
|
|
where
|
|
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
|
+ std::marker::Unpin,
|
|
{
|
|
inner: T,
|
|
}
|
|
|
|
impl<T> From<T> for QuestionsConverterAsync<T>
|
|
where
|
|
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
|
+ std::marker::Unpin,
|
|
{
|
|
fn from(inner: T) -> Self {
|
|
Self { inner }
|
|
}
|
|
}
|
|
|
|
pub trait QuestionsConverterAsyncForStream<T>
|
|
where
|
|
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
|
+ std::marker::Unpin,
|
|
{
|
|
fn converter(&mut self) -> QuestionsConverterAsync<&mut T>;
|
|
}
|
|
|
|
impl<T> QuestionsConverterAsyncForStream<T> for T
|
|
where
|
|
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
|
+ std::marker::Unpin,
|
|
{
|
|
fn converter(&mut self) -> QuestionsConverterAsync<&mut T> {
|
|
QuestionsConverterAsync::from(self)
|
|
}
|
|
}
|
|
|
|
impl<T> QuestionsConverterAsync<T>
|
|
where
|
|
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
|
+ std::marker::Unpin,
|
|
{
|
|
pub fn convert(self) -> impl Stream<Item = Question> {
|
|
self.inner
|
|
.filter_map(|(name, res)| async move {
|
|
if let Ok(item) = res {
|
|
Some((name, item))
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.flat_map(|(filename, batch)| {
|
|
stream::iter({
|
|
let mut batch = batch;
|
|
batch.filename = filename;
|
|
let questions: Vec<Question> = batch.into();
|
|
questions
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use crate::questions::test::convert_common::sample_batch;
|
|
|
|
use super::*;
|
|
use futures_util::{pin_mut, StreamExt};
|
|
use insta::assert_yaml_snapshot;
|
|
|
|
#[tokio::test]
|
|
async fn test_convert_stream() {
|
|
let source = futures::stream::once(async {
|
|
(
|
|
String::from("test.json"),
|
|
Ok::<SourceQuestionsBatch, serde_json::Error>(sample_batch()),
|
|
)
|
|
});
|
|
|
|
pin_mut!(source);
|
|
let converter = source.converter();
|
|
let converter = converter.convert();
|
|
let converted: Vec<_> = converter.collect().await;
|
|
assert_yaml_snapshot!(converted, @r#"
|
|
---
|
|
- id: Вопрос 1
|
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
|
answer: "42"
|
|
batch_info:
|
|
filename: test.json
|
|
description: Тестовый
|
|
date: 00-000-2000
|
|
- id: Вопрос 2
|
|
description: Зимой и летом одним цветом
|
|
answer: ёлка
|
|
batch_info:
|
|
filename: test.json
|
|
description: Тестовый
|
|
date: 00-000-2000
|
|
|
|
"#);
|
|
}
|
|
}
|
|
}
|
|
#[cfg(feature = "convert_async")]
|
|
pub use convert_async::{QuestionsConverterAsync, QuestionsConverterAsyncForStream};
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use insta::assert_yaml_snapshot;
|
|
use serde_json::json;
|
|
|
|
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
|
pub mod convert_common {
|
|
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
|
|
|
pub fn sample_batch() -> SourceQuestionsBatch {
|
|
SourceQuestionsBatch {
|
|
description: "Тестовый".into(),
|
|
date: "00-000-2000".into(),
|
|
questions: vec![
|
|
SourceQuestion {
|
|
id: "Вопрос 1".into(),
|
|
description: "Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2".into(),
|
|
answer: "42".into(),
|
|
..Default::default()
|
|
},
|
|
SourceQuestion {
|
|
id: "Вопрос 2".into(),
|
|
description: "Зимой и летом одним цветом".into(),
|
|
answer: "ёлка".into(),
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
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#"
|
|
---
|
|
id: Вопрос 1
|
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
|
answer: "42"
|
|
batch_info:
|
|
description: Тестовый
|
|
date: 00-000-2000
|
|
|
|
"#);
|
|
}
|
|
#[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#"
|
|
---
|
|
id: Вопрос 1
|
|
description: Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2
|
|
answer: "42"
|
|
batch_info:
|
|
description: Тестовый
|
|
date: 00-000-2000
|
|
|
|
"#);
|
|
}
|
|
}
|