1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::Error;
7
8const DEFAULT_MODEL: &str = "jev-latest";
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12pub struct Request {
13 pub state: Value,
17 pub model: String,
19 pub questions: BTreeMap<String, Question>,
24}
25
26impl Request {
27 pub fn new(state: impl Into<Value>) -> Self {
29 Self {
30 state: state.into(),
31 model: DEFAULT_MODEL.to_owned(),
32 questions: BTreeMap::new(),
33 }
34 }
35
36 #[must_use]
38 pub fn with_model(mut self, model: impl Into<String>) -> Self {
39 self.model = model.into();
40 self
41 }
42
43 #[must_use]
47 pub fn with_question(mut self, id: impl Into<String>, question: Question) -> Self {
48 self.questions.insert(id.into(), question);
49 self
50 }
51
52 pub fn insert_question(
54 &mut self,
55 id: impl Into<String>,
56 question: Question,
57 ) -> Option<Question> {
58 self.questions.insert(id.into(), question)
59 }
60
61 pub fn validate(&self) -> Result<(), Error> {
66 if !is_state(&self.state) {
67 return Err(Error::Validation {
68 field: "state".to_owned(),
69 message: "must be a string, object, or array".to_owned(),
70 });
71 }
72
73 for (id, question) in &self.questions {
74 question.validate(id)?;
75 }
76 Ok(())
77 }
78}
79
80impl Default for Request {
81 fn default() -> Self {
82 Self::new(Value::String(String::new()))
83 }
84}
85
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct NoulCriteria {
91 #[serde(default, rename = "true")]
93 pub when_true: Value,
94 #[serde(default, rename = "false")]
96 pub when_false: Value,
97}
98
99impl NoulCriteria {
100 pub fn new(when_true: impl Into<Value>, when_false: impl Into<Value>) -> Self {
102 Self {
103 when_true: when_true.into(),
104 when_false: when_false.into(),
105 }
106 }
107}
108
109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
113#[serde(tag = "type", rename_all = "lowercase")]
114pub enum Question {
115 Noul {
117 instructions: Value,
119 #[serde(skip_serializing_if = "Option::is_none")]
121 criteria: Option<NoulCriteria>,
122 },
123 Choice {
125 instructions: Value,
127 criteria: BTreeMap<String, Value>,
129 },
130 Score {
132 instructions: Value,
134 criteria: Vec<Value>,
136 },
137}
138
139impl Question {
140 pub fn noul(instructions: impl Into<Value>) -> Self {
142 Self::Noul {
143 instructions: instructions.into(),
144 criteria: None,
145 }
146 }
147
148 pub fn noul_with_criteria(instructions: impl Into<Value>, criteria: NoulCriteria) -> Self {
150 Self::Noul {
151 instructions: instructions.into(),
152 criteria: Some(criteria),
153 }
154 }
155
156 pub fn choice(instructions: impl Into<Value>, criteria: BTreeMap<String, Value>) -> Self {
160 Self::Choice {
161 instructions: instructions.into(),
162 criteria,
163 }
164 }
165
166 pub fn score(instructions: impl Into<Value>, criteria: Vec<Value>) -> Self {
170 Self::Score {
171 instructions: instructions.into(),
172 criteria,
173 }
174 }
175
176 fn validate(&self, id: &str) -> Result<(), Error> {
177 let instructions = match self {
178 Self::Noul { instructions, .. }
179 | Self::Choice { instructions, .. }
180 | Self::Score { instructions, .. } => instructions,
181 };
182 validate_entry(instructions, format!("questions.{id}.instructions"))?;
183
184 match self {
185 Self::Noul {
186 criteria: Some(criteria),
187 ..
188 } => {
189 validate_entry(&criteria.when_true, format!("questions.{id}.criteria.true"))?;
190 validate_entry(
191 &criteria.when_false,
192 format!("questions.{id}.criteria.false"),
193 )
194 }
195 Self::Noul { criteria: None, .. } => Ok(()),
196 Self::Choice { criteria, .. } => {
197 if criteria.is_empty() {
198 return Err(Error::Validation {
199 field: format!("questions.{id}.criteria"),
200 message: "must contain at least one option".to_owned(),
201 });
202 }
203 for (option, description) in criteria {
204 validate_entry(description, format!("questions.{id}.criteria.{option}"))?;
205 }
206 Ok(())
207 }
208 Self::Score { criteria, .. } => {
209 if criteria.len() < 2 {
210 return Err(Error::Validation {
211 field: format!("questions.{id}.criteria"),
212 message: "must contain at least two levels".to_owned(),
213 });
214 }
215 for (level, description) in criteria.iter().enumerate() {
216 validate_entry(description, format!("questions.{id}.criteria[{level}]"))?;
217 }
218 Ok(())
219 }
220 }
221 }
222}
223
224fn is_state(value: &Value) -> bool {
225 matches!(value, Value::String(_) | Value::Object(_) | Value::Array(_))
226}
227
228fn validate_entry(value: &Value, field: String) -> Result<(), Error> {
229 if matches!(
230 value,
231 Value::String(_) | Value::Object(_) | Value::Array(_) | Value::Null
232 ) {
233 Ok(())
234 } else {
235 Err(Error::Validation {
236 field,
237 message: "must be a string, object, array, or null".to_owned(),
238 })
239 }
240}