1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs;
use std::path::Path;
/// Definition of a model field.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ModelField {
/// Field name (last part of binding path)
pub name: String,
/// Type hint for display purposes
#[serde(default)]
pub type_name: String,
/// If true, field has nested children
#[serde(default)]
pub is_nested: bool,
/// Nested field definitions for structs
#[serde(default)]
pub children: Vec<ModelField>,
}
/// Registry of model fields for validation.
#[derive(Debug, Clone, Default)]
pub struct ModelInfo {
fields: HashSet<ModelField>,
}
impl ModelInfo {
/// Creates a new empty model info.
pub fn new() -> Self {
Self {
fields: HashSet::new(),
}
}
/// Loads model info from a JSON file.
///
/// # Arguments
///
/// * `path` - Path to the JSON file containing model field definitions
///
/// # Returns
///
/// A `Result` containing the model info or an error if loading fails.
///
/// # Example JSON format
///
/// ```json
/// [
/// {
/// "name": "count",
/// "type_name": "i32",
/// "is_nested": false,
/// "children": []
/// },
/// {
/// "name": "user",
/// "type_name": "User",
/// "is_nested": true,
/// "children": [
/// {"name": "name", "type_name": "String", "is_nested": false, "children": []},
/// {"name": "email", "type_name": "String", "is_nested": false, "children": []}
/// ]
/// }
/// ]
/// ```
pub fn load_from_json(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
let content = fs::read_to_string(path)?;
let fields: Vec<ModelField> = serde_json::from_str(&content)?;
let mut model = Self::new();
for field in fields {
model.add_field(field);
}
Ok(model)
}
/// Checks if a field path exists in the model.
///
/// # Arguments
///
/// * `path` - Slice of field names representing the path (e.g., &["user", "name"])
///
/// # Returns
///
/// `true` if the field path exists, `false` otherwise.
pub fn contains_field(&self, path: &[&str]) -> bool {
if path.is_empty() {
return false;
}
// Find the top-level field
let top_level_name = path[0];
let top_level_field = self.fields.iter().find(|f| f.name == top_level_name);
match top_level_field {
None => false,
Some(field) => {
if path.len() == 1 {
// Just checking the top-level field
true
} else {
// Need to check nested fields
Self::contains_nested_field(field, &path[1..])
}
}
}
}
/// Helper function to check nested fields recursively.
fn contains_nested_field(field: &ModelField, path: &[&str]) -> bool {
if path.is_empty() {
return true;
}
// If field is not nested, it can't have children
if !field.is_nested {
return false;
}
// Find the next field in the path
let next_name = path[0];
let next_field = field.children.iter().find(|f| f.name == next_name);
match next_field {
None => false,
Some(child) => {
if path.len() == 1 {
true
} else {
Self::contains_nested_field(child, &path[1..])
}
}
}
}
/// Gets all top-level field names.
///
/// # Returns
///
/// A vector of all top-level field names.
pub fn top_level_fields(&self) -> Vec<&str> {
self.fields.iter().map(|f| f.name.as_str()).collect()
}
/// Gets all available field paths as strings.
///
/// # Returns
///
/// A vector of all field paths (e.g., "count", "user.name").
pub fn all_field_paths(&self) -> Vec<String> {
let mut paths = Vec::new();
for field in &self.fields {
paths.push(field.name.clone());
Self::collect_nested_paths(field, &field.name, &mut paths);
}
paths
}
/// Helper function to collect nested field paths recursively.
fn collect_nested_paths(field: &ModelField, prefix: &str, paths: &mut Vec<String>) {
if field.is_nested {
for child in &field.children {
let path = format!("{}.{}", prefix, child.name);
paths.push(path.clone());
Self::collect_nested_paths(child, &path, paths);
}
}
}
/// Adds a field to the model.
pub fn add_field(&mut self, field: ModelField) {
self.fields.insert(field);
}
/// Gets the number of top-level fields.
pub fn len(&self) -> usize {
self.fields.len()
}
/// Checks if the model is empty.
pub fn is_empty(&self) -> bool {
self.fields.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_model() {
let model = ModelInfo::new();
assert!(model.is_empty());
assert_eq!(model.len(), 0);
assert!(!model.contains_field(&["count"]));
}
#[test]
fn test_add_simple_field() {
let mut model = ModelInfo::new();
let field = ModelField {
name: "count".to_string(),
type_name: "i32".to_string(),
is_nested: false,
children: vec![],
};
model.add_field(field);
assert!(!model.is_empty());
assert_eq!(model.len(), 1);
assert!(model.contains_field(&["count"]));
assert!(!model.contains_field(&["unknown"]));
}
#[test]
fn test_nested_field() {
let mut model = ModelInfo::new();
let field = ModelField {
name: "user".to_string(),
type_name: "User".to_string(),
is_nested: true,
children: vec![ModelField {
name: "name".to_string(),
type_name: "String".to_string(),
is_nested: false,
children: vec![],
}],
};
model.add_field(field);
assert!(model.contains_field(&["user"]));
assert!(model.contains_field(&["user", "name"]));
assert!(!model.contains_field(&["user", "email"]));
}
#[test]
fn test_top_level_fields() {
let mut model = ModelInfo::new();
model.add_field(ModelField {
name: "count".to_string(),
type_name: "i32".to_string(),
is_nested: false,
children: vec![],
});
model.add_field(ModelField {
name: "enabled".to_string(),
type_name: "bool".to_string(),
is_nested: false,
children: vec![],
});
let fields = model.top_level_fields();
assert_eq!(fields.len(), 2);
assert!(fields.contains(&"count"));
assert!(fields.contains(&"enabled"));
}
#[test]
fn test_all_field_paths() {
let mut model = ModelInfo::new();
model.add_field(ModelField {
name: "count".to_string(),
type_name: "i32".to_string(),
is_nested: false,
children: vec![],
});
model.add_field(ModelField {
name: "user".to_string(),
type_name: "User".to_string(),
is_nested: true,
children: vec![ModelField {
name: "name".to_string(),
type_name: "String".to_string(),
is_nested: false,
children: vec![],
}],
});
let paths = model.all_field_paths();
assert!(paths.contains(&"count".to_string()));
assert!(paths.contains(&"user".to_string()));
assert!(paths.contains(&"user.name".to_string()));
}
}