pub enum Utf8WindowsComponent<'a> {
    Prefix(Utf8WindowsPrefixComponent<'a>),
    RootDir,
    CurDir,
    ParentDir,
    Normal(&'a str),
}
Expand description

str slice version of std::path::Component that represents a Windows-specific component

Variants§

§

Prefix(Utf8WindowsPrefixComponent<'a>)

§

RootDir

§

CurDir

§

ParentDir

§

Normal(&'a str)

Implementations§

Returns path representing this specific component

Converts a non-UTF-8 WindowsComponent to a UTF-8 Utf8WindowsComponent by checking that the component contains valid UTF-8.

Errors

Returns Err if the component is not UTF-8 with a description as to why the provided component is not UTF-8.

Examples

Basic usage:

use typed_path::{Utf8Component, windows::{WindowsComponent, Utf8WindowsComponent}};

// some bytes, in a vector
let component = WindowsComponent::Normal(&[240, 159, 146, 150]);

// We know these bytes are valid, so just use `unwrap()`.
let utf8_component = Utf8WindowsComponent::from_utf8(&component).unwrap();

assert_eq!("💖", utf8_component.as_str());

Incorrect bytes:

use typed_path::windows::{WindowsComponent, Utf8WindowsComponent};

// some invalid bytes, in a vector
let component = WindowsComponent::Normal(&[0, 159, 146, 150]);

assert!(Utf8WindowsComponent::from_utf8(&component).is_err());

See the docs for Utf8Error for more details on the kinds of errors that can be returned.

Converts a non-UTF-8 WindowsComponent to a UTF-8 Utf8WindowsComponent without checking that the string contains valid UTF-8.

See the safe version, from_utf8, for more information.

Safety

The bytes passed in must be valid UTF-8.

Examples

Basic usage:

use typed_path::{Utf8Component, windows::{WindowsComponent, Utf8WindowsComponent}};

// some bytes, in a vector
let component = WindowsComponent::Normal(&[240, 159, 146, 150]);

let utf8_component = unsafe {
    Utf8WindowsComponent::from_utf8_unchecked(&component)
};

assert_eq!("💖", utf8_component.as_str());

Returns true if represents a prefix

Converts from Utf8WindowsComponent to Option<Utf8WindowsPrefixComponent>

Converts self into an Option<Utf8WindowsPrefixComponent>, consuming self, and discarding if not a Utf8WindowsPrefixComponent

Converts from Utf8WindowsComponent to Option<Utf8WindowsPrefix>

Converts self into an Option<Utf8WindowsPrefix>, consuming self, and discarding if not a Utf8WindowsPrefixComponent whose kind method we invoke

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Parses the byte slice into a Utf8WindowsComponent

Examples
use typed_path::windows::{Utf8WindowsComponent, Utf8WindowsPrefix};
use std::convert::TryFrom;

// Supports parsing Windows prefixes
let component = Utf8WindowsComponent::try_from("c:").unwrap();
assert_eq!(component.prefix_kind(), Some(Utf8WindowsPrefix::Disk('C')));

// Supports parsing standard windows path components
assert_eq!(Utf8WindowsComponent::try_from(r"\"), Ok(Utf8WindowsComponent::RootDir));
assert_eq!(Utf8WindowsComponent::try_from("."), Ok(Utf8WindowsComponent::CurDir));
assert_eq!(Utf8WindowsComponent::try_from(".."), Ok(Utf8WindowsComponent::ParentDir));
assert_eq!(Utf8WindowsComponent::try_from(r"file.txt"), Ok(Utf8WindowsComponent::Normal("file.txt")));
assert_eq!(Utf8WindowsComponent::try_from(r"dir\"), Ok(Utf8WindowsComponent::Normal("dir")));

// Parsing more than one component will fail
assert!(Utf8WindowsComponent::try_from(r"\file").is_err());
The type returned in the event of a conversion error.

Extracts the underlying str slice

Examples
use typed_path::{Utf8Component, Utf8WindowsPath};

let path = Utf8WindowsPath::new(r"C:\tmp\foo\..\bar.txt");
let components: Vec<_> = path.components().map(|comp| comp.as_str()).collect();
assert_eq!(&components, &["C:", r"\", "tmp", "foo", "..", "bar.txt"]);

Root is one of two situations

  • Is the root separator, e.g. \windows
  • Is a non-disk prefix, e.g. \\server\share
Examples
use typed_path::{Utf8Component, windows::Utf8WindowsComponent};
use std::convert::TryFrom;

let root_dir = Utf8WindowsComponent::try_from(r"\").unwrap();
assert!(root_dir.is_root());

let non_disk_prefix = Utf8WindowsComponent::try_from(r"\\?\pictures").unwrap();
assert!(non_disk_prefix.is_root());

let disk_prefix = Utf8WindowsComponent::try_from("C:").unwrap();
assert!(!disk_prefix.is_root());

let normal = Utf8WindowsComponent::try_from("file.txt").unwrap();
assert!(!normal.is_root());

Returns true if component is normal

Examples
use typed_path::{Utf8Component, windows::Utf8WindowsComponent};
use std::convert::TryFrom;

let normal = Utf8WindowsComponent::try_from("file.txt").unwrap();
assert!(normal.is_normal());

let root_dir = Utf8WindowsComponent::try_from(r"\").unwrap();
assert!(!root_dir.is_normal());
Returns size of component in bytes
Returns true if component represents an empty str

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.