use std::collections::HashMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::question::Question;
use crate::state::State;
pub type WireHashState = foldhash::fast::RandomState;
pub const API_VERSION: &str = "jev-compatible-0.1";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct SystemRequest {
pub state: State,
pub model: String,
#[serde(deserialize_with = "deserialize_questions")]
pub questions: HashMap<String, Question, WireHashState>,
}
fn deserialize_questions<'de, D>(
deserializer: D,
) -> Result<HashMap<String, Question, WireHashState>, D::Error>
where
D: serde::Deserializer<'de>,
{
struct QuestionsVisitor;
impl<'de> serde::de::Visitor<'de> for QuestionsVisitor {
type Value = HashMap<String, Question, WireHashState>;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a map of questions")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut questions = HashMap::with_capacity_and_hasher(8, Default::default());
while let Some(id) = map.next_key::<String>()? {
questions.insert(id, map.next_value()?);
if questions.len() == 12 {
questions.reserve(20);
}
}
Ok(questions)
}
}
deserializer.deserialize_map(QuestionsVisitor)
}