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