Struct Path

Source
pub struct Path<'a>(/* private fields */);
Expand description

Represents a (borrowed) path to file.

Neotron OS uses the following format for file paths:

<drive>:/[<directory>/]...<filename>.<extension>

Unlike on MS-DOS, the drive specifier portion is not limited to a single ASCII letter and can be any UTF-8 string that does not contain : or /.

Typically drives will look like DEV: or HD0:, but that’s not enforced here.

Paths are a sub-set of UTF-8 strings in this API, but be aware that not all filesystems support all Unicode characters. In particular FAT16 and FAT32 volumes are likely to be limited to only A-Z, a-z, 0-9 and $%-_@~\!(){}^#&`. This API will expressly disallow UTF-8 codepoints below 32 (i.e. C0 control characters) to avoid confusion, but non-ASCII code-points are accepted.

Paths are case-preserving but file operations may not be case-sensitive (depending on the filesystem you are accessing). Paths may contain spaces (but your filesystem may not support that).

Here are some examples of valid paths:

# relative to the Current Directory
Documents/2023/June/Sales in €.xls
# a file on drive HD0
HD0:/MYDOCU~1/SALES.TXT
# a directory on drive SD0
SD0:/MYDOCU~1/
# a file on drive SD0, with no file extension
SD0:/BOOTLDR

Files and Directories generally have distinct APIs, so a directory without a trailing / is likely to be accepted. A file path with a trailing / won’t be accepted.

Implementations§

Source§

impl<'a> Path<'a>

Source

pub const PATH_SEP: char = '/'

The character that separates one directory name from another directory name.

Source

pub const DRIVE_SEP: char = ':'

The character that separates drive specifiers from directories.

Source

pub fn new(path_str: &'a str) -> Result<Path<'a>, Error>

Create a path from a string.

If the given string is not a valid path, an Err is returned.

Source

pub fn is_absolute_path(&self) -> bool

Is this an absolute path?

Absolute paths have drive specifiers. Relative paths do not.

Source

pub fn drive_specifier(&self) -> Option<&str>

Get the drive specifier for this path.

  • A path like DS0:/FOO/BAR.TXT has a drive specifier of DS0.
  • A path like BAR.TXT has no drive specifier.
Source

pub fn drive_path(&self) -> Option<&str>

Get the drive path portion.

That is, everything after the directory specifier.

Source

pub fn directory(&self) -> Option<&str>

Get the directory portion of this path.

  • A path like DS0:/FOO/BAR.TXT has a directory portion of /FOO.
  • A path like DS0:/FOO/BAR/ has a directory portion of /FOO/BAR.
  • A path like BAR.TXT has no directory portion.
Source

pub fn filename(&self) -> Option<&str>

Get the filename portion of this path. This filename will include the file extension, if any.

  • A path like DS0:/FOO/BAR.TXT has a filename portion of /BAR.TXT.
  • A path like DS0:/FOO has a filename portion of /FOO.
  • A path like DS0:/FOO/ has no filename portion (so it’s important directories have a trailing /)
Source

pub fn extension(&self) -> Option<&str>

Get the filename extension portion of this path.

A path like DS0:/FOO/BAR.TXT has a filename extension portion of TXT. A path like DS0:/FOO/BAR has no filename extension portion.

Source

pub fn as_str(&self) -> &str

View this Path as a string-slice.

Auto Trait Implementations§

§

impl<'a> Freeze for Path<'a>

§

impl<'a> RefUnwindSafe for Path<'a>

§

impl<'a> Send for Path<'a>

§

impl<'a> Sync for Path<'a>

§

impl<'a> Unpin for Path<'a>

§

impl<'a> UnwindSafe for Path<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.