hocon-parser 1.6.0

Full Lightbend HOCON specification-compliant parser for Rust
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
//! # hocon
//!
//! Full [Lightbend HOCON specification](https://github.com/lightbend/config/blob/main/HOCON.md)-compliant
//! parser for Rust.
//!
//! ## Quick Example
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = hocon::parse(r#"
//!     server {
//!         host = "localhost"
//!         port = 8080
//!     }
//! "#)?;
//!
//! assert_eq!(config.get_string("server.host")?, "localhost");
//! assert_eq!(config.get_i64("server.port")?, 8080);
//! # Ok(())
//! # }
//! ```
//!
//! ## Parsing
//!
//! - [`parse`] -- parse a HOCON string into a [`Config`].
//! - [`parse_file`] -- parse a HOCON file. Include directives are resolved
//!   relative to the file's directory.
//! - [`parse_with_env`] / [`parse_file_with_env`] -- parse with a custom
//!   environment variable map instead of inheriting the process environment.
//!
//! ## Accessing Values
//!
//! [`Config`] provides typed getters that accept dot-separated paths:
//!
//! | Method | Return type |
//! |--------|-------------|
//! | [`Config::get_string`] | `Result<String, ConfigError>` |
//! | [`Config::get_i64`] | `Result<i64, ConfigError>` |
//! | [`Config::get_f64`] | `Result<f64, ConfigError>` |
//! | [`Config::get_bool`] | `Result<bool, ConfigError>` |
//! | [`Config::get_config`] | `Result<Config, ConfigError>` |
//! | [`Config::get_list`] | `Result<Vec<HoconValue>, ConfigError>` |
//! | [`Config::get_duration`] | `Result<Duration, ConfigError>` |
//! | [`Config::get_bytes`] | `Result<i64, ConfigError>` |
//!
//! Each typed getter has an `_option` variant (e.g., [`Config::get_string_option`])
//! that returns `Option<T>` instead.
//!
//! ## Duration and Byte-Size Values
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = hocon::parse(r#"
//!     timeout = 30 seconds
//!     max-upload = 512 MB
//! "#)?;
//!
//! let timeout = config.get_duration("timeout")?;
//! let max_upload = config.get_bytes("max-upload")?;
//! # Ok(())
//! # }
//! ```
//!
//! Duration units: `ns`, `us`, `ms`, `s`/`seconds`, `m`/`minutes`, `h`/`hours`, `d`/`days`.
//!
//! Byte-size units: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`
//! (and their long forms like `megabytes`, `mebibytes`).
//!
//! ## Serde Deserialization
//!
//! With the `serde` feature enabled, deserialize a [`Config`] (or sub-config)
//! into any type implementing `serde::Deserialize`:
//!
//! ```rust,ignore
//! use serde::Deserialize;
//!
//! #[derive(Deserialize)]
//! struct Server {
//!     host: String,
//!     port: u16,
//! }
//!
//! let server: Server = config.get_config("server")?.deserialize()?;
//! ```
//!
//! ## Include Files
//!
//! HOCON supports `include` directives to compose configuration from multiple files:
//!
//! ```hocon
//! include "defaults.conf"
//!
//! server.port = 9090  # override a value from defaults
//! ```
//!
//! When parsing with [`parse_file`], include paths are resolved relative to the
//! file being parsed.
//!
//! ## Error Types
//!
//! - [`HoconError`] -- unified error returned by parse functions. Wraps:
//!   - [`ParseError`] -- syntax errors during lexing or parsing (includes line/column).
//!   - [`ResolveError`] -- substitution resolution failures, cycle detection.
//!   - `std::io::Error` -- file I/O errors (top-level file read; include file errors appear as [`ResolveError`]).
//! - [`ConfigError`] -- missing keys or type mismatches when accessing values.
//!
//! ## HOCON Specification
//!
//! For the full specification, see the
//! [Lightbend HOCON spec](https://github.com/lightbend/config/blob/main/HOCON.md).

pub mod config;
pub mod error;
/// Internal lexer module. Not part of the stable public API.
///
/// This module is `pub` to allow integration tests to access internal types.
/// All items are subject to change without notice across minor versions.
/// Prefer the re-exported items (`tokenize`, `Token`, etc.) over direct module access.
#[doc(hidden)]
pub mod lexer;
pub(crate) mod numeric_array;
pub mod options;
/// Internal parser module. Not part of the stable public API.
///
/// This module is `pub` to allow integration tests to access internal types.
/// All items are subject to change without notice across minor versions.
#[doc(hidden)]
pub mod parser;
pub(crate) mod properties;
/// Internal resolver module. Not part of the stable public API.
///
/// This module is `pub` to allow integration tests to access internal types
/// (`build_tree`, `resolve_tree`, `merge_unresolved`, `ResObj`, etc.).
/// All items are subject to change without notice across minor versions.
#[doc(hidden)]
pub mod resolver;
pub mod value;
mod value_factory;

#[cfg(feature = "serde")]
pub mod serde;

pub use config::{Config, Period};
pub use error::{ConfigError, HoconError, NotResolvedError, ParseError, ResolveError};
pub use options::{ParseOptions, ResolveOptions};
pub use value::{HoconValue, ScalarType, ScalarValue};
pub use value_factory::empty;

#[cfg(feature = "serde")]
pub use value_factory::from_map;

// Lexer surface intentionally narrow — only the items integration tests
// and diagnostic tooling need. The full lexer module is not part of the
// public API.
pub use lexer::{tokenize, Segment, SubstPayload, Token, TokenKind};

#[cfg(feature = "serde")]
pub use serde::DeserializeError;

use std::collections::HashMap;
use std::path::Path;

// ── include-package feature: public Parser builder ───────────────────────────

/// Builder-style parser with a per-instance package registry for
/// `include package(...)` support (E11).
///
/// # Feature flag
///
/// This type is only available when the `include-package` Cargo feature is
/// enabled:
///
/// ```toml
/// [dependencies]
/// hocon-parser = { version = "...", features = ["include-package"] }
/// ```
///
/// # Usage
///
/// ```rust,ignore
/// # #[cfg(feature = "include-package")]
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = hocon::Parser::new()
///     .register_package("github.com/org/pkg", "reference.conf", include_str!("conf/reference.conf"))
///     .parse_file("app.conf")?;
/// # Ok(())
/// # }
/// ```
///
/// # Cascade convention
///
/// For packages that depend on other HOCON-config-providing packages, follow
/// this convention to cascade registrations:
///
/// ```rust,ignore
/// // In your package (e.g., pkg_a/src/hocon.rs):
/// pub fn register(parser: hocon::Parser) -> hocon::Parser {
///     let parser = parser
///         .register_package("github.com/org/pkg_a", "reference.conf", include_str!("../conf/reference.conf"));
///     // Cascade to dependencies:
///     // let parser = pkg_b::hocon::register(parser);
///     parser
/// }
/// ```
///
/// Callers:
/// ```rust,ignore
/// let config = pkg_a::hocon::register(hocon::Parser::new())
///     .parse_file("app.conf")?;
/// ```
///
/// # Collision policy
///
/// Registering two **different** content strings for the same `(identifier, file)`
/// key **panics** — this is a programming error (setup-time invariant). Re-registering
/// **byte-identical** content is idempotent (no panic).
#[cfg(feature = "include-package")]
pub struct Parser {
    registry: HashMap<(String, String), String>,
}

#[cfg(feature = "include-package")]
impl Default for Parser {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "include-package")]
impl Parser {
    /// Create a new `Parser` with an empty package registry.
    pub fn new() -> Self {
        Parser {
            registry: HashMap::new(),
        }
    }

    /// Register HOCON content for an `include package("identifier", "file")`
    /// include statement.
    ///
    /// # Arguments
    ///
    /// * `identifier` — the package identifier (e.g., `"github.com/org/pkg"`).
    ///   Should follow Go-module-path style for cross-impl portability (E11 decision 1),
    ///   but any non-empty string is accepted by the parser.
    /// * `file` — the file path within the package (e.g., `"reference.conf"`).
    ///   Must satisfy E11 decision 6 constraints.
    /// * `content` — the HOCON source text. Typically loaded via `include_str!`.
    ///   Empty content is valid and contributes `{}` to the merge.
    ///
    /// # Panics
    ///
    /// Panics if different content is registered for the same `(identifier, file)` pair.
    /// Re-registering byte-identical content is idempotent (no panic).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let parser = hocon::Parser::new()
    ///     .register_package("github.com/org/pkg", "reference.conf", include_str!("conf/reference.conf"))
    ///     .register_package("github.com/org/pkg", "overrides.conf", include_str!("conf/overrides.conf"));
    /// ```
    pub fn register_package(
        mut self,
        identifier: impl Into<String>,
        file: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        let id = identifier.into();
        let f = file.into();
        let c = content.into();
        if let Some(existing) = self.registry.get(&(id.clone(), f.clone())) {
            if existing != &c {
                panic!(
                    "hocon: conflicting content registered for package ({:?}, {:?}): \
                     different content already registered for this (identifier, file) pair",
                    id, f
                );
            }
            // byte-identical: idempotent, no-op
        } else {
            self.registry.insert((id, f), c);
        }
        self
    }

    /// Parse a HOCON string using the registered package registry.
    pub fn parse(self, input: &str) -> Result<Config, HoconError> {
        self.parse_with_env(input, &std::env::vars().collect())
    }

    /// Parse a HOCON file using the registered package registry.
    pub fn parse_file(self, path: impl AsRef<Path>) -> Result<Config, HoconError> {
        self.parse_file_with_env(path, &std::env::vars().collect())
    }

    /// Parse a HOCON string with a custom environment map and the registered registry.
    pub fn parse_with_env(
        self,
        input: &str,
        env: &HashMap<String, String>,
    ) -> Result<Config, HoconError> {
        self.parse_with_options(input, ParseOptions::defaults().with_env(env.clone()))
    }

    /// Parse a HOCON file with a custom environment map and the registered registry.
    pub fn parse_file_with_env(
        self,
        path: impl AsRef<Path>,
        env: &HashMap<String, String>,
    ) -> Result<Config, HoconError> {
        self.parse_file_with_options(path, ParseOptions::defaults().with_env(env.clone()))
    }

    /// Parse a HOCON string with explicit [`ParseOptions`] and the registered registry.
    ///
    /// Equivalent to the module-level [`parse_string_with_options`] but threads the
    /// per-`Parser` package registry through phase 1, enabling
    /// `include package("identifier", "file")` to resolve against `register_package`-supplied
    /// content. Supports both fused (`resolve_substitutions = true`, default) and deferred
    /// (`resolve_substitutions = false`) lifecycles. The latter returns an unresolved
    /// `Config` whose `Config::resolve` call performs phase 2 — includes are already
    /// inlined at phase 1 so the registry is not needed after this call returns.
    pub fn parse_with_options(self, input: &str, opts: ParseOptions) -> Result<Config, HoconError> {
        let tokens = lexer::tokenize(input)?;
        assert_non_empty_document(&tokens)?;
        let ast = parser::parse_tokens(&tokens)?;

        let env: HashMap<String, String> = opts.env.clone().unwrap_or_else(|| {
            if opts.resolve_substitutions {
                std::env::vars().collect()
            } else {
                HashMap::new()
            }
        });

        let internal_opts = self.into_resolve_opts(env, opts.base_dir.clone());

        if opts.resolve_substitutions {
            let value = resolver::resolve(ast, &internal_opts)?;
            match value {
                HoconValue::Object(fields) => {
                    let mut cfg = Config::new(fields);
                    cfg.parse_base_dir = opts.base_dir;
                    cfg.origin_description = opts.origin_description;
                    Ok(cfg)
                }
                _ => Err(HoconError::Parse(ParseError {
                    message: "root must be an object".into(),
                    line: 1,
                    col: 1,
                })),
            }
        } else {
            let tree = resolver::build_tree(ast, &internal_opts)?;
            Ok(Config::new_from_res_obj(
                tree,
                opts.base_dir,
                opts.origin_description,
            ))
        }
    }

    /// Parse a HOCON file with explicit [`ParseOptions`] and the registered registry.
    /// File's parent directory is used as base_dir (overrides `opts.base_dir`).
    pub fn parse_file_with_options(
        self,
        path: impl AsRef<Path>,
        opts: ParseOptions,
    ) -> Result<Config, HoconError> {
        let path = path.as_ref();
        let content = std::fs::read_to_string(path)
            .map_err(|e| std::io::Error::new(e.kind(), format!("{}: {}", path.display(), e)))?;
        let base_dir = path.parent().map(|p| p.to_path_buf());
        let opts = ParseOptions { base_dir, ..opts };
        self.parse_with_options(&content, opts)
    }

    /// Convert this `Parser` into `ResolveOptions`, threading the registry in.
    fn into_resolve_opts(
        self,
        env: HashMap<String, String>,
        base_dir: Option<std::path::PathBuf>,
    ) -> resolver::InternalResolveOptions {
        let mut opts = resolver::InternalResolveOptions::new(env);
        if let Some(dir) = base_dir {
            opts = opts.with_base_dir(dir);
        }
        opts.package_registry = std::sync::Arc::new(self.registry);
        opts
    }
}

/// Parse a HOCON string into a Config.
pub fn parse(input: &str) -> Result<Config, HoconError> {
    parse_with_env(input, &std::env::vars().collect())
}

/// Parse a HOCON string with explicit [`ParseOptions`].
///
/// `opts.resolve_substitutions = true` (default): fused parse + resolve, same
/// as [`parse`]. `opts.resolve_substitutions = false`: phase 1 only; returned
/// `Config` may have `is_resolved() = false`. Use [`Config::resolve`] later.
///
/// This module-level function does **not** thread a package registry — for that,
/// use [`Parser::parse_with_options`] (feature `include-package`).
pub fn parse_string_with_options(input: &str, opts: ParseOptions) -> Result<Config, HoconError> {
    let tokens = lexer::tokenize(input)?;
    assert_non_empty_document(&tokens)?;
    let ast = parser::parse_tokens(&tokens)?;

    let env: HashMap<String, String> = opts.env.clone().unwrap_or_else(|| {
        if opts.resolve_substitutions {
            std::env::vars().collect()
        } else {
            HashMap::new()
        }
    });

    let mut internal_opts = resolver::InternalResolveOptions::new(env);
    if let Some(ref bd) = opts.base_dir {
        internal_opts = internal_opts.with_base_dir(bd.clone());
    }

    if opts.resolve_substitutions {
        // Fused path: phase 1 + phase 2.
        let value = resolver::resolve(ast, &internal_opts)?;
        match value {
            HoconValue::Object(fields) => {
                let mut cfg = Config::new(fields);
                cfg.parse_base_dir = opts.base_dir;
                cfg.origin_description = opts.origin_description;
                Ok(cfg)
            }
            _ => Err(HoconError::Parse(ParseError {
                message: "root must be an object".into(),
                line: 1,
                col: 1,
            })),
        }
    } else {
        // Deferred path: phase 1 only.
        let tree = resolver::build_tree(ast, &internal_opts)?;
        Ok(Config::new_from_res_obj(
            tree,
            opts.base_dir,
            opts.origin_description,
        ))
    }
}

/// Parse a HOCON file with explicit [`ParseOptions`].
/// File's parent directory is used as base_dir (overrides opts.base_dir).
pub fn parse_file_with_options<P: AsRef<Path>>(
    path: P,
    opts: ParseOptions,
) -> Result<Config, HoconError> {
    let path = path.as_ref();
    let content = std::fs::read_to_string(path)
        .map_err(|e| std::io::Error::new(e.kind(), format!("{}: {}", path.display(), e)))?;
    let base_dir = path.parent().map(|p| p.to_path_buf());
    let opts = ParseOptions { base_dir, ..opts };
    parse_string_with_options(&content, opts)
}

/// Parse a HOCON file into a Config.
pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Config, HoconError> {
    parse_file_with_env(path, &std::env::vars().collect())
}

/// Parse a HOCON file with a custom environment variable map.
pub fn parse_file_with_env<P: AsRef<Path>>(
    path: P,
    env: &HashMap<String, String>,
) -> Result<Config, HoconError> {
    let path = path.as_ref();
    let content = std::fs::read_to_string(path)
        .map_err(|e| std::io::Error::new(e.kind(), format!("{}: {}", path.display(), e)))?;
    let tokens = lexer::tokenize(&content)?;
    assert_non_empty_document(&tokens)?;
    let ast = parser::parse_tokens(&tokens)?;
    let mut opts = resolver::InternalResolveOptions::new(env.clone());
    if let Some(dir) = path.parent() {
        opts = opts.with_base_dir(dir.to_path_buf());
    }
    let value = resolver::resolve(ast, &opts)?;
    match value {
        HoconValue::Object(fields) => Ok(Config::new(fields)),
        _ => Err(HoconError::Parse(ParseError {
            message: "root must be an object".into(),
            line: 1,
            col: 1,
        })),
    }
}

/// Parse a HOCON string with a custom environment variable map.
pub fn parse_with_env(input: &str, env: &HashMap<String, String>) -> Result<Config, HoconError> {
    let tokens = lexer::tokenize(input)?;
    assert_non_empty_document(&tokens)?;
    let ast = parser::parse_tokens(&tokens)?;
    let opts = resolver::InternalResolveOptions::new(env.clone());
    let value = resolver::resolve(ast, &opts)?;
    match value {
        HoconValue::Object(fields) => Ok(Config::new(fields)),
        _ => Err(HoconError::Parse(ParseError {
            message: "root must be an object".into(),
            line: 1,
            col: 1,
        })),
    }
}

/// Internal JSON renderer for use by Layer-2 fixture tests.
///
/// Emits compact sorted-key JSON. Not semver-stable.
/// Callers: `tests/deferred_resolution_fixtures.rs`.
#[doc(hidden)]
pub fn _render_json_for_test(config: &Config) -> String {
    use crate::value::HoconValue;
    use std::fmt::Write;

    fn render_value(val: &HoconValue, out: &mut String) {
        match val {
            HoconValue::Scalar(sv) => {
                use crate::value::ScalarType;
                match sv.value_type {
                    ScalarType::Null => out.push_str("null"),
                    ScalarType::Boolean => out.push_str(&sv.raw),
                    ScalarType::Number => out.push_str(&sv.raw),
                    ScalarType::String => {
                        let escaped = sv
                            .raw
                            .replace('\\', "\\\\")
                            .replace('"', "\\\"")
                            .replace('\n', "\\n")
                            .replace('\r', "\\r")
                            .replace('\t', "\\t");
                        let _ = write!(out, "\"{}\"", escaped);
                    }
                }
            }
            HoconValue::Object(map) => {
                out.push('{');
                let mut keys: Vec<&str> = map.keys().map(|s| s.as_str()).collect();
                keys.sort_unstable();
                for (i, k) in keys.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    let _ = write!(out, "\"{}\":", k);
                    render_value(map.get(*k).unwrap(), out);
                }
                out.push('}');
            }
            HoconValue::Array(arr) => {
                out.push('[');
                for (i, v) in arr.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    render_value(v, out);
                }
                out.push(']');
            }
            HoconValue::Placeholder(pv) => {
                let _ = write!(out, "\"<unresolved:{}>\"", pv.path);
            }
        }
    }

    let mut out = String::from("{");
    let mut keys: Vec<&str> = config.root.keys().map(|s| s.as_str()).collect();
    keys.sort_unstable();
    for (i, k) in keys.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        let _ = write!(out, "\"{}\":", k);
        render_value(config.root.get(*k).unwrap(), &mut out);
    }
    out.push('}');
    out
}

/// Guard: reject token streams that carry no semantic content (HOCON.md L130).
///
/// An empty document is one whose token stream contains only `Newline` and `Eof`
/// tokens after the lexer has already stripped whitespace, BOM, and comments.
/// A document with at least one structural or value token (including `{`, `}`,
/// unquoted/quoted text, substitutions, …) is not empty even if it resolves to
/// an empty object.
fn assert_non_empty_document(tokens: &[lexer::Token]) -> Result<(), HoconError> {
    let has_content = tokens
        .iter()
        .any(|t| !matches!(t.kind, lexer::TokenKind::Newline | lexer::TokenKind::Eof));
    if !has_content {
        return Err(HoconError::Parse(ParseError {
            message: "empty file is not a valid HOCON document (HOCON.md L130)".into(),
            line: 1,
            col: 1,
        }));
    }
    Ok(())
}