pub struct Path(/* private fields */);Expand description
A broadcast path that provides safe prefix matching operations.
This type wraps a String but provides path-aware operations that respect delimiter boundaries, preventing issues like “foo” matching “foobar”.
Paths are automatically trimmed of leading and trailing slashes on creation, making all slashes implicit at boundaries.
§Examples
use moq_lite::{Path, PathRef};
// Creation automatically trims slashes
let path1 = Path::new("/foo/bar/");
let path2 = Path::new("foo/bar");
assert_eq!(path1, path2);
// Methods accept both &str and &Path via PathRef
let base = Path::new("api/v1");
assert!(base.has_prefix("api"));
assert!(base.has_prefix(&Path::new("api/v1")));
let joined = base.join("users");
assert_eq!(joined.as_str(), "api/v1/users");Implementations§
Source§impl Path
impl Path
Sourcepub fn new<S: Into<String>>(s: S) -> Self
pub fn new<S: Into<String>>(s: S) -> Self
Create a new Path from a string.
Leading and trailing slashes are automatically trimmed.
Sourcepub fn has_prefix<'a>(&self, prefix: impl Into<PathRef<'a>>) -> bool
pub fn has_prefix<'a>(&self, prefix: impl Into<PathRef<'a>>) -> bool
Check if this path has the given prefix, respecting path boundaries.
Unlike String::starts_with, this ensures that “foo” does not match “foobar”. The prefix must either:
- Be exactly equal to this path
- Be followed by a ‘/’ delimiter in the original path
- Be empty (matches everything)
§Examples
use moq_lite::Path;
let path = Path::new("foo/bar");
assert!(path.has_prefix("foo"));
assert!(path.has_prefix(&Path::new("foo")));
assert!(path.has_prefix("foo/"));
assert!(!path.has_prefix("fo"));
let path = Path::new("foobar");
assert!(!path.has_prefix("foo"));Sourcepub fn strip_prefix<'a>(
&'a self,
prefix: impl Into<PathRef<'a>>,
) -> Option<PathRef<'a>>
pub fn strip_prefix<'a>( &'a self, prefix: impl Into<PathRef<'a>>, ) -> Option<PathRef<'a>>
Strip the given prefix from this path, returning the suffix.
Returns None if the prefix doesn’t match according to has_prefix rules.
§Examples
use moq_lite::Path;
let path = Path::new("foo/bar/baz");
let suffix = path.strip_prefix("foo").unwrap();
assert_eq!(suffix.as_str(), "bar/baz");
let suffix = path.strip_prefix(&Path::new("foo/")).unwrap();
assert_eq!(suffix.as_str(), "bar/baz");Sourcepub fn join<'a>(&self, other: impl Into<PathRef<'a>>) -> Path
pub fn join<'a>(&self, other: impl Into<PathRef<'a>>) -> Path
Join this path with another path component.
§Examples
use moq_lite::Path;
let base = Path::new("foo");
let joined = base.join("bar");
assert_eq!(joined.as_str(), "foo/bar");
let joined = base.join(&Path::new("bar"));
assert_eq!(joined.as_str(), "foo/bar");