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