pub trait PathResolver: Send + Sync {
// Required methods
fn canonicalize(&self, path: &Path, fs: &dyn Fs) -> Result<PathBuf, FsError>;
fn soft_canonicalize(
&self,
path: &Path,
fs: &dyn Fs,
) -> Result<PathBuf, FsError>;
}Expand description
Strategy trait for path resolution algorithms.
Encapsulates how paths are normalized, symlinks are followed,
and ../. components are resolved.
§Thread Safety
All implementations must be Send + Sync to support concurrent access.
§Object Safety
Uses &dyn Fs to remain object-safe, enabling runtime resolver selection.
§Symlink Handling
The trait accepts &dyn Fs for object safety. Implementations that need
symlink awareness can attempt to downcast to check for FsLink capabilities.
All built-in virtual backends implement FsLink, so symlink-aware resolution
works out of the box. For backends without FsLink, resolution still works
but treats all entries as non-symlinks.
§Implementors
IterativeResolver(default inanyfs): Walks path component by componentNoOpResolver(inanyfs): Pass-through forSelfResolvingbackendsCachingResolver(inanyfs): LRU cache wrapper for any resolver (with TTL expiration)
§Example
use anyfs_backend::{PathResolver, Fs, FsError};
use std::path::{Path, PathBuf};
struct MyCustomResolver;
impl PathResolver for MyCustomResolver {
fn canonicalize(&self, path: &Path, _fs: &dyn Fs) -> Result<PathBuf, FsError> {
// Custom resolution logic
Ok(path.to_path_buf())
}
fn soft_canonicalize(&self, path: &Path, _fs: &dyn Fs) -> Result<PathBuf, FsError> {
// Custom resolution logic (allows non-existent final component)
Ok(path.to_path_buf())
}
}Required Methods§
Sourcefn canonicalize(&self, path: &Path, fs: &dyn Fs) -> Result<PathBuf, FsError>
fn canonicalize(&self, path: &Path, fs: &dyn Fs) -> Result<PathBuf, FsError>
Resolve path to canonical form.
All symlinks are resolved, . and .. are normalized,
and all path components must exist.
§Arguments
path- The path to canonicalizefs- The filesystem to query for path resolution
§Returns
The fully resolved canonical path.
§Errors
FsError::NotFound- A component doesn’t existFsError::InvalidData- Symlink loop detected (circular symlinks)
Sourcefn soft_canonicalize(
&self,
path: &Path,
fs: &dyn Fs,
) -> Result<PathBuf, FsError>
fn soft_canonicalize( &self, path: &Path, fs: &dyn Fs, ) -> Result<PathBuf, FsError>
Like canonicalize, but allows non-existent final component.
Resolves parent path fully, appends final component lexically.
This is useful for write() operations where the target file
doesn’t exist yet.
§Arguments
path- The path to soft-canonicalizefs- The filesystem to query for path resolution
§Returns
The resolved path with the final component appended lexically.
§Errors
FsError::NotFound- A parent component doesn’t existFsError::InvalidData- Symlink loop detected