Skip to main content

hocon/adapters/
properties.rs

1//! `java.util.Properties` files as HOCON config.
2
3use super::{config_from_object, AdapterError};
4use crate::properties::properties_to_hocon;
5use crate::Config;
6
7/// Read Properties-syntax text.
8///
9/// Shares its syntax layer with `include "x.properties"`, so the two cannot
10/// drift apart. Values are all strings, and a `${a.b}` among them stays that
11/// literal text (spec F0.2, F2.2).
12pub fn parse(input: &str, origin: Option<&str>) -> Result<Config, AdapterError> {
13    let value = properties_to_hocon(super::strip_bom(input)).map_err(AdapterError::new)?;
14    Ok(config_from_object(value, origin))
15}
16
17/// Read a Properties file, using its path as the origin description.
18pub fn parse_file(path: impl AsRef<std::path::Path>) -> Result<Config, AdapterError> {
19    let path = path.as_ref();
20    let text = std::fs::read_to_string(path)
21        .map_err(|e| AdapterError::new(format!("properties: {}: {e}", path.display())))?;
22    parse(&text, Some(&path.display().to_string()))
23}