pub mod conf;
pub mod properties_parser;
pub mod ini_parser;
#[cfg(any(feature = "json", feature = "jsonx"))]
pub mod json_parser;
#[cfg(feature = "xml")]
pub mod xml_parser;
#[cfg(feature = "hcl")]
pub mod hcl_parser;
#[cfg(feature = "toml")]
pub mod toml_parser;
#[cfg(feature = "noml")]
pub mod noml_parser;
#[cfg(any(feature = "noml", feature = "toml"))]
mod noml_guard;
#[cfg(feature = "noml")]
mod noml_resolve;
use crate::error::Result;
use crate::load::LoadOptions;
use crate::value::Value;
use std::path::Path;
pub fn parse_string(source: &str, format: Option<&str>) -> Result<Value> {
parse_string_with(source, format, None, &LoadOptions::default())
}
pub fn parse_string_strict(source: &str, format: Option<&str>) -> Result<Value> {
parse_string_with(source, format, None, &LoadOptions::default().strict(true))
}
pub(crate) fn parse_string_with(
source: &str,
format: Option<&str>,
file: Option<&Path>,
options: &LoadOptions,
) -> Result<Value> {
#[cfg(not(feature = "noml"))]
let _ = (file, options);
let detected_format = format.unwrap_or_else(|| detect_format(source));
match detected_format {
"conf" => conf::parse(source),
"properties" => properties_parser::parse(source),
"ini" => ini_parser::parse(source),
#[cfg(any(feature = "json", feature = "jsonx"))]
"json" => json_parser::parse(source),
#[cfg(feature = "xml")]
"xml" => xml_parser::parse(source),
#[cfg(feature = "hcl")]
"hcl" => hcl_parser::parse(source),
#[cfg(feature = "noml")]
"noml" => noml_parser::parse_with(source, file, options),
#[cfg(feature = "toml")]
"toml" => toml_parser::parse(source),
_ => {
#[cfg(not(any(feature = "json", feature = "jsonx")))]
if detected_format == "json" {
return Err(crate::error::Error::feature_not_enabled("json"));
}
#[cfg(not(feature = "xml"))]
if detected_format == "xml" {
return Err(crate::error::Error::feature_not_enabled("xml"));
}
#[cfg(not(feature = "hcl"))]
if detected_format == "hcl" {
return Err(crate::error::Error::feature_not_enabled("hcl"));
}
#[cfg(not(feature = "noml"))]
if detected_format == "noml" {
return Err(crate::error::Error::feature_not_enabled("noml"));
}
#[cfg(not(feature = "toml"))]
if detected_format == "toml" {
return Err(crate::error::Error::feature_not_enabled("toml"));
}
conf::parse(source)
}
}
}
pub(crate) fn file_format(path: &Path, content: &str) -> &'static str {
detect_format_from_path(path).unwrap_or_else(|| detect_format(content))
}
pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Value> {
let path = path.as_ref();
let options = LoadOptions::default();
let content = crate::load::read_file(path, options.max_file_size)?;
parse_string_with(
&content,
Some(file_format(path, &content)),
Some(path),
&options,
)
}
#[cfg(feature = "async")]
pub async fn parse_file_async<P: AsRef<Path>>(path: P) -> Result<Value> {
let path = path.as_ref();
let options = LoadOptions::default();
let content = crate::load::read_file_async(path, options.max_file_size).await?;
parse_string_with(
&content,
Some(file_format(path, &content)),
Some(path),
&options,
)
}
pub fn detect_format_from_path(path: &Path) -> Option<&'static str> {
path.extension()
.and_then(|ext| ext.to_str())
.map(|ext| match ext.to_lowercase().as_str() {
"conf" | "config" | "cfg" => "conf",
"properties" => "properties",
"ini" => "ini",
"toml" => "toml",
"json" => "json",
"noml" => "noml",
"xml" => "xml",
"hcl" | "tf" => "hcl", _ => "conf", })
}
pub fn detect_format(content: &str) -> &'static str {
let trimmed = content.trim_start_matches('\u{feff}').trim();
if trimmed.starts_with("<?xml")
|| (trimmed.starts_with('<') && (trimmed.contains("</") || trimmed.contains("/>")))
{
return "xml";
}
if trimmed.starts_with('{') {
return "json";
}
if trimmed.starts_with('[') {
let first_line = trimmed.lines().next().unwrap_or("").trim();
if !is_section_header(first_line) {
return "json";
}
}
if content
.lines()
.any(|line| line.trim_start().starts_with(';'))
{
return "ini";
}
"conf"
}
fn is_section_header(line: &str) -> bool {
let inner = match line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
Some(inner) => inner.trim(),
None => return false,
};
!inner.is_empty()
&& inner
.chars()
.all(|c| c.is_alphanumeric() || matches!(c, '_' | '-' | '.' | ' '))
}
pub(crate) fn decimal_float(text: &str) -> Option<f64> {
let digits = text.strip_prefix(['+', '-']).unwrap_or(text);
let (mantissa, exponent) = match digits.find(['e', 'E']) {
Some(i) => (&digits[..i], Some(&digits[i + 1..])),
None => (digits, None),
};
let (whole, fraction) = match mantissa.find('.') {
Some(i) => (&mantissa[..i], Some(&mantissa[i + 1..])),
None => (mantissa, None),
};
let all_digits = |s: &str| s.bytes().all(|b| b.is_ascii_digit());
let mantissa_ok = all_digits(whole)
&& fraction.map_or(true, all_digits)
&& !(whole.is_empty() && fraction.map_or(true, str::is_empty));
let exponent_ok = exponent.map_or(true, |e| {
let e = e.strip_prefix(['+', '-']).unwrap_or(e);
!e.is_empty() && all_digits(e)
});
if !mantissa_ok || !exponent_ok || (fraction.is_none() && exponent.is_none()) {
return None;
}
text.parse::<f64>().ok().filter(|f| f.is_finite())
}