PathResolver

Trait PathResolver 

Source
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.

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 in anyfs): Walks path component by component
  • NoOpResolver (in anyfs): Pass-through for SelfResolving backends
  • CachingResolver (in anyfs): 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§

Source

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 canonicalize
  • fs - The filesystem to query for path resolution
§Returns

The fully resolved canonical path.

§Errors
Source

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-canonicalize
  • fs - The filesystem to query for path resolution
§Returns

The resolved path with the final component appended lexically.

§Errors

Implementors§