config-lib 1.4.1

Enterprise-grade multi-format configuration library supporting 8 formats (CONF, INI, Properties, JSON, XML, HCL, TOML, NOML) with sub-50ns caching, hot reloading, and comprehensive validation.
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! # HCL Configuration Parser
//!
//! A parser for the static subset of the HashiCorp Configuration Language:
//! the parts of HCL that describe values, as used in Vault, Consul and Nomad
//! configuration files.
//!
//! ## Supported
//!
//! - Attributes: `name = value`, one per line.
//! - Blocks with any number of labels: `server { ... }`,
//!   `resource "aws_instance" "web" { ... }`. Labels nest, so the second
//!   example is the table `resource.aws_instance.web`. A block that appears
//!   twice with the same name and labels becomes an array of tables.
//! - Values: double-quoted strings (escapes `\n`, `\r`, `\t`, `\"`, `\\`,
//!   `\uXXXX`, `\UXXXXXXXX`), integers, decimal floats, `true`, `false`,
//!   `null`, lists `[a, b]` and objects `{ key = value }` (`:` also works),
//!   nested to any depth up to 128 levels.
//! - Comments: `#`, `//` and `/* ... */`.
//!
//! ## Refused
//!
//! Anything that needs evaluating is an error with its line and column:
//! references (`var.region`, a bare word such as `string`), function calls,
//! operators, conditionals, `for` expressions and heredocs. A `${...}` inside
//! a string is kept as text. Since v1.4.0; before, the parser split lines and
//! accepted anything, misreading nested blocks, lists and comments.

use crate::{Error, Result, Value};
use std::collections::BTreeMap;

/// Deepest nesting of blocks, lists and objects
const MAX_DEPTH: usize = 128;

/// HCL configuration parser for HashiCorp Configuration Language
#[cfg(feature = "hcl")]
pub struct HclParser<'a> {
    content: &'a str,
}

/// Parse HCL format configuration
#[cfg(feature = "hcl")]
pub fn parse(source: &str) -> Result<Value> {
    parse_hcl(source)
}

/// Parse HCL format configuration (fallback when HCL is not available)
#[cfg(not(feature = "hcl"))]
pub fn parse(_source: &str) -> Result<Value> {
    Err(crate::Error::feature_not_enabled("hcl"))
}

/// Parse HCL configuration from string
#[cfg(feature = "hcl")]
pub fn parse_hcl(content: &str) -> Result<Value> {
    let mut parser = HclParser::new(content);
    parser.parse()
}

impl<'a> HclParser<'a> {
    /// Create a new HCL parser
    pub fn new(content: &'a str) -> Self {
        Self { content }
    }

    /// Parse HCL content into a Value tree
    pub fn parse(&mut self) -> Result<Value> {
        let mut cursor = Cursor {
            text: self.content,
            pos: 0,
            line: 1,
            column: 1,
            depth: 0,
        };
        let body = cursor.body(false)?;
        Ok(Value::table(body))
    }
}

/// Position in the text. `pos` is a byte offset on a character boundary.
struct Cursor<'a> {
    text: &'a str,
    pos: usize,
    line: usize,
    column: usize,
    depth: usize,
}

