Skip to main content

Crate hocon

Crate hocon 

Source
Expand description

§hocon

Full Lightbend HOCON specification-compliant parser for Rust.

§Quick Example

let config = hocon::parse(r#"
    server {
        host = "localhost"
        port = 8080
    }
"#)?;

assert_eq!(config.get_string("server.host")?, "localhost");
assert_eq!(config.get_i64("server.port")?, 8080);

§Parsing

  • parse – parse a HOCON string into a Config.
  • parse_file – parse a HOCON file. Include directives are resolved relative to the file’s directory.
  • parse_with_env / parse_file_with_env – parse with a custom environment variable map instead of inheriting the process environment.

§Accessing Values

Config provides typed getters that accept dot-separated paths:

MethodReturn type
Config::get_stringResult<String, ConfigError>
Config::get_i64Result<i64, ConfigError>
Config::get_f64Result<f64, ConfigError>
Config::get_boolResult<bool, ConfigError>
Config::get_configResult<Config, ConfigError>
Config::get_listResult<Vec<HoconValue>, ConfigError>
Config::get_durationResult<Duration, ConfigError>
Config::get_bytesResult<i64, ConfigError>

Each typed getter has an _option variant (e.g., Config::get_string_option) that returns Option<T> instead.

§Duration and Byte-Size Values

let config = hocon::parse(r#"
    timeout = 30 seconds
    max-upload = 512 MB
"#)?;

let timeout = config.get_duration("timeout")?;
let max_upload = config.get_bytes("max-upload")?;

Duration units: ns, us, ms, s/seconds, m/minutes, h/hours, d/days.

Byte-size units: B, KB, KiB, MB, MiB, GB, GiB, TB, TiB (and their long forms like megabytes, mebibytes).

§Serde Deserialization

With the serde feature enabled, deserialize a Config (or sub-config) into any type implementing serde::Deserialize:

use serde::Deserialize;

#[derive(Deserialize)]
struct Server {
    host: String,
    port: u16,
}

let server: Server = config.get_config("server")?.deserialize()?;

§Include Files

HOCON supports include directives to compose configuration from multiple files:

include "defaults.conf"

server.port = 9090  # override a value from defaults

When parsing with parse_file, include paths are resolved relative to the file being parsed.

§Error Types

  • HoconError – unified error returned by parse functions. Wraps:
    • ParseError – syntax errors during lexing or parsing (includes line/column).
    • ResolveError – substitution resolution failures, cycle detection.
    • std::io::Error – file I/O errors (top-level file read; include file errors appear as ResolveError).
  • ConfigError – missing keys or type mismatches when accessing values.

§HOCON Specification

For the full specification, see the Lightbend HOCON spec.

Re-exports§

pub use config::Config;
pub use error::ConfigError;
pub use error::HoconError;
pub use error::ParseError;
pub use error::ResolveError;
pub use value::HoconValue;
pub use value::ScalarType;
pub use value::ScalarValue;

Modules§

config
error
value

Functions§

parse
Parse a HOCON string into a Config.
parse_file
Parse a HOCON file into a Config.
parse_file_with_env
Parse a HOCON file with a custom environment variable map.
parse_with_env
Parse a HOCON string with a custom environment variable map.