hedl-core 2.0.0

Core parser and data model for HEDL (Hierarchical Entity Data Language)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Configuration for HEDL lexical analysis with resource limits.
//!
//! This module provides configuration options to prevent resource exhaustion
//! attacks and ensure bounded parsing behavior. Limits are fully configurable
//! and default to high values suitable for trusted input processing. For
//! untrusted input, use `LexConfig::strict()` or customize limits as needed.

/// Configuration for HEDL lexical analysis with resource limits.
///
/// These limits prevent resource exhaustion from malicious or malformed input.
/// Defaults are set high (100MB, 10K recursion, 10M fields) for trusted data
/// processing. For untrusted input, use `LexConfig::strict()`.
///
/// # Examples
///
/// ```
/// use hedl_core::lex::LexConfig;
///
/// // Use default limits (high values for trusted input)
/// let config = LexConfig::default();
/// assert_eq!(config.max_string_length(), 100 * 1024 * 1024); // 100 MB
///
/// // Use strict limits for untrusted input
/// let strict = LexConfig::strict();
/// assert_eq!(strict.max_string_length(), 64 * 1024); // 64 KB
///
/// // Customize limits for specific requirements
/// let custom = LexConfig::new()
///     .with_max_string_length(500 * 1024 * 1024) // 500 MB
///     .with_max_recursion_depth(50_000);          // 50K levels
///
/// // For maximum throughput with trusted data, use very high limits
/// let unlimited = LexConfig::new()
///     .with_max_string_length(usize::MAX)
///     .with_max_recursion_depth(usize::MAX)
///     .with_max_field_count(usize::MAX);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LexConfig {
    /// Maximum length of a single string literal in bytes.
    ///
    /// Prevents memory exhaustion from extremely long strings.
    /// Default: 100 MB (104,857,600 bytes).
    max_string_length: usize,

    /// Maximum recursion depth for nested expressions.
    ///
    /// Prevents stack overflow from deeply nested `$(...)` expressions.
    /// Default: 10,000 levels.
    max_recursion_depth: usize,

    /// Maximum number of fields in a CSV row or struct.
    ///
    /// Prevents memory exhaustion from rows with excessive fields.
    /// Default: 10,000,000 fields.
    max_field_count: usize,

    /// Maximum nesting depth for parentheses in expressions.
    ///
    /// Prevents stack overflow from expressions like `$(((((...)))))`
    /// Default: 1,000 levels.
    max_paren_depth: usize,
}

impl LexConfig {
    /// Default maximum string length (100 MB).
    ///
    /// This high default supports large data processing while still preventing
    /// unbounded memory consumption. For untrusted input, use `LexConfig::strict()`.
    pub const DEFAULT_MAX_STRING_LENGTH: usize = 100 * 1024 * 1024;

    /// Default maximum recursion depth (10,000 levels).
    ///
    /// This high default supports deeply nested structures while preventing stack overflow.
    /// For untrusted input, use `LexConfig::strict()`.
    pub const DEFAULT_MAX_RECURSION_DEPTH: usize = 10_000;

    /// Default maximum field count (10 million fields).
    ///
    /// This high default supports large datasets while preventing unbounded memory.
    /// For untrusted input, use `LexConfig::strict()`.
    pub const DEFAULT_MAX_FIELD_COUNT: usize = 10_000_000;

    /// Default maximum parenthesis depth (1,000 levels).
    ///
    /// This high default supports complex expressions while preventing stack overflow.
    /// For untrusted input, use `LexConfig::strict()`.
    pub const DEFAULT_MAX_PAREN_DEPTH: usize = 1_000;

    /// Create a new configuration with default limits.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the maximum string length in bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::lex::LexConfig;
    ///
    /// let config = LexConfig::new().with_max_string_length(10_000);
    /// assert_eq!(config.max_string_length(), 10_000);
    /// ```
    #[inline]
    pub fn with_max_string_length(mut self, max: usize) -> Self {
        self.max_string_length = max;
        self
    }

    /// Set the maximum recursion depth.
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::lex::LexConfig;
    ///
    /// let config = LexConfig::new().with_max_recursion_depth(32);
    /// assert_eq!(config.max_recursion_depth(), 32);
    /// ```
    #[inline]
    pub fn with_max_recursion_depth(mut self, max: usize) -> Self {
        self.max_recursion_depth = max;
        self
    }

    /// Set the maximum field count.
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::lex::LexConfig;
    ///
    /// let config = LexConfig::new().with_max_field_count(1000);
    /// assert_eq!(config.max_field_count(), 1000);
    /// ```
    #[inline]
    pub fn with_max_field_count(mut self, max: usize) -> Self {
        self.max_field_count = max;
        self
    }

