pub fn section_path(section: &str) -> String {
let mut path = String::new();
push_segment(&mut path, section);
path
}
pub fn child_path(parent: &str, key: &str) -> String {
if parent.is_empty() {
return section_path(key);
}
let mut path = format!("{parent}.");
push_segment(&mut path, key);
path
}
pub fn element_path(path: &str, index: usize) -> String {
format!("{path}[{index}]")
}
fn push_segment(path: &mut String, key: &str) {
if !key.is_empty()
&& key
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-')
{
path.push_str(key);
return;
}
path.push('"');
for ch in key.chars() {
match ch {
'"' => path.push_str("\\\""),
'\\' => path.push_str("\\\\"),
'\u{8}' => path.push_str("\\b"),
'\t' => path.push_str("\\t"),
'\n' => path.push_str("\\n"),
'\u{c}' => path.push_str("\\f"),
'\r' => path.push_str("\\r"),
'\0'..='\u{1f}' | '\u{7f}'..='\u{9f}' => {
path.push_str(&format!("\\u{:04X}", u32::from(ch)));
}
other => path.push(other),
}
}
path.push('"');
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paths_quote_only_non_bare_segments() {
for key in ["server", "a-Z_09", "123"] {
assert_eq!(section_path(key), key);
assert_eq!(child_path("App", key), format!("App.{key}"));
}
for key in [
"",
"my key",
"a.b",
"x[1]",
"\u{e9}",
"\"\\\n\t\r\u{8}\u{c}\0\u{1b}\u{7f}\u{80}\u{9f}\u{a0}",
] {
let quoted = section_path(key);
assert!(quoted.starts_with('"') && quoted.ends_with('"'));
let parsed: toml::Table = format!("{quoted} = 1").parse().unwrap();
assert_eq!(parsed.keys().next().unwrap(), key);
assert_eq!(child_path("App", key), format!("App.{quoted}"));
assert_eq!(child_path("", key), quoted);
}
assert_eq!(
child_path("App.with.dot", "my key"),
"App.with.dot.\"my key\""
);
assert_eq!(
element_path(&child_path("App", "upstream"), 1),
"App.upstream[1]"
);
assert_eq!(
child_path("App.upstream[1]", "port"),
"App.upstream[1].port"
);
}
#[test]
fn quoted_segments_use_canonical_escapes() {
assert_eq!(section_path(""), "\"\"");
assert_eq!(section_path("a\"b\\c"), r#""a\"b\\c""#);
assert_eq!(section_path("\u{8}\t\n\u{c}\r"), r#""\b\t\n\f\r""#);
assert_eq!(
section_path("\0\u{1}\u{1b}\u{1f}\u{7f}\u{80}\u{9f}"),
r#""\u0000\u0001\u001B\u001F\u007F\u0080\u009F""#
);
assert_eq!(
section_path("caf\u{e9} \u{a0}\u{1f600}"),
"\"caf\u{e9} \u{a0}\u{1f600}\""
);
}
}