pub struct Path(/* private fields */);Expand description
Represents a path to a field in a nested structure.
Paths are used to identify which field caused a validation error in nested and collection structures. They support dot notation for nested fields and bracket notation for array indices.
§Examples
use domainstack::Path;
// Simple field path
let path = Path::root().field("email");
assert_eq!(path.to_string(), "email");
// Nested path
let path = Path::root().field("user").field("email");
assert_eq!(path.to_string(), "user.email");
// Collection path
let path = Path::root().field("items").index(0).field("name");
assert_eq!(path.to_string(), "items[0].name");§Memory Management
Path uses Arc<str> for field names, providing:
- No memory leaks: Reference counting ensures proper cleanup
- Efficient cloning: Cloning a path is cheap (just incrementing reference counts)
- Shared ownership: Multiple errors can reference the same field names
Field names from compile-time literals ("email") are converted to Arc<str>
on first use and reference-counted thereafter.
Implementations§
Source§impl Path
impl Path
Sourcepub fn root() -> Self
pub fn root() -> Self
Creates an empty root path.
§Examples
use domainstack::Path;
let path = Path::root();
assert_eq!(path.to_string(), "");Sourcepub fn field(self, name: impl Into<Arc<str>>) -> Self
pub fn field(self, name: impl Into<Arc<str>>) -> Self
Appends a field name to the path.
§Examples
use domainstack::Path;
let path = Path::root().field("email");
assert_eq!(path.to_string(), "email");
let nested = Path::root().field("user").field("email");
assert_eq!(nested.to_string(), "user.email");Sourcepub fn index(self, idx: usize) -> Self
pub fn index(self, idx: usize) -> Self
Appends an array index to the path.
§Examples
use domainstack::Path;
let path = Path::root().field("items").index(0);
assert_eq!(path.to_string(), "items[0]");
let nested = Path::root().field("items").index(0).field("name");
assert_eq!(nested.to_string(), "items[0].name");Sourcepub fn parse(s: &str) -> Self
pub fn parse(s: &str) -> Self
Parses a path from a string representation.
Uses Arc<str> for field names, ensuring proper memory management
without leaks. Field names are reference-counted and cleaned up
when no longer needed.
§Examples
use domainstack::Path;
let path = Path::parse("user.email");
assert_eq!(path, Path::root().field("user").field("email"));
let with_index = Path::parse("items[0].name");
assert_eq!(with_index, Path::root().field("items").index(0).field("name"));Sourcepub fn segments(&self) -> &[PathSegment]
pub fn segments(&self) -> &[PathSegment]
Returns a slice of the path segments.
§Examples
use domainstack::{Path, PathSegment};
let path = Path::root().field("user").index(0).field("name");
assert_eq!(path.segments().len(), 3);Sourcepub fn push_field(&mut self, name: impl Into<Arc<str>>)
pub fn push_field(&mut self, name: impl Into<Arc<str>>)
Pushes a field segment to the path.
§Examples
use domainstack::Path;
let mut path = Path::root();
path.push_field("email");
assert_eq!(path.to_string(), "email");Sourcepub fn push_index(&mut self, idx: usize)
pub fn push_index(&mut self, idx: usize)
Pushes an index segment to the path.
§Examples
use domainstack::Path;
let mut path = Path::root();
path.push_field("items");
path.push_index(0);
assert_eq!(path.to_string(), "items[0]");