impl Cursor<'_> {
    fn error(&self, message: &str) -> Error {
        Error::parse(message, self.line, self.column)
    }

    fn peek(&self) -> Option<char> {
        self.text.get(self.pos..)?.chars().next()
    }

    fn peek_at(&self, offset: usize) -> Option<char> {
        self.text.get(self.pos + offset..)?.chars().next()
    }

    fn bump(&mut self) -> Option<char> {
        let c = self.peek()?;
        self.pos += c.len_utf8();
        if c == '\n' {
            self.line += 1;
            self.column = 1;
        } else {
            self.column += 1;
        }
        Some(c)
    }

    /// Skip spaces, tabs and comments; with `newlines`, line ends too.
    /// Returns whether a line end was skipped.
    fn skip(&mut self, newlines: bool) -> Result<bool> {
        let mut crossed = false;
        loop {
            match self.peek() {
                Some(' ' | '\t' | '\r') => {
                    self.bump();
                }
                Some('\n') if newlines => {
                    crossed = true;
                    self.bump();
                }
                Some('#') => self.skip_line(),
                Some('/') if self.peek_at(1) == Some('/') => self.skip_line(),
                Some('/') if self.peek_at(1) == Some('*') => {
                    let (line, column) = (self.line, self.column);
                    self.bump();
                    self.bump();
                    loop {
                        match self.peek() {
                            None => return Err(Error::parse("Unterminated comment", line, column)),
                            Some('*') if self.peek_at(1) == Some('/') => {
                                self.bump();
                                self.bump();
                                break;
                            }
                            Some(_) => {
                                if self.bump() == Some('\n') {
                                    crossed = true;
                                }
                            }
                        }
                    }
                }
                _ => return Ok(crossed),
            }
        }
    }

    fn skip_line(&mut self) {
        while let Some(c) = self.peek() {
            if c == '\n' {
                break;
            }
            self.bump();
        }
    }

    fn enter(&mut self) -> Result<()> {
        if self.depth >= MAX_DEPTH {
            return Err(self.error("Blocks, lists or objects are nested too deeply"));
        }
        self.depth += 1;
        Ok(())
    }

    fn identifier(&mut self) -> Option<String> {
        let start = self.pos;
        match self.peek() {
            Some(c) if c.is_alphabetic() || c == '_' => {}
            _ => return None,
        }
        while let Some(c) = self.peek() {
            if c.is_alphanumeric() || c == '_' || c == '-' {
                self.bump();
            } else {
                break;
            }
        }
        Some(self.text[start..self.pos].to_string())
    }

    /// A body: attributes and blocks until `}` (when `nested`) or the end
    fn body(&mut self, nested: bool) -> Result<BTreeMap<String, Value>> {
        let mut table = BTreeMap::new();
        loop {
            self.skip(true)?;
            match self.peek() {
                None if nested => return Err(self.error("A block is not closed")),
                None => return Ok(table),
                Some('}') if nested => {
                    self.bump();
                    return Ok(table);
                }
                Some('}') => return Err(self.error("Unexpected '}'")),
                _ => {}
            }
            let (line, column) = (self.line, self.column);
            let name = match self.peek() {
                Some('"') => self.string()?,
                _ => self
                    .identifier()
                    .ok_or_else(|| self.error("Expected an attribute or block name"))?,
            };
            self.skip(false)?;
            if self.peek() == Some('=') {
                self.bump();
                self.skip(false)?;
                let value = self.expression()?;
                self.end_of_item()?;
                if table.contains_key(&name) {
                    return Err(Error::parse("An attribute is defined twice", line, column));
                }
                table.insert(name, value);
            } else {
                self.block(&mut table, name, line, column)?;
            }
        }
    }

    /// After an attribute: a line end, a comment, `}` or the end
    fn end_of_item(&mut self) -> Result<()> {
        let crossed = self.skip(false)?;
        match self.peek() {
            None | Some('\n' | '}') => Ok(()),
            _ if crossed => Ok(()),
            _ => Err(self.error("Expected a line end after the value")),
        }
    }

    /// A block: labels, then `{ body }`
    fn block(
        &mut self,
        table: &mut BTreeMap<String, Value>,
        name: String,
        line: usize,
        column: usize,
    ) -> Result<()> {
        let mut path = vec![name];
        loop {
            self.skip(false)?;
            match self.peek() {
                Some('{') => break,
                Some('"') => path.push(self.string()?),
                Some(c) if c.is_alphabetic() || c == '_' => {
                    if let Some(label) = self.identifier() {
                        path.push(label);
                    }
                }
                _ => return Err(self.error("Expected '=' or a block")),
            }
        }
        self.bump();
        self.enter()?;
        let body = self.body(true)?;
        self.depth -= 1;
        insert_block(table, &path, body).map_err(|message| Error::parse(message, line, column))
    }

    fn expression(&mut self) -> Result<Value> {
        match self.peek() {
            Some('"') => Ok(Value::string(self.string()?)),
            Some('[') => self.list(),
            Some('{') => self.object(),
            Some('<') if self.peek_at(1) == Some('<') => {
                Err(self.error("Heredoc strings are not supported"))
            }
            Some(c) if c.is_ascii_digit() || c == '-' || c == '+' || c == '.' => self.number(),
            Some(c) if c.is_alphabetic() || c == '_' => {
                let (line, column) = (self.line, self.column);
                let word = self.identifier().unwrap_or_default();
                match word.as_str() {
                    "true" => Ok(Value::bool(true)),
                    "false" => Ok(Value::bool(false)),
                    "null" => Ok(Value::null()),
                    _ => Err(Error::parse(
                        "References, function calls and other expressions are not supported",
                        line,
                        column,
                    )),
                }
            }
            _ => Err(self.error("Expected a value")),
        }
        .and_then(|value| {
            // A value followed by an operator, `.`, `(` or `?` is an
            // expression this parser does not evaluate
            self.skip(false)?;
            match self.peek() {
                Some(
                    '+' | '-' | '*' | '/' | '%' | '<' | '>' | '!' | '&' | '|' | '?' | '.' | '(',
                ) if !(self.peek() == Some('/') && matches!(self.peek_at(1), Some('/' | '*'))) => {
                    Err(self.error("Operators and other expressions are not supported"))
                }
                _ => Ok(value),
            }
        })
    }

    fn number(&mut self) -> Result<Value> {
        let (line, column) = (self.line, self.column);
        let start = self.pos;
        if matches!(self.peek(), Some('-' | '+')) {
            self.bump();
        }
        while let Some(c) = self.peek() {
            let exponent_sign = matches!(c, '+' | '-')
                && matches!(self.text[..self.pos].chars().last(), Some('e' | 'E'));
            if c.is_ascii_digit() || matches!(c, '.' | 'e' | 'E') || exponent_sign {
                self.bump();
            } else {
                break;
            }
        }
        let text = &self.text[start..self.pos];
        if let Ok(i) = text.parse::<i64>() {
            return Ok(Value::integer(i));
        }
        match super::decimal_float(text) {
            Some(f) => Ok(Value::float(f)),
            None => Err(Error::parse("Invalid number", line, column)),
        }
    }

    fn list(&mut self) -> Result<Value> {
        self.bump();
        self.enter()?;
        let mut items = Vec::new();
        loop {
            self.skip(true)?;
            if self.peek() == Some(']') {
                self.bump();
                break;
            }
            items.push(self.expression()?);
            self.skip(true)?;
            match self.peek() {
                Some(',') => {
                    self.bump();
                }
                Some(']') => {
                    self.bump();
                    break;
                }
                None => return Err(self.error("A list is not closed")),
                _ => return Err(self.error("Expected ',' or ']' in a list")),
            }
        }
        self.depth -= 1;
        Ok(Value::array(items))
    }

    fn object(&mut self) -> Result<Value> {
        self.bump();
        self.enter()?;
        let mut table = BTreeMap::new();
        loop {
            self.skip(true)?;
            match self.peek() {
                Some('}') => {
                    self.bump();
                    break;
                }
                None => return Err(self.error("An object is not closed")),
                _ => {}
            }
            let (line, column) = (self.line, self.column);
            let key = match self.peek() {
                Some('"') => self.string()?,
                _ => self
                    .identifier()
                    .ok_or_else(|| self.error("Expected an object key"))?,
            };
            self.skip(false)?;
            match self.peek() {
                Some('=' | ':') => {
                    self.bump();
                }
                _ => return Err(self.error("Expected '=' or ':' after an object key")),
            }
            self.skip(false)?;
            let value = self.expression()?;
            if table.contains_key(&key) {
                return Err(Error::parse("An object key is defined twice", line, column));
            }
            table.insert(key, value);
            let crossed = self.skip(false)?;
            match self.peek() {
                Some(',') => {
                    self.bump();
                }
                Some('}' | '\n') => {}
                _ if crossed => {}
                None => return Err(self.error("An object is not closed")),
                _ => return Err(self.error("Expected ',', a line end or '}' in an object")),
            }
        }
        self.depth -= 1;
        Ok(Value::table(table))
    }

    /// A double-quoted string. The text is not quoted back in errors.
    fn string(&mut self) -> Result<String> {
        let (line, column) = (self.line, self.column);
        self.bump();
        let mut out = String::new();
        loop {
            match self.bump() {
                None | Some('\n') => {
                    return Err(Error::parse("Unterminated string", line, column));
                }
                Some('"') => return Ok(out),
                Some('\\') => match self.bump() {
                    Some('n') => out.push('\n'),
                    Some('r') => out.push('\r'),
                    Some('t') => out.push('\t'),
                    Some('"') => out.push('"'),
                    Some('\\') => out.push('\\'),
                    Some(kind @ ('u' | 'U')) => {
                        let len = if kind == 'u' { 4 } else { 8 };
                        let hex = self.text.get(self.pos..self.pos + len).unwrap_or("");
                        let ch = u32::from_str_radix(hex, 16)
                            .ok()
                            .filter(|_| hex.len() == len)
                            .and_then(char::from_u32)
                            .ok_or_else(|| self.error("Invalid unicode escape"))?;
                        for _ in 0..len {
                            self.bump();
                        }
                        out.push(ch);
                    }
                    _ => return Err(self.error("Invalid escape sequence")),
                },
                Some(c) => out.push(c),
            }
        }
    }
}

