json_eval_rs/
subform_methods.rs

1// Subform methods for isolated array field evaluation
2
3use crate::JSONEval;
4use serde_json::Value;
5
6impl JSONEval {
7    /// Evaluate a subform with data
8    /// Evaluate a subform with data and optional selective paths
9    pub fn evaluate_subform(
10        &mut self,
11        subform_path: &str,
12        data: &str,
13        context: Option<&str>,
14        paths: Option<&[String]>,
15    ) -> Result<(), String> {
16        let subform = self
17            .subforms
18            .get_mut(subform_path)
19            .ok_or_else(|| format!("Subform not found: {}", subform_path))?;
20
21        subform.evaluate(data, context, paths)
22    }
23
24    /// Validate subform data against its schema rules
25    pub fn validate_subform(
26        &mut self,
27        subform_path: &str,
28        data: &str,
29        context: Option<&str>,
30        paths: Option<&[String]>,
31    ) -> Result<crate::ValidationResult, String> {
32        let subform = self
33            .subforms
34            .get_mut(subform_path)
35            .ok_or_else(|| format!("Subform not found: {}", subform_path))?;
36
37        subform.validate(data, context, paths)
38    }
39
40    /// Evaluate dependents in subform when a field changes
41    pub fn evaluate_dependents_subform(
42        &mut self,
43        subform_path: &str,
44        changed_paths: &[String],
45        data: Option<&str>,
46        context: Option<&str>,
47        re_evaluate: bool,
48    ) -> Result<Value, String> {
49        let subform = self
50            .subforms
51            .get_mut(subform_path)
52            .ok_or_else(|| format!("Subform not found: {}", subform_path))?;
53
54        subform.evaluate_dependents(changed_paths, data, context, re_evaluate)
55    }
56
57    /// Resolve layout for subform
58    pub fn resolve_layout_subform(
59        &mut self,
60        subform_path: &str,
61        evaluate: bool,
62    ) -> Result<(), String> {
63        let subform = self
64            .subforms
65            .get_mut(subform_path)
66            .ok_or_else(|| format!("Subform not found: {}", subform_path))?;
67
68        subform.resolve_layout(evaluate)
69    }
70
71    /// Get evaluated schema from subform
72    pub fn get_evaluated_schema_subform(
73        &mut self,
74        subform_path: &str,
75        resolve_layout: bool,
76    ) -> Value {
77        if let Some(subform) = self.subforms.get_mut(subform_path) {
78            subform.get_evaluated_schema(resolve_layout)
79        } else {
80            Value::Null
81        }
82    }
83
84    /// Get schema value from subform (all .value fields)
85    pub fn get_schema_value_subform(&mut self, subform_path: &str) -> Value {
86        if let Some(subform) = self.subforms.get_mut(subform_path) {
87            subform.get_schema_value()
88        } else {
89            Value::Null
90        }
91    }
92
93    /// Get evaluated schema without $params from subform
94    pub fn get_evaluated_schema_without_params_subform(
95        &mut self,
96        subform_path: &str,
97        resolve_layout: bool,
98    ) -> Value {
99        if let Some(subform) = self.subforms.get_mut(subform_path) {
100            subform.get_evaluated_schema_without_params(resolve_layout)
101        } else {
102            Value::Null
103        }
104    }
105
106    /// Get evaluated schema by specific path from subform
107    pub fn get_evaluated_schema_by_path_subform(
108        &mut self,
109        subform_path: &str,
110        schema_path: &str,
111        skip_layout: bool,
112    ) -> Option<Value> {
113        if let Some(subform) = self.subforms.get_mut(subform_path) {
114            subform.get_evaluated_schema_by_path(schema_path, skip_layout)
115        } else {
116            None
117        }
118    }
119
120    /// Get evaluated schema by multiple paths from subform
121    pub fn get_evaluated_schema_by_paths_subform(
122        &mut self,
123        subform_path: &str,
124        schema_paths: &[String],
125        skip_layout: bool,
126        format: Option<crate::ReturnFormat>,
127    ) -> Value {
128        if let Some(subform) = self.subforms.get_mut(subform_path) {
129            subform.get_evaluated_schema_by_paths(schema_paths, skip_layout, format)
130        } else {
131            match format.unwrap_or_default() {
132                crate::ReturnFormat::Array => Value::Array(vec![]),
133                _ => Value::Object(serde_json::Map::new()),
134            }
135        }
136    }
137
138    /// Get schema by specific path from subform
139    pub fn get_schema_by_path_subform(
140        &self,
141        subform_path: &str,
142        schema_path: &str,
143    ) -> Option<Value> {
144        if let Some(subform) = self.subforms.get(subform_path) {
145            subform.get_schema_by_path(schema_path)
146        } else {
147            None
148        }
149    }
150
151    /// Get schema by multiple paths from subform
152    pub fn get_schema_by_paths_subform(
153        &self,
154        subform_path: &str,
155        schema_paths: &[String],
156        format: Option<crate::ReturnFormat>,
157    ) -> Value {
158        if let Some(subform) = self.subforms.get(subform_path) {
159            subform.get_schema_by_paths(schema_paths, format)
160        } else {
161            match format.unwrap_or_default() {
162                crate::ReturnFormat::Array => Value::Array(vec![]),
163                _ => Value::Object(serde_json::Map::new()),
164            }
165        }
166    }
167
168    /// Get list of available subform paths
169    pub fn get_subform_paths(&self) -> Vec<String> {
170        self.subforms.keys().cloned().collect()
171    }
172
173    /// Check if a subform exists at the given path
174    pub fn has_subform(&self, subform_path: &str) -> bool {
175        self.subforms.contains_key(subform_path)
176    }
177}