dixscript 1.0.0

Config, code, and encryption in one file — a data interchange format with compile-time functions, AES-256/ChaCha20 built-in, and cross-platform FFI
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Centralized path builder for DATA section variable paths
//!
//! Ensures consistent path construction across parsers, analyzers, and runtime
//!
//! ## Rules:
//! - All paths start with "DATA"
//! - Properties use dot notation: DATA.property
//! - Table paths use dots: DATA.table.subtable.property
//! - Array indices have NO dot prefix: DATA.array[0] NOT DATA.array.[0]
//! - Array item properties: DATA.array[0].property
//!
//! ## Examples:
//!
//! DATA.simple_host
//! DATA.server.config.host
//! DATA.inventory.items[0]
//! DATA.inventory.items[0].name
//! 

const ROOT: &str = "DATA";

/// PathBuilder - static utility for building DATA section paths
pub struct PathBuilder;

impl PathBuilder {
    // ==================== BASIC PATH BUILDING ====================
    
    /// Build a DATA path from segments
    /// Automatically handles array notation (segments starting with '[')
    ///
    /// # Examples
    /// 
    /// let path = PathBuilder::build(&["server", "host"]);
    /// // Returns: "DATA.server.host"
    ///
    /// let path = PathBuilder::build(&["items", "[0]", "name"]);
    /// // Returns: "DATA.items[0].name"
    /// 
    pub fn build(segments: &[&str]) -> String {
        if segments.is_empty() {
            return ROOT.to_string();
        }
        
        let mut result = String::from(ROOT);
        
        for segment in segments {
            if segment.is_empty() {
                continue;
            }
            
            // Array index - no dot prefix
            if segment.starts_with('[') && segment.ends_with(']') {
                result.push_str(segment);
            } else {
                // Regular property - add dot
                result.push('.');
                result.push_str(segment);
            }
        }
        
        result
    }
    
    /// Build path from a base path and additional segments
    ///
    /// # Examples
    /// 
    /// let path = PathBuilder::build_from("DATA.server", &["config", "host"]);
    /// // Returns: "DATA.server.config.host"
    /// 
    pub fn build_from(base_path: &str, segments: &[&str]) -> String {
        if base_path.is_empty() {
            return Self::build(segments);
        }
        
        if segments.is_empty() {
            return base_path.to_string();
        }
        
        let mut result = String::from(base_path);
        
        for segment in segments {
            if segment.is_empty() {
                continue;
            }
            
            if segment.starts_with('[') && segment.ends_with(']') {
                result.push_str(segment);
            } else {
                result.push('.');
                result.push_str(segment);
            }
        }
        
        result
    }
    
    /// Build path from current scope tracker state
    ///
    /// # Examples
    /// 
    /// let path = PathBuilder::build_from_scope("DATA", "server");
    /// // Returns: "DATA.server"
    ///
    /// let path = PathBuilder::build_from_scope("DATA.server", "host");
    /// // Returns: "DATA.server.host"
    /// 
    pub fn build_from_scope(current_path: &str, property: &str) -> String {
        if current_path == ROOT {
            Self::build(&[property])
        } else {
            Self::build_from(current_path, &[property])
        }
    }
    
    // ==================== ARRAY PATH BUILDING ====================
    
    /// Build array item path
    ///
    /// # Examples
    /// 
    /// let path = PathBuilder::build_array_item("DATA.inventory.items", 0);
    /// // Returns: "DATA.inventory.items[0]"
    /// 
    pub fn build_array_item(array_path: &str, index: usize) -> String {
        format!("{}[{}]", array_path, index)
    }
    
    /// Build array item property path
    ///
    /// # Examples
    /// 
    /// let path = PathBuilder::build_array_item_property("DATA.inventory.items", 0, "name");
    /// // Returns: "DATA.inventory.items[0].name"
    /// 
    pub fn build_array_item_property(array_path: &str, index: usize, property: &str) -> String {
        Self::build_from(&Self::build_array_item(array_path, index), &[property])
    }
    
    // ==================== PATH MANIPULATION ====================
    
