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

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.

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

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.

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

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · 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: &Config) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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.
Source§

impl StructuralPartialEq for Config

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.