Path

Struct Path 

Source
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

Source

pub fn root() -> Self

Creates an empty root path.

§Examples
use domainstack::Path;

let path = Path::root();
assert_eq!(path.to_string(), "");
Source

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");
Source

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");
Source

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"));
Source

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);
Source

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");
Source

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]");

Trait Implementations§

Source§

impl Clone for Path

Source§

fn clone(&self) -> Path

Returns a duplicate of the value. Read more
1.0.0 · 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 Display for Path

Source§

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

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

impl From<&'static str> for Path

Source§

fn from(s: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Path

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
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: &Path) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Path

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 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.