    /// Remove "DATA." prefix if present
    ///
    /// # Examples
    /// 
    /// let stripped = PathBuilder::strip_root("DATA.server.host");
    /// // Returns: "server.host"
    ///
    /// let stripped = PathBuilder::strip_root("DATA");
    /// // Returns: ""
    /// 
    pub fn strip_root(path: &str) -> String {
        if path.starts_with(ROOT) {
            if path.len() == ROOT.len() {
                String::new()
            } else if path.chars().nth(ROOT.len()) == Some('.') {
                path[ROOT.len() + 1..].to_string()
            } else {
                path.to_string()
            }
        } else {
            path.to_string()
        }
    }
    
    /// Ensure path has "DATA." prefix
    ///
    /// # Examples
    /// 
    /// let path = PathBuilder::ensure_root("server.host");
    /// // Returns: "DATA.server.host"
    ///
    /// let path = PathBuilder::ensure_root("DATA.server.host");
    /// // Returns: "DATA.server.host" (unchanged)
    /// 
    pub fn ensure_root(path: &str) -> String {
        if path.is_empty() {
            ROOT.to_string()
        } else if path.starts_with(ROOT) {
            path.to_string()
        } else {
            Self::build(&[path])
        }
    }
    
    /// Get the last segment of a path
    ///
    /// # Examples
    /// 
    /// let segment = PathBuilder::get_last_segment("DATA.server.config.host");
    /// // Returns: "host"
    ///
    /// let segment = PathBuilder::get_last_segment("DATA.items[0]");
    /// // Returns: "[0]"
    /// 
    pub fn get_last_segment(path: &str) -> String {
        if path.is_empty() {
            return String::new();
        }
        
        // Handle array notation: DATA.items[0] → "[0]"
        if let Some(last_bracket) = path.rfind('[') {
            if path.ends_with(']') {
                return path[last_bracket..].to_string();
            }
        }
        
        // Handle dot notation
        if let Some(last_dot) = path.rfind('.') {
            path[last_dot + 1..].to_string()
        } else {
            path.to_string()
        }
    }
    
    /// Get parent path by removing last segment
    ///
    /// # Examples
    /// 
    /// let parent = PathBuilder::get_parent("DATA.server.config.host");
    /// // Returns: "DATA.server.config"
    ///
    /// let parent = PathBuilder::get_parent("DATA.items[0].name");
    /// // Returns: "DATA.items[0]"
    ///
    /// let parent = PathBuilder::get_parent("DATA");
    /// // Returns: "DATA"
    /// 
    pub fn get_parent(path: &str) -> String {
        if path.is_empty() || path == ROOT {
            return ROOT.to_string();
        }
        
        // Handle array notation: DATA.items[0].name → DATA.items[0]
        if let Some(last_dot) = path.rfind('.') {
            if last_dot <= ROOT.len() {
                ROOT.to_string()
            } else {
                path[..last_dot].to_string()
            }
        } else {
            ROOT.to_string()
        }
    }
    
    // ==================== PATH PARSING ====================
    
    /// Split path into segments (excluding ROOT)
    ///
    /// # Examples
    /// 
    /// let segments = PathBuilder::get_segments("DATA.server.config.host");
    /// // Returns: ["server", "config", "host"]
    ///
    /// let segments = PathBuilder::get_segments("DATA.items[0].name");
    /// // Returns: ["items", "[0]", "name"]
    /// 
    pub fn get_segments(path: &str) -> Vec<String> {
        let stripped = Self::strip_root(path);
        
        if stripped.is_empty() {
            return Vec::new();
        }
        
        let mut segments = Vec::new();
        let mut current = String::new();
        let chars: Vec<char> = stripped.chars().collect();
        let mut i = 0;
        
        while i < chars.len() {
            let c = chars[i];
            
            if c == '.' {
                if !current.is_empty() {
                    segments.push(current.clone());
                    current.clear();
                }
            } else if c == '[' {
                // Add accumulated segment before array index
                if !current.is_empty() {
                    segments.push(current.clone());
                    current.clear();
                }
                
                // Find matching closing bracket
                let mut bracket_content = String::from("[");
                i += 1;
                while i < chars.len() && chars[i] != ']' {
                    bracket_content.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    bracket_content.push(']');
                }
                segments.push(bracket_content);
            } else {
                current.push(c);
            }
            
            i += 1;
        }
        
        if !current.is_empty() {
            segments.push(current);
        }
        
        segments
    }
    
