Skip to main content

try_parse_path

Function try_parse_path 

Source
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")]

An empty path returns an empty list. A malformed [...] index is an error (unclosed ] or a non-usize index).

§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

Returns PathParseError::UnclosedIndex when a [ has no matching ], or PathParseError::InvalidIndex when the brackets do not contain a usize.