Skip to main content

apif_execution/
response_handler.rs

1use apif_assert::{AssertionResult, JsonComparator};
2use apif_ast::{InlineOptions, Section, SectionContent};
3use serde_json::Value;
4
5pub struct ResponseHandler {
6    no_assert: bool,
7}
8
9impl ResponseHandler {
10    pub fn new(no_assert: bool) -> Self {
11        Self { no_assert }
12    }
13
14    pub fn validate_message(
15        &self,
16        actual: &Value,
17        expected: &Value,
18        options: &InlineOptions,
19    ) -> Vec<String> {
20        if self.no_assert {
21            return vec![];
22        }
23        let diffs = JsonComparator::compare(actual, expected, options);
24        diffs
25            .iter()
26            .filter_map(|d| match d {
27                AssertionResult::Fail { message, .. } => Some(message.clone()),
28                AssertionResult::Error(m) => Some(m.clone()),
29                _ => None,
30            })
31            .collect()
32    }
33
34    pub fn expected_values_for_section(section: &Section) -> Vec<Value> {
35        match &section.content {
36            SectionContent::Json(v) => vec![v.clone()],
37            SectionContent::JsonLines(vals) => vals.clone(),
38            _ => vec![],
39        }
40    }
41}