pub fn try_parse_path(path: &str) -> Result<Vec<PathSegment>, PathParseError>Expand description
Parse a path string into components.
Supports multiple syntaxes:
- Dot notation:
"server.host"→[Key("server"), Key("host")] - Array indices with brackets:
"items[0].name"→[Key("items"), Index(0), Key("name")] - Array indices with dots:
"items.0.name"→[Key("items"), Index(0), Key("name")] - Escaped dots:
"key\\.with\\.dots"→[Key("key.with.dots")] - Escaped digits are keys:
"items.\\0"->[Key("items"), Key("0")] - Digits with leading zeros are keys:
"zip.01"->[Key("zip"), Key("01")]
An index is ASCII digits only, so "items.+1" is the key "+1" and
"items[+1]" is an error. A [...] index may start the path and may
be followed only by ., another [...] or the end of the path.
An empty path returns an empty list; any other path yields at least one segment or an error.
§Examples
use yaml_edit::path::{try_parse_path, PathSegment};
let segments = try_parse_path("server.host").unwrap();
assert_eq!(segments, vec![
PathSegment::Key("server".to_string()),
PathSegment::Key("host".to_string())
]);
let segments = try_parse_path("items[0].name").unwrap();
assert_eq!(segments, vec![
PathSegment::Key("items".to_string()),
PathSegment::Index(0),
PathSegment::Key("name".to_string())
]);
let segments = try_parse_path("items.0").unwrap();
assert_eq!(segments, vec![
PathSegment::Key("items".to_string()),
PathSegment::Index(0)
]);§Errors
PathParseError::UnclosedIndexwhen a[has no matching].PathParseError::InvalidIndexwhen the brackets do not contain a decimalusize.PathParseError::TrailingBackslashwhen the path ends in an unescaped\\.PathParseError::EmptySegmentfora..b,.a,a.ora.[0].PathParseError::MissingSeparatorfora[0]b.