    /// Set the maximum parenthesis depth.
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::lex::LexConfig;
    ///
    /// let config = LexConfig::new().with_max_paren_depth(32);
    /// assert_eq!(config.max_paren_depth(), 32);
    /// ```
    #[inline]
    pub fn with_max_paren_depth(mut self, max: usize) -> Self {
        self.max_paren_depth = max;
        self
    }

    /// Get the maximum string length in bytes.
    #[inline]
    pub fn max_string_length(&self) -> usize {
        self.max_string_length
    }

    /// Get the maximum recursion depth.
    #[inline]
    pub fn max_recursion_depth(&self) -> usize {
        self.max_recursion_depth
    }

    /// Get the maximum field count.
    #[inline]
    pub fn max_field_count(&self) -> usize {
        self.max_field_count
    }

    /// Get the maximum parenthesis depth.
    #[inline]
    pub fn max_paren_depth(&self) -> usize {
        self.max_paren_depth
    }

    /// Create a configuration with strict (small) limits for untrusted input.
    ///
    /// Useful for parsing untrusted data where security is paramount.
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::lex::LexConfig;
    ///
    /// let config = LexConfig::strict();
    /// assert_eq!(config.max_string_length(), 65_536); // 64 KB
    /// assert_eq!(config.max_recursion_depth(), 32);
    /// ```
    #[inline]
    pub fn strict() -> Self {
        Self {
            max_string_length: 64 * 1024, // 64 KB
            max_recursion_depth: 32,      // 32 levels
            max_field_count: 1_000,       // 1,000 fields
            max_paren_depth: 16,          // 16 levels
        }
    }

    /// Create a configuration with permissive (large) limits for trusted input.
    ///
    /// Useful for parsing trusted data where performance matters more than security.
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::lex::LexConfig;
    ///
    /// let config = LexConfig::permissive();
    /// assert_eq!(config.max_string_length(), 104_857_600); // 100 MB
    /// ```
    #[inline]
    pub fn permissive() -> Self {
        Self {
            max_string_length: 100 * 1024 * 1024, // 100 MB
            max_recursion_depth: 1_000,           // 1,000 levels
            max_field_count: 1_000_000,           // 1 million fields
            max_paren_depth: 256,                 // 256 levels
        }
    }

    /// Check if a string length is within limits.
    #[inline]
    pub fn check_string_length(&self, length: usize) -> bool {
        length <= self.max_string_length
    }

    /// Check if a recursion depth is within limits.
    #[inline]
    pub fn check_recursion_depth(&self, depth: usize) -> bool {
        depth <= self.max_recursion_depth
    }

    /// Check if a field count is within limits.
    #[inline]
    pub fn check_field_count(&self, count: usize) -> bool {
        count <= self.max_field_count
    }

    /// Check if a parenthesis depth is within limits.
    #[inline]
    pub fn check_paren_depth(&self, depth: usize) -> bool {
        depth <= self.max_paren_depth
    }
}

