pub struct Path(/* private fields */);Expand description
Represents a path to a field in a nested structure.
Examples: "root", "user.name", "items[3].id", "config.db.host"
§Examples
use diffo::Path;
let root = Path::root();
let user_name = root.field("user").field("name");
assert_eq!(user_name.as_str(), "user.name");
let item_id = root.field("items").index(3).field("id");
assert_eq!(item_id.as_str(), "items[3].id");Implementations§
Source§impl Path
impl Path
Sourcepub fn root() -> Self
pub fn root() -> Self
Create a root path.
§Examples
use diffo::Path;
let root = Path::root();
assert_eq!(root.as_str(), "");Sourcepub fn field(&self, name: &str) -> Self
pub fn field(&self, name: &str) -> Self
Append a field name to the path.
§Examples
use diffo::Path;
let path = Path::root().field("user").field("name");
assert_eq!(path.as_str(), "user.name");Sourcepub fn index(&self, idx: usize) -> Self
pub fn index(&self, idx: usize) -> Self
Append an array index to the path.
§Examples
use diffo::Path;
let path = Path::root().field("items").index(3);
assert_eq!(path.as_str(), "items[3]");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the path as a string slice.
§Examples
use diffo::Path;
let path = Path::root().field("user");
assert_eq!(path.as_str(), "user");Sourcepub fn to_json_pointer(&self) -> String
pub fn to_json_pointer(&self) -> String
Convert path to JSON Pointer format (RFC 6901).
§Examples
use diffo::Path;
let path = Path::root().field("user").field("name");
assert_eq!(path.to_json_pointer(), "/user/name");
let indexed = Path::root().field("items").index(0);
assert_eq!(indexed.to_json_pointer(), "/items/0");Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Calculate the depth of the path (number of components).
§Examples
use diffo::Path;
assert_eq!(Path::root().depth(), 0);
assert_eq!(Path::root().field("user").depth(), 1);
assert_eq!(Path::root().field("user").field("name").depth(), 2);
assert_eq!(Path::root().field("items").index(0).depth(), 2);Trait Implementations§
Source§impl Ord for Path
impl Ord for Path
Source§impl PartialOrd for Path
impl PartialOrd for Path
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 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