pub struct Path { /* private fields */ }Expand description
A path from the document root to a node.
A flat list of segments: no recursion anywhere in its impls.
use oneyaml_core::{Path, PathSegment};
let p = Path::new().with_key("spec").with_key("ports").with_index(2);
assert_eq!(p.to_string(), "spec.ports[2]");
assert_eq!(p.segments().last(), Some(&PathSegment::Index(2)));
let mut q = p.clone();
q.push_key("port");
assert_eq!((q.len(), q.pop()), (4, Some(PathSegment::Key("port".into()))));
q.push_index(0);
assert_eq!(q.to_string(), "spec.ports[2][0]");
assert!(!q.is_empty() && Path::new().is_empty());Implementations§
Source§impl Path
impl Path
Sourcepub fn push_index(&mut self, index: usize)
pub fn push_index(&mut self, index: usize)
Appends a sequence-index segment.
Sourcepub fn with_key(self, key: impl Into<String>) -> Self
pub fn with_key(self, key: impl Into<String>) -> Self
Chainable Path::push_key.
Sourcepub fn with_index(self, index: usize) -> Self
pub fn with_index(self, index: usize) -> Self
Chainable Path::push_index.
Sourcepub fn pop(&mut self) -> Option<PathSegment>
pub fn pop(&mut self) -> Option<PathSegment>
Removes and returns the last segment.
Sourcepub fn segments(&self) -> &[PathSegment]
pub fn segments(&self) -> &[PathSegment]
The segments, root first.
Source§impl Path
impl Path
Sourcepub fn parse(s: &str) -> Result<Path, ParsePathError>
pub fn parse(s: &str) -> Result<Path, ParsePathError>
Parses a path string: the inverse of Display, so
Path::parse(&p.to_string()) == Ok(p) for every path.
The syntax:
- keys are separated by
.:spec.template.metadata; [n](decimal digits) is a sequence index:containers[0],[0]at the start,m[1][2]for nested sequences;- a key holding
.,[,],",\, white space or a control character, or an empty key, is written in double quotes, where\"is a quote,\\a backslash and\u{hex}any character:a."b.c"[0],""(the empty key),"say \"hi\""; .alone is the root path (whatDisplaywrites for it); the empty string is an error, so an empty key from user input never names the whole document.
Every key is text: [n] is always an index, and a.1 is the key
"1".
use oneyaml_core::{Path, path};
let p = Path::parse("spec.containers[0].\"app.kubernetes.io/name\"").unwrap();
assert_eq!(p, path!["spec", "containers", 0, "app.kubernetes.io/name"]);
assert_eq!(Path::parse(&p.to_string()), Ok(p));
assert_eq!(Path::parse("."), Ok(Path::new()));
assert_eq!(
Path::parse("").unwrap_err().reason(),
"an empty path: write `.` for the root"
);
let err = Path::parse("a..b").unwrap_err();
assert_eq!(err.offset(), 2);
assert_eq!(err.reason(), "expected a key after `.`");§Errors
ParsePathError, with the byte offset and the reason, for
anything else: the empty string, an empty segment (a..b, a., .a), a bare key
with a reserved character (a b, a]), an unclosed quote or [,
a non-digit or overflowing index, an unknown escape, or text right
after a quoted key or an index ("a"b, [0]x).
Trait Implementations§
impl Eq for Path
impl StructuralPartialEq for Path
Auto Trait Implementations§
impl Freeze for Path
impl RefUnwindSafe for Path
impl Send for Path
impl Sync for Path
impl Unpin for Path
impl UnsafeUnpin for Path
impl UnwindSafe for Path
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more