/// Put a block's body at `path`; a repeated block becomes an array
fn insert_block(
    table: &mut BTreeMap<String, Value>,
    path: &[String],
    body: BTreeMap<String, Value>,
) -> std::result::Result<(), &'static str> {
    let (last, parents) = match path.split_last() {
        Some(split) => split,
        None => return Ok(()),
    };
    let mut current = table;
    for part in parents {
        let entry = current
            .entry(part.clone())
            .or_insert_with(|| Value::table(BTreeMap::new()));
        current = match entry {
            Value::Table(inner) => inner,
            _ => return Err("A block has the name of an attribute"),
        };
    }
    match current.get_mut(last) {
        None => {
            current.insert(last.clone(), Value::table(body));
        }
        Some(Value::Array(items)) if items.iter().all(Value::is_table) => {
            items.push(Value::table(body));
        }
        Some(existing @ Value::Table(_)) => {
            let first = std::mem::replace(existing, Value::Null);
            *existing = Value::array(vec![first, Value::table(body)]);
        }
        Some(_) => return Err("A block has the name of an attribute"),
    }
    Ok(())
}

/// Placeholder when HCL feature is disabled
#[cfg(not(feature = "hcl"))]
pub fn parse_hcl(_content: &str) -> Result<Value> {
    Err(crate::error::Error::feature_not_enabled("hcl"))
}

