pub struct FilePath<T> { /* private fields */ }Expand description
Generic file path value object with type-safe path categories
§Purpose
Type-safe file path that provides:
- Compile-time path category enforcement (Input vs Output vs Temp)
- Shared validation and utility methods
- Zero-cost abstractions with phantom types
- Extensible design for new path categories
§Generic Benefits
- Type Safety: Cannot mix input and output paths at compile time
- Code Reuse: Shared implementation for all path types
- Extensibility: Easy to add new path categories
- Zero Cost: Phantom types have no runtime overhead
§Use Cases
- Pipeline input/output path specification
- Temporary file management
- Configuration file paths
- Log file paths
§Cross-Language Mapping
- Rust:
FilePath<T>with marker types
§Examples
- Go: Separate types with shared interface
- JSON: String representation with type hints
- SQLite: TEXT column with category metadata
Implementations§
Source§impl<T: PathCategory> FilePath<T>
impl<T: PathCategory> FilePath<T>
Sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<Self, PipelineError>
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, PipelineError>
Creates a new file path with category-specific validation
§Purpose
Creates a type-safe file path with compile-time category enforcement. Uses phantom types to prevent mixing different path categories at compile time. # Why Type-safe paths provide:
- Compile-time prevention of input/output path mixing
- Category-specific validation rules
- Zero-cost abstractions with phantom types
- Clear API contracts for path requirements
§Arguments
path- Path to validate (can be&str,String,Path,PathBuf)
§Returns
Ok(FilePath<T>)- Validated path with category typeErr(PipelineError::InvalidConfiguration)- Validation failed
§Errors
Returns PipelineError::InvalidConfiguration when:
- Path is empty
- Path contains null bytes
- Path exceeds 4096 characters
- Category-specific validation fails
§Examples
Sourcepub fn parse(path: &str) -> Result<Self, PipelineError>
pub fn parse(path: &str) -> Result<Self, PipelineError>
Creates a file path from a string
Sourcepub fn to_path_buf(&self) -> PathBuf
pub fn to_path_buf(&self) -> PathBuf
Gets the path as a PathBuf
Sourcepub fn to_string_lossy(&self) -> String
pub fn to_string_lossy(&self) -> String
Gets the path as a string
Sourcepub fn into_category<U: PathCategory>(
self,
) -> Result<FilePath<U>, PipelineError>
pub fn into_category<U: PathCategory>( self, ) -> Result<FilePath<U>, PipelineError>
Converts to a different path category (type conversion)
§Purpose
Safely converts a path from one category to another with validation. Useful when a path needs to be used in a different context.
§Why
Category conversion enables:
- Reusing paths across different contexts
- Type-safe path transformations
- Validation of new category requirements
- Flexible path handling
§Type Parameters
U- Target path category (must implementPathCategory)
§Returns
Ok(FilePath<U>)- Converted path with new categoryErr(PipelineError)- Target category validation failed
§Errors
Returns PipelineError if the path doesn’t meet the target
category’s validation requirements.
§Examples
Sourcepub fn with_extension(&self, extension: &str) -> FilePath<T>
pub fn with_extension(&self, extension: &str) -> FilePath<T>
Creates a path with a different extension
Sourcepub fn validate(&self) -> Result<(), PipelineError>
pub fn validate(&self) -> Result<(), PipelineError>
Validates the file path with category-specific rules
Source§impl FilePath<InputMarker>
Specialized constructors for different path types
impl FilePath<InputMarker>
Specialized constructors for different path types
Sourcepub fn existing<P: AsRef<Path>>(path: P) -> Result<Self, PipelineError>
pub fn existing<P: AsRef<Path>>(path: P) -> Result<Self, PipelineError>
Creates an input path that must exist
Sourcepub fn with_required_extension<P: AsRef<Path>>(
path: P,
ext: &str,
) -> Result<Self, PipelineError>
pub fn with_required_extension<P: AsRef<Path>>( path: P, ext: &str, ) -> Result<Self, PipelineError>
Creates an input path with required extension
Source§impl FilePath<OutputMarker>
impl FilePath<OutputMarker>
Sourcepub fn with_parent_creation<P: AsRef<Path>>(
path: P,
) -> Result<Self, PipelineError>
pub fn with_parent_creation<P: AsRef<Path>>( path: P, ) -> Result<Self, PipelineError>
Creates an output path, ensuring parent directory exists
Sourcepub fn create_backup(&self) -> Result<OutputPath, PipelineError>
pub fn create_backup(&self) -> Result<OutputPath, PipelineError>
Creates a backup of an existing output path
Source§impl FilePath<TempMarker>
impl FilePath<TempMarker>
Sourcepub fn unique(prefix: &str, extension: &str) -> Result<Self, PipelineError>
pub fn unique(prefix: &str, extension: &str) -> Result<Self, PipelineError>
Creates a temporary path with unique name
Sourcepub fn auto_cleanup(
prefix: &str,
extension: &str,
) -> Result<AutoCleanupTempPath, PipelineError>
pub fn auto_cleanup( prefix: &str, extension: &str, ) -> Result<AutoCleanupTempPath, PipelineError>
Creates a temporary path that will be automatically cleaned up
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for FilePath<T>
impl<'de, T> Deserialize<'de> for FilePath<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T> Display for FilePath<T>where
T: PathCategory,
impl<T> Display for FilePath<T>where
T: PathCategory,
impl<T: Eq> Eq for FilePath<T>
impl<T> StructuralPartialEq for FilePath<T>
Auto Trait Implementations§
impl<T> Freeze for FilePath<T>
impl<T> RefUnwindSafe for FilePath<T>where
T: RefUnwindSafe,
impl<T> Send for FilePath<T>where
T: Send,
impl<T> Sync for FilePath<T>where
T: Sync,
impl<T> Unpin for FilePath<T>where
T: Unpin,
impl<T> UnwindSafe for FilePath<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more