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").
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.
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 scalar value (string, number, boolean,
or null). Returns ConfigError if the path is missing or the value
is an Object or Array.
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.
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 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 defaults