#[cfg(all(test, feature = "hcl"))]
mod tests {
    use super::*;

    #[test]
    fn test_simple_hcl() {
        let hcl = r#"
        database {
          host = "localhost"
          port = 5432
          enabled = true
        }
        
        app {
          name = "MyApp"
          version = "1.0.0"
        }
        "#;

        let result = parse_hcl(hcl).unwrap();

        if let Value::Table(config) = result {
            if let Some(Value::Table(db)) = config.get("database") {
                assert_eq!(db.get("host"), Some(&Value::string("localhost")));
                assert_eq!(db.get("port"), Some(&Value::integer(5432)));
                assert_eq!(db.get("enabled"), Some(&Value::bool(true)));
            } else {
                panic!("Expected database configuration");
            }

            if let Some(Value::Table(app)) = config.get("app") {
                assert_eq!(app.get("name"), Some(&Value::string("MyApp")));
                assert_eq!(app.get("version"), Some(&Value::string("1.0.0")));
            } else {
                panic!("Expected app configuration");
            }
        } else {
            panic!("Expected table result");
        }
    }

    #[test]
    fn test_terraform_style_hcl() {
        let hcl = r#"
        resource "aws_instance" "web" {
          ami           = "ami-12345678"
          instance_type = "t2.micro"
          
          tags = {
            Name = "WebServer"
            Environment = "production"
          }
        }
        
        variable "region" {
          description = "AWS region"
          type        = "string"
          default     = "us-west-2"
        }
        "#;

        let result = parse_hcl(hcl);

        // Test passes if parsing doesn't panic (HCL syntax can be complex)
        match result {
            Ok(Value::Table(_)) => {
                // Successfully parsed
            }
            Ok(_) => panic!("Expected table result"),
            Err(_) => {
                // Some HCL syntax variants are not supported by the simplified
                // parser; the test tolerates any structured `Err` here as long
                // as it didn't panic. Specific syntax coverage is asserted
                // separately in `test_terraform_style_hcl`.
            }
        }
    }

    #[test]
    fn test_hcl_arrays_and_objects() {
        let hcl = r#"
        servers = ["web1", "web2", "web3"]
        
        database {
          replicas = [
            {
              host = "db1.example.com"
              role = "master"
            },
            {
              host = "db2.example.com" 
              role = "slave"
            }
          ]
        }
        "#;

        let result = parse_hcl(hcl).unwrap();

        if let Value::Table(config) = result {
            // Check servers array
            if let Some(Value::Array(servers)) = config.get("servers") {
                assert_eq!(servers.len(), 3);
                assert_eq!(servers[0], Value::string("web1"));
            } else {
                panic!("Expected servers array");
            }

            // Check database replicas
            if let Some(Value::Table(db)) = config.get("database") {
                if let Some(Value::Array(replicas)) = db.get("replicas") {
                    assert_eq!(replicas.len(), 2);
                    if let Value::Table(replica1) = &replicas[0] {
                        assert_eq!(replica1.get("role"), Some(&Value::string("master")));
                    }
                } else {
                    panic!("Expected replicas array");
                }
            } else {
                panic!("Expected database configuration");
            }
        } else {
            panic!("Expected table result");
        }
    }
}