pub struct Config { /* private fields */ }Expand description
A parsed HOCON configuration object.
Config wraps an ordered map of top-level keys to HoconValues and
provides typed getters that accept dot-separated paths
(e.g., "server.host").
After E12, Config may be resolved (no substitution placeholders remain)
or unresolved (placeholders remain; call Config::resolve before
accessing values that touch a placeholder). Check with Config::is_resolved.
Implementations§
Source§impl Config
impl Config
Sourcepub fn new(root: IndexMap<String, HoconValue>) -> Self
pub fn new(root: IndexMap<String, HoconValue>) -> Self
Create a Config from a pre-built ordered map of key-value pairs.
Marks the config as resolved (no substitution placeholders).
Sourcepub fn is_resolved(&self) -> bool
pub fn is_resolved(&self) -> bool
Returns true if the config’s value tree contains no unresolved
substitution placeholders. Whole-config granularity per E12 decision 11.
Sourcepub fn origin_description(&self) -> Option<&str>
pub fn origin_description(&self) -> Option<&str>
The user-visible source name associated with this config, if any.
Sourcepub fn resolve(&self, opts: ResolveOptions) -> Result<Config, HoconError>
pub fn resolve(&self, opts: ResolveOptions) -> Result<Config, HoconError>
Perform substitution resolution, producing a fully resolved Config.
Idempotent on already-resolved Configs. On unresolved Configs, runs
resolver::resolve_tree (phase 2) on the stored unresolved_tree
(priors preserved for S13a self-ref) or reconstructed ResObj.
Sourcepub fn resolve_with(
&self,
source: &Config,
opts: ResolveOptions,
) -> Result<Config, HoconError>
pub fn resolve_with( &self, source: &Config, opts: ResolveOptions, ) -> Result<Config, HoconError>
Resolve substitutions using source for lookup; source keys NOT in result.
Differs from self.with_fallback(source).resolve(opts) which DOES
include source keys in the result.
Precondition: source.is_resolved() must be true. If not,
returns Err(HoconError::NotResolved(...)) immediately (E12 decision 10).
The filter is RECURSIVE: only paths in receiver’s pre-merge shape are kept.
Sourcepub fn get(&self, path: &str) -> Option<&HoconValue>
pub fn get(&self, path: &str) -> Option<&HoconValue>
Return the raw HoconValue at the given dot-separated path,
or None if the path does not exist.
Sourcepub fn get_string(&self, path: &str) -> Result<String, ConfigError>
pub fn get_string(&self, path: &str) -> Result<String, ConfigError>
Return the value at path as a String.
Returns the raw string for any non-null scalar (string, number,
boolean). Returns ConfigError if the path is missing, the value
is an Object or Array, or the value is null (spec L1252: null →
any non-null type is an error).
Sourcepub fn get_i64(&self, path: &str) -> Result<i64, ConfigError>
pub fn get_i64(&self, path: &str) -> Result<i64, ConfigError>
Return the value at path as an i64.
Whole-number floats and numeric strings are coerced automatically.
Returns ConfigError if the path is missing or the value cannot be
represented as i64.
Sourcepub fn get_f64(&self, path: &str) -> Result<f64, ConfigError>
pub fn get_f64(&self, path: &str) -> Result<f64, ConfigError>
Return the value at path as an f64.
Integers and numeric strings are coerced automatically.
Returns ConfigError if the path is missing or the value cannot be
represented as f64.
Sourcepub fn get_bool(&self, path: &str) -> Result<bool, ConfigError>
pub fn get_bool(&self, path: &str) -> Result<bool, ConfigError>
Return the value at path as a bool.
String values "true", "yes", "on" (case-insensitive) coerce to
true; "false", "no", "off" coerce to false.
Returns ConfigError if the path is missing or the value is not boolean-like.
Sourcepub fn get_config(&self, path: &str) -> Result<Config, ConfigError>
pub fn get_config(&self, path: &str) -> Result<Config, ConfigError>
Return the sub-object at path as a new Config.
Returns ConfigError if the path is missing or the value is not an object.
Sourcepub fn get_list(&self, path: &str) -> Result<Vec<HoconValue>, ConfigError>
pub fn get_list(&self, path: &str) -> Result<Vec<HoconValue>, ConfigError>
Return the array at path as a Vec<HoconValue>.
Returns ConfigError if the path is missing or the value is not an array.
Numerically-indexed objects (S15) are converted to an array on demand:
{"0":"a","1":"b"} returns ["a","b"]. Empty objects and objects with
no integer keys are NOT converted — they return a type-mismatch error.
Sourcepub fn get_string_option(&self, path: &str) -> Option<String>
pub fn get_string_option(&self, path: &str) -> Option<String>
Like get_string but returns None instead of an error.
Sourcepub fn get_i64_option(&self, path: &str) -> Option<i64>
pub fn get_i64_option(&self, path: &str) -> Option<i64>
Like get_i64 but returns None instead of an error.
Sourcepub fn get_f64_option(&self, path: &str) -> Option<f64>
pub fn get_f64_option(&self, path: &str) -> Option<f64>
Like get_f64 but returns None instead of an error.
Sourcepub fn get_bool_option(&self, path: &str) -> Option<bool>
pub fn get_bool_option(&self, path: &str) -> Option<bool>
Like get_bool but returns None instead of an error.
Sourcepub fn get_config_option(&self, path: &str) -> Option<Config>
pub fn get_config_option(&self, path: &str) -> Option<Config>
Like get_config but returns None instead of an error.
Sourcepub fn get_list_option(&self, path: &str) -> Option<Vec<HoconValue>>
pub fn get_list_option(&self, path: &str) -> Option<Vec<HoconValue>>
Like get_list but returns None instead of an error.
Sourcepub fn get_duration(&self, path: &str) -> Result<Duration, ConfigError>
pub fn get_duration(&self, path: &str) -> Result<Duration, ConfigError>
Return the value at path as a Duration.
Accepts HOCON duration strings (e.g., "30 seconds", "100ms",
"2 hours"). Bare integers are interpreted as milliseconds.
Supported units: ns/nano/nanos/nanosecond/nanoseconds,
us/micro/micros/microsecond/microseconds,
ms/milli/millis/millisecond/milliseconds,
s/second/seconds, m/minute/minutes,
h/hour/hours, d/day/days, w/week/weeks.
Sourcepub fn get_duration_option(&self, path: &str) -> Option<Duration>
pub fn get_duration_option(&self, path: &str) -> Option<Duration>
Like get_duration but returns None instead of an error.
Sourcepub fn get_bytes(&self, path: &str) -> Result<i64, ConfigError>
pub fn get_bytes(&self, path: &str) -> Result<i64, ConfigError>
Return the value at path as a byte count (i64).
Accepts HOCON byte-size strings (e.g., "512 MB", "1 GiB").
Bare integers are returned as-is (assumed bytes).
Supported units: B/byte/bytes, K/KB/kilobyte/kilobytes,
KiB/kibibyte/kibibytes, M/MB/megabyte/megabytes,
MiB/mebibyte/mebibytes, G/GB/gigabyte/gigabytes,
GiB/gibibyte/gibibytes, T/TB/terabyte/terabytes,
TiB/tebibyte/tebibytes. Fractional numbers (e.g. 0.5M) are supported.
Sourcepub fn get_bytes_option(&self, path: &str) -> Option<i64>
pub fn get_bytes_option(&self, path: &str) -> Option<i64>
Like get_bytes but returns None instead of an error.
Sourcepub fn get_period(&self, path: &str) -> Result<Period, ConfigError>
pub fn get_period(&self, path: &str) -> Result<Period, ConfigError>
Return the value at path as a calendar Period.
Accepts HOCON period strings (e.g. "7d", "2w", "3m", "1y") or a bare
integer string, which is taken as days per HOCON.md L1321.
Supported units: d/day/days (default), w/week/weeks (× 7 days),
m/mo/month/months, y/year/years.
Negative values are permitted (matches Lightbend behaviour).
Sourcepub fn get_period_option(&self, path: &str) -> Option<Period>
pub fn get_period_option(&self, path: &str) -> Option<Period>
Like get_period but returns None instead of an error.
Sourcepub fn has(&self, path: &str) -> bool
pub fn has(&self, path: &str) -> bool
Return true if a value exists at the given dot-separated path.
Sourcepub fn with_fallback(&self, fallback: &Config) -> Config
pub fn with_fallback(&self, fallback: &Config) -> Config
Merge this config with a fallback. Keys present in self win;
missing keys are filled from fallback. Nested objects are deep-merged.
let app = hocon::parse(r#"server.port = 9090"#)?;
let defaults = hocon::parse(r#"server { host = "0.0.0.0", port = 8080 }"#)?;
let merged = app.with_fallback(&defaults);
assert_eq!(merged.get_i64("server.port")?, 9090); // app wins
assert_eq!(merged.get_string("server.host")?, "0.0.0.0"); // filled from defaultsMerge this config with a fallback. Receiver’s keys win; missing keys come from fallback. Nested objects are deep-merged.
Accepts both resolved and unresolved operands (E12 decision 5). Non-object collision captures fallback value as prior for S13a cross-layer self-reference. Result is resolved iff merged tree contains no placeholders.