pub struct XPath { /* private fields */ }Expand description
Cross Path allowing to recursively retrieve a FieldValue
from a structure implementing FieldGetter.
§Example:
use ::gene_derive::FieldGetter;
use ::gene::{XPath, FieldGetter, FieldValue, FieldNameIterator};
#[derive(FieldGetter)]
struct LogData
{
a: String,
b: i32,
c: f64,
}
#[derive(FieldGetter)]
struct LogEntry
{
name: String,
data: LogData,
}
let e = LogEntry{
name: "SomeEntry".into(),
data: LogData{
a: "SomeData".into(),
b: 42,
c: 24.0,
}
};
let p = XPath::parse(".name").unwrap();
assert_eq!(e.get_from_path(&p), Some("SomeEntry".into()));
let p = XPath::parse(".data.a").unwrap();
assert_eq!(e.get_from_path(&p), Some("SomeData".into()));
let p = XPath::parse(".data.b").unwrap();
assert_eq!(e.get_from_path(&p), Some(42.into()));
let p = XPath::parse(".data.c").unwrap();
assert_eq!(e.get_from_path(&p), Some(24.0.into()));Implementations§
Source§impl XPath
impl XPath
Sourcepub fn parse<S: AsRef<str>>(s: S) -> Result<Self, PathError>
pub fn parse<S: AsRef<str>>(s: S) -> Result<Self, PathError>
Parses a string into an XPath.
Creates an XPath from a string representation, validating the path structure
and extracting segments. The path string should use dot notation (e.g., .field.subfield)
to represent nested field access.
§Errors
Returns PathError if the path string is malformed or contains invalid characters.
§Examples
use gene::XPath;
let path = XPath::parse(".field.subfield").unwrap();
assert_eq!(path.to_string_lossy(), ".field.subfield");Sourcepub fn segments(&self) -> &[String]
pub fn segments(&self) -> &[String]
Returns a reference to the segments of this path.
Path segments are the individual components extracted from the path string.
For a path like .a.b.c, the segments will be ["a", "b", "c"].
The dot (.) character is used as the segment separator.
§Examples
use gene::XPath;
let path = XPath::parse(".field.subfield").unwrap();
let segments = path.segments();
assert_eq!(segments, &vec!["field".to_string(), "subfield".to_string()]);Sourcepub fn iter_segments(&self) -> Iter<'_, String>
pub fn iter_segments(&self) -> Iter<'_, String>
Returns an iterator over the segments of this path.
This provides a more efficient way to access path segments when you need to iterate over them rather than access the full vector. The iterator yields string references to each segment.
§Examples
use gene::XPath;
let path = XPath::parse(".field.subfield").unwrap();
let mut iter = path.iter_segments();
assert_eq!(iter.next(), Some(&"field".to_string()));
assert_eq!(iter.next(), Some(&"subfield".to_string()));
assert_eq!(iter.next(), None);Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Returns a borrowed string representation of this path.
This method provides efficient access to the original path string
without allocation. The returned Cow will typically be a borrowed
reference to the internal string data.
§Examples
use gene::XPath;
let path = XPath::parse(".field.subfield").unwrap();
assert_eq!(path.to_string_lossy(), ".field.subfield");Trait Implementations§
Source§impl<'f> From<&'f XPath> for FieldNameIterator<'f>
impl<'f> From<&'f XPath> for FieldNameIterator<'f>
impl Eq for XPath
Auto Trait Implementations§
impl Freeze for XPath
impl RefUnwindSafe for XPath
impl Send for XPath
impl Sync for XPath
impl Unpin for XPath
impl UnsafeUnpin for XPath
impl UnwindSafe for XPath
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.