    // ==================== PATH QUERIES ====================
    
    /// Check if path represents an array index
    ///
    /// # Examples
    /// 
    /// assert!(PathBuilder::is_array_index("DATA.items[0]"));
    /// assert!(!PathBuilder::is_array_index("DATA.items"));
    /// 
    pub fn is_array_index(path: &str) -> bool {
        path.contains('[') && path.ends_with(']')
    }
    
    /// Extract table path from property path
    ///
    /// # Examples
    /// 
    /// let table = PathBuilder::get_table_path("DATA.server.config.host");
    /// // Returns: "DATA.server.config"
    ///
    /// let table = PathBuilder::get_table_path("DATA.simple");
    /// // Returns: "DATA"
    /// 
    pub fn get_table_path(property_path: &str) -> String {
        let segments = Self::get_segments(property_path);
        
        if segments.len() <= 1 {
            return ROOT.to_string();
        }
        
        // Remove last segment (property name)
        let table_segments: Vec<&str> = segments[..segments.len() - 1]
            .iter()
            .map(|s| s.as_str())
            .collect();
        
        Self::build(&table_segments)
    }
}

// ==================== TESTS ====================

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_build_simple() {
        assert_eq!(PathBuilder::build(&["server", "host"]), "DATA.server.host");
    }
    
    #[test]
    fn test_build_with_array() {
        assert_eq!(PathBuilder::build(&["items", "[0]"]), "DATA.items[0]");
        assert_eq!(PathBuilder::build(&["items", "[0]", "name"]), "DATA.items[0].name");
    }
    
    #[test]
    fn test_build_from() {
        assert_eq!(
            PathBuilder::build_from("DATA.server", &["config", "host"]),
            "DATA.server.config.host"
        );
    }
    
    #[test]
    fn test_strip_root() {
        assert_eq!(PathBuilder::strip_root("DATA.server.host"), "server.host");
        assert_eq!(PathBuilder::strip_root("DATA"), "");
    }
    
    #[test]
    fn test_ensure_root() {
        assert_eq!(PathBuilder::ensure_root("server.host"), "DATA.server.host");
        assert_eq!(PathBuilder::ensure_root("DATA.server.host"), "DATA.server.host");
    }
    
    #[test]
    fn test_get_last_segment() {
        assert_eq!(PathBuilder::get_last_segment("DATA.server.config.host"), "host");
        assert_eq!(PathBuilder::get_last_segment("DATA.items[0]"), "[0]");
    }
    
    #[test]
    fn test_get_parent() {
        assert_eq!(PathBuilder::get_parent("DATA.server.config.host"), "DATA.server.config");
        assert_eq!(PathBuilder::get_parent("DATA.items[0].name"), "DATA.items[0]");
        assert_eq!(PathBuilder::get_parent("DATA"), "DATA");
    }
    
    #[test]
    fn test_get_segments() {
        assert_eq!(
            PathBuilder::get_segments("DATA.server.config.host"),
            vec!["server", "config", "host"]
        );
        assert_eq!(
            PathBuilder::get_segments("DATA.items[0].name"),
            vec!["items", "[0]", "name"]
        );
    }
    
    #[test]
    fn test_is_array_index() {
        assert!(PathBuilder::is_array_index("DATA.items[0]"));
        assert!(!PathBuilder::is_array_index("DATA.items"));
    }
    
    #[test]
    fn test_get_table_path() {
        assert_eq!(
            PathBuilder::get_table_path("DATA.server.config.host"),
            "DATA.server.config"
        );
        assert_eq!(
            PathBuilder::get_table_path("DATA.simple"),
            "DATA"
        );
    }
  }