pub struct HoconLoader { /* private fields */ }
Expand description

Helper to load an HOCON file. This is used to set up the HOCON loader’s option, like strict mode, disabling system environment, and to buffer several documents.

Strict mode

If strict mode is enabled with strict(), loading a document will return the first error encountered. Otherwise, most errors will be wrapped in a Hocon::BadValue.

Usage


let mut loader = HoconLoader::new()         // Creating new loader with default configuration
    .no_system()                            // Disable substituting from system environment
    .no_url_include();                      // Disable including files from URLs

let default_values = r#"{ a = 7 }"#;
loader = loader.load_str(default_values)?   // Load values from a string
    .load_file("tests/data/basic.conf")?    // Load first file
    .load_file("tests/data/test01.conf")?;  // Load another file

let hocon = loader.hocon()?;                // Create the Hocon document from the loaded sources

Implementations

New HoconLoader with default configuration

Disable System environment substitutions

Example HOCON document
"system" : {
    "home"  : ${HOME},
    "pwd"   : ${PWD},
    "shell" : ${SHELL},
    "lang"  : ${LANG},
}

with system:

assert_eq!(
    HoconLoader::new().load_str(example)?.hocon()?["system"]["shell"],
    Hocon::String(String::from("/bin/bash"))
);

without system:

assert_eq!(
    HoconLoader::new().no_system().load_str(example)?.hocon()?["system"]["shell"],
    Hocon::BadValue(Error::KeyNotFound { key: String::from("SHELL") })
);

Disable loading included files from external urls.

Example HOCON document
include url("https://raw.githubusercontent.com/mockersf/hocon.rs/master/tests/data/basic.conf")

with url include:

assert_eq!(
    HoconLoader::new().load_file("tests/data/include_url.conf")?.hocon()?["d"],
    Hocon::Boolean(true)
);

without url include:

assert_eq!(
    HoconLoader::new().no_url_include().load_file("tests/data/include_url.conf")?.hocon()?["d"],
    Hocon::BadValue(Error::MissingKey)
);
Feature

This method depends on feature url-support

Sets the HOCON loader to return the first Error encoutered instead of wrapping it in a Hocon::BadValue and continuing parsing

Example HOCON document
{
    a = ${b}
}

in permissive mode:

assert_eq!(
    HoconLoader::new().load_str(example)?.hocon()?["a"],
    Hocon::BadValue(Error::KeyNotFound { key: String::from("b") })
);

in strict mode:

assert_eq!(
    HoconLoader::new().strict().load_str(example)?.hocon(),
    Err(Error::KeyNotFound { key: String::from("b") })
);

Set a new maximum include depth, by default 10

Load a string containing an Hocon document. Includes are not supported when loading from a string

Errors
Additional errors in strict mode

Load the HOCON configuration file containing an Hocon document

Errors
Additional errors in strict mode

Load the documents as HOCON

Errors in strict mode

Deserialize the loaded documents to the target type

Errors
  • Error::Deserialization if there was a serde error during deserialization (missing required field, type issue, …)
Additional errors in strict mode

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more