impl Default for LexConfig {
    fn default() -> Self {
        Self {
            max_string_length: Self::DEFAULT_MAX_STRING_LENGTH,
            max_recursion_depth: Self::DEFAULT_MAX_RECURSION_DEPTH,
            max_field_count: Self::DEFAULT_MAX_FIELD_COUNT,
            max_paren_depth: Self::DEFAULT_MAX_PAREN_DEPTH,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ==================== Construction tests ====================

    #[test]
    fn test_config_new() {
        let config = LexConfig::new();
        assert_eq!(
            config.max_string_length(),
            LexConfig::DEFAULT_MAX_STRING_LENGTH
        );
        assert_eq!(
            config.max_recursion_depth(),
            LexConfig::DEFAULT_MAX_RECURSION_DEPTH
        );
        assert_eq!(config.max_field_count(), LexConfig::DEFAULT_MAX_FIELD_COUNT);
        assert_eq!(config.max_paren_depth(), LexConfig::DEFAULT_MAX_PAREN_DEPTH);
    }

    #[test]
    fn test_config_default() {
        let config = LexConfig::default();
        assert_eq!(config.max_string_length(), 100 * 1024 * 1024);
        assert_eq!(config.max_recursion_depth(), 10_000);
        assert_eq!(config.max_field_count(), 10_000_000);
        assert_eq!(config.max_paren_depth(), 1_000);
    }

    #[test]
    fn test_config_strict() {
        let config = LexConfig::strict();
        assert_eq!(config.max_string_length(), 64 * 1024);
        assert_eq!(config.max_recursion_depth(), 32);
        assert_eq!(config.max_field_count(), 1_000);
        assert_eq!(config.max_paren_depth(), 16);
    }

    #[test]
    fn test_config_permissive() {
        let config = LexConfig::permissive();
        assert_eq!(config.max_string_length(), 100 * 1024 * 1024);
        assert_eq!(config.max_recursion_depth(), 1_000);
        assert_eq!(config.max_field_count(), 1_000_000);
        assert_eq!(config.max_paren_depth(), 256);
    }

    // ==================== Builder pattern tests ====================

    #[test]
    fn test_with_max_string_length() {
        let config = LexConfig::new().with_max_string_length(5000);
        assert_eq!(config.max_string_length(), 5000);
    }

    #[test]
    fn test_with_max_recursion_depth() {
        let config = LexConfig::new().with_max_recursion_depth(50);
        assert_eq!(config.max_recursion_depth(), 50);
    }

    #[test]
    fn test_with_max_field_count() {
        let config = LexConfig::new().with_max_field_count(500);
        assert_eq!(config.max_field_count(), 500);
    }

    #[test]
    fn test_with_max_paren_depth() {
        let config = LexConfig::new().with_max_paren_depth(32);
        assert_eq!(config.max_paren_depth(), 32);
    }

    #[test]
    fn test_builder_chaining() {
        let config = LexConfig::new()
            .with_max_string_length(1000)
            .with_max_recursion_depth(20)
            .with_max_field_count(100)
            .with_max_paren_depth(16);

        assert_eq!(config.max_string_length(), 1000);
        assert_eq!(config.max_recursion_depth(), 20);
        assert_eq!(config.max_field_count(), 100);
        assert_eq!(config.max_paren_depth(), 16);
    }

    // ==================== Check methods tests ====================

    #[test]
    fn test_check_string_length_within_limit() {
        let config = LexConfig::new().with_max_string_length(100);
        assert!(config.check_string_length(50));
        assert!(config.check_string_length(100));
    }

    #[test]
    fn test_check_string_length_exceeds_limit() {
        let config = LexConfig::new().with_max_string_length(100);
        assert!(!config.check_string_length(101));
        assert!(!config.check_string_length(1000));
    }

    #[test]
    fn test_check_recursion_depth_within_limit() {
        let config = LexConfig::new().with_max_recursion_depth(10);
        assert!(config.check_recursion_depth(5));
        assert!(config.check_recursion_depth(10));
    }

    #[test]
    fn test_check_recursion_depth_exceeds_limit() {
        let config = LexConfig::new().with_max_recursion_depth(10);
        assert!(!config.check_recursion_depth(11));
        assert!(!config.check_recursion_depth(100));
    }

    #[test]
    fn test_check_field_count_within_limit() {
        let config = LexConfig::new().with_max_field_count(1000);
        assert!(config.check_field_count(500));
        assert!(config.check_field_count(1000));
    }

    #[test]
    fn test_check_field_count_exceeds_limit() {
        let config = LexConfig::new().with_max_field_count(1000);
        assert!(!config.check_field_count(1001));
        assert!(!config.check_field_count(10000));
    }

    #[test]
    fn test_check_paren_depth_within_limit() {
        let config = LexConfig::new().with_max_paren_depth(32);
        assert!(config.check_paren_depth(16));
        assert!(config.check_paren_depth(32));
    }

    #[test]
    fn test_check_paren_depth_exceeds_limit() {
        let config = LexConfig::new().with_max_paren_depth(32);
        assert!(!config.check_paren_depth(33));
        assert!(!config.check_paren_depth(100));
    }

    // ==================== Edge cases ====================

    #[test]
    fn test_zero_limits() {
        let config = LexConfig::new()
            .with_max_string_length(0)
            .with_max_recursion_depth(0)
            .with_max_field_count(0)
            .with_max_paren_depth(0);

        assert_eq!(config.max_string_length(), 0);
        assert_eq!(config.max_recursion_depth(), 0);
        assert_eq!(config.max_field_count(), 0);
        assert_eq!(config.max_paren_depth(), 0);

        // Zero limits reject everything
        assert!(!config.check_string_length(1));
        assert!(!config.check_recursion_depth(1));
        assert!(!config.check_field_count(1));
        assert!(!config.check_paren_depth(1));
    }

    #[test]
    fn test_max_usize_limits() {
        let config = LexConfig::new()
            .with_max_string_length(usize::MAX)
            .with_max_recursion_depth(usize::MAX)
            .with_max_field_count(usize::MAX)
            .with_max_paren_depth(usize::MAX);

        assert_eq!(config.max_string_length(), usize::MAX);
        assert_eq!(config.max_recursion_depth(), usize::MAX);
        assert_eq!(config.max_field_count(), usize::MAX);
        assert_eq!(config.max_paren_depth(), usize::MAX);

        // Max limits accept everything (except usize::MAX + 1, which overflows)
        assert!(config.check_string_length(usize::MAX));
        assert!(config.check_recursion_depth(usize::MAX));
        assert!(config.check_field_count(usize::MAX));
        assert!(config.check_paren_depth(usize::MAX));
    }

    #[test]
    fn test_check_boundary_values() {
        let config = LexConfig::new().with_max_string_length(100);
        assert!(config.check_string_length(0));
        assert!(config.check_string_length(100));
        assert!(!config.check_string_length(101));
    }

    // ==================== Equality and clone tests ====================

    #[test]
    fn test_config_equality() {
        let a = LexConfig::new();
        let b = LexConfig::new();
        assert_eq!(a, b);

        let c = LexConfig::new().with_max_string_length(5000);
        assert_ne!(a, c);
    }

    #[test]
    fn test_config_clone() {
        let original = LexConfig::new().with_max_string_length(5000);
        let cloned = original;
        assert_eq!(original, cloned);
    }

    // ==================== Debug and display ====================

    #[test]
    fn test_config_debug() {
        let config = LexConfig::new();
        let debug = format!("{:?}", config);
        assert!(debug.contains("LexConfig"));
    }

    // ==================== Preset comparison ====================

    #[test]
    fn test_strict_vs_default() {
        let strict = LexConfig::strict();
        let default = LexConfig::default();

        assert!(strict.max_string_length() < default.max_string_length());
        assert!(strict.max_recursion_depth() < default.max_recursion_depth());
        assert!(strict.max_field_count() < default.max_field_count());
        assert!(strict.max_paren_depth() < default.max_paren_depth());
    }

    #[test]
    fn test_permissive_vs_default() {
        let permissive = LexConfig::permissive();
        let default = LexConfig::default();

        assert!(permissive.max_string_length() == default.max_string_length());
        assert!(permissive.max_recursion_depth() < default.max_recursion_depth());
        assert!(permissive.max_field_count() < default.max_field_count());
        assert!(permissive.max_paren_depth() < default.max_paren_depth());
    }

    // ==================== Custom high limit configuration ====================

    #[test]
    fn test_custom_high_limits() {
        // Demonstrate configuring very high limits for trusted data
        let config = LexConfig::new()
            .with_max_string_length(500 * 1024 * 1024) // 500 MB
            .with_max_recursion_depth(50_000) // 50K levels
            .with_max_field_count(100_000_000) // 100M fields
            .with_max_paren_depth(10_000); // 10K levels

        assert_eq!(config.max_string_length(), 500 * 1024 * 1024);
        assert_eq!(config.max_recursion_depth(), 50_000);
        assert_eq!(config.max_field_count(), 100_000_000);
        assert_eq!(config.max_paren_depth(), 10_000);

        // Verify these high limits work correctly
        assert!(config.check_string_length(500 * 1024 * 1024));
        assert!(config.check_recursion_depth(50_000));
        assert!(config.check_field_count(100_000_000));
        assert!(config.check_paren_depth(10_000));
    }

    #[test]
    fn test_unlimited_config() {
        // Demonstrate setting unlimited (max) values
        let config = LexConfig::new()
            .with_max_string_length(usize::MAX)
            .with_max_recursion_depth(usize::MAX)
            .with_max_field_count(usize::MAX)
            .with_max_paren_depth(usize::MAX);

        // Verify unlimited config accepts any value
        assert!(config.check_string_length(usize::MAX));
        assert!(config.check_recursion_depth(usize::MAX));
        assert!(config.check_field_count(usize::MAX));
        assert!(config.check_paren_depth(usize::MAX));

        // Even very large values are accepted
        assert!(config.check_string_length(1_000_000_000));
        assert!(config.check_recursion_depth(1_000_000));
    }

    #[test]
    fn test_use_case_large_datasets() {
        // Configuration for processing large trusted datasets
        let config = LexConfig::new()
            .with_max_string_length(1024 * 1024 * 1024) // 1 GB strings
            .with_max_field_count(50_000_000); // 50M fields

        assert!(config.check_string_length(500 * 1024 * 1024)); // 500 MB ok
        assert!(config.check_field_count(30_000_000)); // 30M ok
    }

    #[test]
    fn test_use_case_deep_nesting() {
        // Configuration for deeply nested trusted structures
        let config = LexConfig::new()
            .with_max_recursion_depth(100_000) // 100K levels
            .with_max_paren_depth(100_000); // 100K parens

        assert!(config.check_recursion_depth(75_000));
        assert!(config.check_paren_depth(75_000));
    }
}