Skip to main content

Config

Struct Config 

Source
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

Source

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).

Source

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.

Source

pub fn origin_description(&self) -> Option<&str>

The user-visible source name associated with this config, if any.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_string_option(&self, path: &str) -> Option<String>

Like get_string but returns None instead of an error.

Source

pub fn get_i64_option(&self, path: &str) -> Option<i64>

Like get_i64 but returns None instead of an error.

Source

pub fn get_f64_option(&self, path: &str) -> Option<f64>

Like get_f64 but returns None instead of an error.

Source

pub fn get_bool_option(&self, path: &str) -> Option<bool>

Like get_bool but returns None instead of an error.

Source

pub fn get_config_option(&self, path: &str) -> Option<Config>

Like get_config but returns None instead of an error.

Source

pub fn get_list_option(&self, path: &str) -> Option<Vec<HoconValue>>

Like get_list but returns None instead of an error.

Source

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.

Source

pub fn get_duration_option(&self, path: &str) -> Option<Duration>

Like get_duration but returns None instead of an error.

Source

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.

Source

pub fn get_bytes_option(&self, path: &str) -> Option<i64>

Like get_bytes but returns None instead of an error.

Source

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).

Source

pub fn get_period_option(&self, path: &str) -> Option<Period>

Like get_period but returns None instead of an error.

Source

pub fn has(&self, path: &str) -> bool

Return true if a value exists at the given dot-separated path.

Source

pub fn keys(&self) -> Vec<&str>

Return the top-level keys in insertion order.

Source

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

Merge 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.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.