pub struct PathBoundary<Marker = ()> { /* private fields */ }Expand description
A path boundary that serves as the secure foundation for validated path operations.
Represent the trusted filesystem boundary directory for all strict and virtual path
operations. All StrictPath/VirtualPath values derived from a PathBoundary are
guaranteed to remain within this boundary.
§Examples
let data_dir = PathBoundary::<()>::try_new_create("./data")?;
// Untrusted input from request/CLI/config/etc.
let requested_file = "logs/app.log";
let file = data_dir.strict_join(requested_file)?;
let file_display = file.strictpath_display();
println!("{file_display}");Implementations§
Source§impl<Marker> PathBoundary<Marker>
impl<Marker> PathBoundary<Marker>
Sourcepub fn try_new<P: AsRef<Path>>(restriction_path: P) -> Result<Self>
pub fn try_new<P: AsRef<Path>>(restriction_path: P) -> Result<Self>
Creates a new PathBoundary anchored at restriction_path (which must already exist and be a directory).
Create a boundary anchored at an existing directory (must exist and be a directory).
§Errors
StrictPathError::InvalidRestriction: Boundary directory is missing, not a directory, or cannot be canonicalized.
§Examples
use strict_path::PathBoundary;
let data_dir = PathBoundary::<()>::try_new("./data")?;Sourcepub fn try_new_create<P: AsRef<Path>>(boundary_dir: P) -> Result<Self>
pub fn try_new_create<P: AsRef<Path>>(boundary_dir: P) -> Result<Self>
Creates the directory if missing, then constructs a new PathBoundary.
Ensure the boundary directory exists (create if missing) and construct a new boundary.
§Errors
StrictPathError::InvalidRestriction: Directory creation/canonicalization fails.
§Examples
use strict_path::PathBoundary;
let data_dir = PathBoundary::<()>::try_new_create("./data")?;Sourcepub fn strict_join(
&self,
candidate_path: impl AsRef<Path>,
) -> Result<StrictPath<Marker>>
pub fn strict_join( &self, candidate_path: impl AsRef<Path>, ) -> Result<StrictPath<Marker>>
Join a candidate path to the boundary and return a validated StrictPath.
§Errors
StrictPathError::PathResolutionError,StrictPathError::PathEscapesBoundary.
Sourcepub fn change_marker<NewMarker>(self) -> PathBoundary<NewMarker>
pub fn change_marker<NewMarker>(self) -> PathBoundary<NewMarker>
Consume this boundary and substitute a new marker type.
Mirrors crate::StrictPath::change_marker and crate::VirtualPath::change_marker, enabling
marker transformation after authorization checks. Use this when encoding proven
authorization into the type system (e.g., after validating a user’s permissions).
The consumption makes marker changes explicit during code review.
§Examples
struct ReadOnly;
struct ReadWrite;
let read_only_dir: PathBoundary<ReadOnly> = PathBoundary::try_new_create("./data")?;
// After authorization check...
let write_access_dir: PathBoundary<ReadWrite> = read_only_dir.change_marker();Sourcepub fn into_strictpath(self) -> Result<StrictPath<Marker>>
pub fn into_strictpath(self) -> Result<StrictPath<Marker>>
Consume this boundary and return a StrictPath anchored at the boundary directory.
§Errors
StrictPathError::PathResolutionError: Canonicalization fails (directory removed or inaccessible).StrictPathError::PathEscapesBoundary: Guard against race conditions that move the directory.
§Examples
let data_dir: PathBoundary = PathBoundary::try_new_create("./data")?;
let data_path: StrictPath = data_dir.into_strictpath()?;
assert!(data_path.is_dir());Sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Returns true if the PathBoundary directory exists.
This is always true for a constructed PathBoundary, but we query the filesystem for robustness.
Sourcepub fn interop_path(&self) -> &OsStr
pub fn interop_path(&self) -> &OsStr
Return the boundary directory path as &OsStr for unavoidable third-party AsRef<Path> interop (no allocation).
Sourcepub fn strictpath_display(&self) -> Display<'_>
pub fn strictpath_display(&self) -> Display<'_>
Returns a Display wrapper that shows the PathBoundary directory system path.
Sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Return filesystem metadata for the boundary directory.
Sourcepub fn strict_symlink<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
pub fn strict_symlink<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
Create a symbolic link at link_path pointing to this boundary’s directory.
Sourcepub fn strict_hard_link<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
pub fn strict_hard_link<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
Create a hard link at link_path pointing to this boundary’s directory.
Accepts the same link_path: impl AsRef<Path> parameter as strict_symlink and returns io::Result<()>.
Sourcepub fn read_dir(&self) -> Result<ReadDir>
pub fn read_dir(&self) -> Result<ReadDir>
Read directory entries under the boundary directory (discovery only).
Sourcepub fn strict_read_dir(&self) -> Result<BoundaryReadDir<'_, Marker>>
pub fn strict_read_dir(&self) -> Result<BoundaryReadDir<'_, Marker>>
Iterate directory entries under the boundary, yielding validated StrictPath values.
Unlike read_dir() which returns raw std::fs::DirEntry values requiring manual
re-validation, this method yields StrictPath entries directly. Each entry is
automatically validated through strict_join() so you can use it immediately
for I/O operations without additional validation.
§Examples
use strict_path::PathBoundary;
let data_dir: PathBoundary = PathBoundary::try_new(temp.path())?;
// Auto-validated iteration - no manual re-join needed!
for entry in data_dir.strict_read_dir()? {
let child = entry?;
println!("Found: {}", child.strictpath_display());
}Sourcepub fn remove_dir(&self) -> Result<()>
pub fn remove_dir(&self) -> Result<()>
Remove the boundary directory (non-recursive); fails if not empty.
Sourcepub fn remove_dir_all(&self) -> Result<()>
pub fn remove_dir_all(&self) -> Result<()>
Recursively remove the boundary directory and its contents.
Sourcepub fn virtualize(self) -> VirtualRoot<Marker>
pub fn virtualize(self) -> VirtualRoot<Marker>
Convert this boundary into a VirtualRoot for virtual path operations.
Trait Implementations§
Source§impl<Marker> Clone for PathBoundary<Marker>
impl<Marker> Clone for PathBoundary<Marker>
Source§impl<Marker> Debug for PathBoundary<Marker>
impl<Marker> Debug for PathBoundary<Marker>
Source§impl<Marker: Default> FromStr for PathBoundary<Marker>
impl<Marker: Default> FromStr for PathBoundary<Marker>
Source§fn from_str(path: &str) -> Result<Self, Self::Err>
fn from_str(path: &str) -> Result<Self, Self::Err>
Parse a PathBoundary from a string path for universal ergonomics.
Creates the directory if it doesn’t exist, enabling seamless integration with any string-parsing context (clap, config files, environment variables, etc.):
let data_dir: PathBoundary<()> = "./data".parse()?;
assert!(data_dir.exists());Source§type Err = StrictPathError
type Err = StrictPathError
Source§impl<Marker> Hash for PathBoundary<Marker>
impl<Marker> Hash for PathBoundary<Marker>
Source§impl<Marker> Ord for PathBoundary<Marker>
impl<Marker> Ord for PathBoundary<Marker>
Source§impl<Marker> PartialEq<&Path> for PathBoundary<Marker>
impl<Marker> PartialEq<&Path> for PathBoundary<Marker>
Source§impl<Marker> PartialEq<Path> for PathBoundary<Marker>
impl<Marker> PartialEq<Path> for PathBoundary<Marker>
Source§impl<M1, M2> PartialEq<PathBoundary<M2>> for PathBoundary<M1>
impl<M1, M2> PartialEq<PathBoundary<M2>> for PathBoundary<M1>
Source§impl<M1, M2> PartialEq<PathBoundary<M2>> for VirtualRoot<M1>
impl<M1, M2> PartialEq<PathBoundary<M2>> for VirtualRoot<M1>
Source§impl<Marker> PartialEq<PathBuf> for PathBoundary<Marker>
impl<Marker> PartialEq<PathBuf> for PathBoundary<Marker>
Source§impl<M1, M2> PartialEq<VirtualRoot<M2>> for PathBoundary<M1>
Available on crate feature virtual-path only.
impl<M1, M2> PartialEq<VirtualRoot<M2>> for PathBoundary<M1>
virtual-path only.