PathType

Trait PathType 

Source
pub trait PathType:
    PartialEq
    + Send
    + Sync {
    type OwnedPath: OwnedPathType<RefType = Self>;
    type PathSegmentRef: ToOwned<Owned = Self::PathSegmentOwned> + PartialEq + ?Sized;
    type PathSegmentOwned: Send + Sync + Clone + Eq + AsRef<Self::PathSegmentRef>;
    type StripPrefixError: StdError + Send + Sync + 'static;

    // Required methods
    fn parent(&self) -> Option<&Self>;
    fn join(&self, new_fragment: impl AsRef<Self>) -> Self::OwnedPath;
    fn join_segment(
        &self,
        new_fragment: impl AsRef<Self::PathSegmentRef>,
    ) -> Self::OwnedPath;
    fn join_segment_str(&self, new_fragment: &str) -> Self::OwnedPath;
    fn strip_prefix(
        &self,
        base: &Self,
    ) -> StdResult<&Self, Self::StripPrefixError>;
    fn owned(&self) -> Self::OwnedPath;
}
Expand description

A trait representing a path in a virtual file system.

This can be implemented for custom path types, allowing you to create Vfs implementations that work with path types other than Path and PathBuf. PathType is implemented for the reference type, which is “linked” to the owned type via the associated type OwnedPath.

Once you have a value of that owned type (e.g., MyPathBuf), you can get a reference to the path type (e.g., &MyPath) via the AsRef trait, which is a super-trait of OwnedPathType.

This trait is implemented for Path, so if your VFS uses Path as its path type, you can just use that.

All the examples in the documentation for PathType and OwnedPathType use Path and PathBuf. You may treat them as expected behaviours for your own implementations of these traits, and derive your tests from them.

Required Associated Types§

Source

type OwnedPath: OwnedPathType<RefType = Self>

The owned version of this path type.

This owned type must implement the OwnedPathType trait, which has as a super-trait of AsRef, allowing us to get a reference to this PathType type from the OwnedPathType type.

Source

type PathSegmentRef: ToOwned<Owned = Self::PathSegmentOwned> + PartialEq + ?Sized

The type of a path segment (a component of a path).

This represents a single segment of a path, such as a file or directory name.

In the case of Path, this is OsStr.

Source

type PathSegmentOwned: Send + Sync + Clone + Eq + AsRef<Self::PathSegmentRef>

The owned version of a path segment.

This represents a single segment of a path, such as a file or directory name.

This is the owned version of PathType::PathSegmentRef.

Source

type StripPrefixError: StdError + Send + Sync + 'static

The error type returned when stripping a prefix fails.

Required Methods§

Source

fn parent(&self) -> Option<&Self>

Returns the parent path, if it exists.

§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::PathType;

let path = Path::new("some/dir/file.txt");
let parent = PathType::parent(path);
assert_eq!(parent, Some(Path::new("some/dir")));

let path = Path::new("file.txt");
let parent = PathType::parent(path);
assert_eq!(parent, Some(Path::new("")));

let empty = Path::new("");
let parent = PathType::parent(empty);
assert_eq!(parent, None);
Source

fn join(&self, new_fragment: impl AsRef<Self>) -> Self::OwnedPath

Joins this path with another path fragment, returning a new owned path.

§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::PathType;

let path = Path::new("some/dir");
let new_path = PathType::join(path, Path::new("file.txt"));
assert_eq!(new_path, PathBuf::from("some/dir/file.txt"));
Source

fn join_segment( &self, new_fragment: impl AsRef<Self::PathSegmentRef>, ) -> Self::OwnedPath

Joins this path with a path segment, returning a new owned path.

§Example
use std::path::{Path, PathBuf};
use std::ffi::OsStr;
use dir_structure::traits::vfs::PathType;

let path = Path::new("some/dir");
let new_path = PathType::join_segment(path, OsStr::new("file.txt"));
assert_eq!(new_path, PathBuf::from("some/dir/file.txt"));
Source

fn join_segment_str(&self, new_fragment: &str) -> Self::OwnedPath

Joins this path with a string slice as a path segment, returning a new owned path.

This is the method used to derive the paths of subfields in a directory structure, when using the derive macro.

§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::PathType;

let path = Path::new("some/dir");
let new_path = PathType::join_segment_str(path, "file.txt");
assert_eq!(new_path, PathBuf::from("some/dir/file.txt"));
Source

fn strip_prefix(&self, base: &Self) -> StdResult<&Self, Self::StripPrefixError>

Strips the given base path from this path, returning the relative path if successful.

§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::PathType;

let path = Path::new("some/dir/file.txt");
let base = Path::new("some");
let relative = PathType::strip_prefix(path, base).unwrap();
assert_eq!(relative, Path::new("dir/file.txt"));

let base = Path::new("other");
let relative = PathType::strip_prefix(path, base);
assert!(relative.is_err());
Source

fn owned(&self) -> Self::OwnedPath

Converts this path to its owned version.

§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::PathType;

let path = Path::new("some/dir/file.txt");
let owned = PathType::owned(path);
assert_eq!(owned, PathBuf::from("some/dir/file.txt"));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PathType for Path

Source§

type OwnedPath = PathBuf

Source§

type PathSegmentRef = OsStr

Source§

type PathSegmentOwned = OsString

Source§

type StripPrefixError = StripPrefixError

Source§

fn parent(&self) -> Option<&Self>

Source§

fn join(&self, new_fragment: impl AsRef<Self>) -> Self::OwnedPath

Source§

fn join_segment( &self, new_fragment: impl AsRef<Self::PathSegmentRef>, ) -> Self::OwnedPath

Source§

fn join_segment_str(&self, new_fragment: &str) -> Self::OwnedPath

Source§

fn strip_prefix(&self, base: &Self) -> StdResult<&Self, Self::StripPrefixError>

Source§

fn owned(&self) -> Self::OwnedPath

Implementors§