Skip to main content

Path

Struct Path 

Source
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

Source

pub const fn new() -> Self

The root path (no segments).

Source

pub fn push_key(&mut self, key: impl Into<String>)

Appends a mapping-key segment.

Source

pub fn push_index(&mut self, index: usize)

Appends a sequence-index segment.

Source

pub fn with_key(self, key: impl Into<String>) -> Self

Chainable Path::push_key.

Source

pub fn with_index(self, index: usize) -> Self

Chainable Path::push_index.

Source

pub fn pop(&mut self) -> Option<PathSegment>

Removes and returns the last segment.

Source

pub fn segments(&self) -> &[PathSegment]

The segments, root first.

Source

pub fn is_empty(&self) -> bool

true for the root path.

Source

pub fn len(&self) -> usize

Number of segments.

Source§

impl Path

Source

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 (what Display writes 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§

Source§

impl Clone for Path

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Path

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Path

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Path

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

a.b[2].c; keys that are empty or contain .[]"\, whitespace, or control characters are written as "…" with \"/\\ escaped; the root path is written as ..

Source§

impl Eq for Path

Source§

impl FromStr for Path

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Source§

type Err = ParsePathError

The associated error which can be returned from parsing.
Source§

impl Hash for Path

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Path

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.