pub struct PathBoundary<Marker = ()> { /* private fields */ }Expand description
A path boundary that serves as the secure foundation for validated path operations.
SUMMARY:
Represent the trusted filesystem root for all strict and virtual path operations. All
StrictPath/VirtualPath values derived from a PathBoundary are guaranteed to remain
within this boundary.
EXAMPLE:
let boundary = PathBoundary::<()>::try_new_create("./data")?;
let file = boundary.strict_join("logs/app.log")?;
println!("{}", file.strictpath_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 rooted at restriction_path (which must already exist and be a directory).
SUMMARY: Create a boundary anchored at an existing directory (must exist and be a directory).
PARAMETERS:
restriction_path(AsRef<Path>): Existing directory to anchor the boundary.
RETURNS:
Result<PathBoundary<Marker>>: New boundary whose root is canonicalized and verified to exist.
ERRORS:
StrictPathError::InvalidRestriction: Root is missing, not a directory, or cannot be canonicalized.
EXAMPLE:
Uses AsRef<Path> for maximum ergonomics, including direct TempDir support for clean shadowing patterns:
use strict_path::PathBoundary;
let tmp_dir = tempfile::tempdir()?;
let tmp_dir = PathBoundary::<()>::try_new(tmp_dir)?; // Clean variable shadowingSourcepub fn try_new_create<P: AsRef<Path>>(root: P) -> Result<Self>
pub fn try_new_create<P: AsRef<Path>>(root: P) -> Result<Self>
Creates the directory if missing, then constructs a new PathBoundary.
SUMMARY: Ensure the root exists (create if missing) and construct a new boundary.
PARAMETERS:
root(AsRef<Path>): Directory to create if needed and use as boundary root.
RETURNS:
Result<PathBoundary<Marker>>: New boundary anchored atroot.
ERRORS:
StrictPathError::InvalidRestriction: Directory creation/canonicalization fails.
EXAMPLE:
Uses AsRef<Path> for maximum ergonomics, including direct TempDir support for clean shadowing patterns:
use strict_path::PathBoundary;
let tmp_dir = tempfile::tempdir()?;
let tmp_dir = PathBoundary::<()>::try_new_create(tmp_dir)?; // Clean variable shadowingSourcepub 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>>
SUMMARY:
Join a candidate path to the boundary and return a validated StrictPath.
PARAMETERS:
candidate_path(AsRef<Path>): Absolute or relative path to validate within this boundary.
RETURNS:
Result<StrictPath<Marker>>: Canonicalized, boundary-checked path.
ERRORS:
StrictPathError::WindowsShortName(windows),StrictPathError::PathResolutionError,StrictPathError::PathEscapesBoundary.
Sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Returns true if the PathBoundary root 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
SUMMARY:
Return the root path as &OsStr for 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 root system path.
Sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
SUMMARY: Return filesystem metadata for the boundary root.
Sourcepub fn strict_symlink(&self, link_path: &StrictPath<Marker>) -> Result<()>
pub fn strict_symlink(&self, link_path: &StrictPath<Marker>) -> Result<()>
SUMMARY:
Create a symbolic link at link_path pointing to this boundary’s root.
PARAMETERS:
link_path(&StrictPath<Marker>): Destination for the symlink, within the same boundary.
RETURNS:
io::Result<()>: Mirrors std semantics.
Sourcepub fn strict_hard_link(&self, link_path: &StrictPath<Marker>) -> Result<()>
pub fn strict_hard_link(&self, link_path: &StrictPath<Marker>) -> Result<()>
SUMMARY:
Create a hard link at link_path pointing to this boundary’s root.
PARAMETERS and RETURNS mirror strict_symlink.
Sourcepub fn read_dir(&self) -> Result<ReadDir>
pub fn read_dir(&self) -> Result<ReadDir>
SUMMARY: Read directory entries under the boundary root (discovery only).
Sourcepub fn remove_dir(&self) -> Result<()>
pub fn remove_dir(&self) -> Result<()>
SUMMARY: Remove the boundary root directory (non-recursive); fails if not empty.
Sourcepub fn remove_dir_all(&self) -> Result<()>
pub fn remove_dir_all(&self) -> Result<()>
SUMMARY: Recursively remove the boundary root directory and contents.
Sourcepub fn virtualize(self) -> VirtualRoot<Marker>
pub fn virtualize(self) -> VirtualRoot<Marker>
SUMMARY:
Convert this boundary into a VirtualRoot for virtual path operations.
Trait Implementations§
Source§impl<Marker> AsRef<Path> for PathBoundary<Marker>
impl<Marker> AsRef<Path> for PathBoundary<Marker>
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 temp_dir = tempfile::tempdir()?;
let safe_path = temp_dir.path().join("safe_dir");
let boundary: PathBoundary<()> = safe_path.to_string_lossy().parse()?;
assert!(safe_path.exists());