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 aConfig.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:
| Method | Return type |
|---|---|
Config::get_string | Result<String, ConfigError> |
Config::get_i64 | Result<i64, ConfigError> |
Config::get_f64 | Result<f64, ConfigError> |
Config::get_bool | Result<bool, ConfigError> |
Config::get_config | Result<Config, ConfigError> |
Config::get_list | Result<Vec<HoconValue>, ConfigError> |
Config::get_duration | Result<Duration, ConfigError> |
Config::get_bytes | Result<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 defaultsWhen 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 asResolveError).
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§
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.