Skip to main content

qa_spec/
examples.rs

1use serde_json::{Map, Number, Value};
2
3use crate::spec::form::FormSpec;
4use crate::spec::question::{QuestionSpec, QuestionType};
5use crate::visibility::VisibilityMap;
6
7pub fn generate(spec: &FormSpec, visibility: &VisibilityMap) -> Value {
8    let mut output = Map::new();
9
10    for question in &spec.questions {
11        if !visibility.get(&question.id).copied().unwrap_or(true) {
12            continue;
13        }
14        output.insert(question.id.clone(), example_for(question));
15    }
16
17    Value::Object(output)
18}
19
20fn example_for(question: &QuestionSpec) -> Value {
21    if let Some(default_value) = &question.default_value {
22        return Value::String(default_value.clone());
23    }
24
25    match question.kind {
26        QuestionType::String | QuestionType::Enum => {
27            Value::String(format!("example-{}", question.id))
28        }
29        QuestionType::Boolean => Value::Bool(false),
30        QuestionType::Integer => Value::Number(Number::from(1)),
31        QuestionType::Number => {
32            Value::Number(Number::from_f64(1.0).unwrap_or_else(|| Number::from(1)))
33        }
34        QuestionType::List => Value::Array(Vec::new()),
35    }
36}