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

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

Variants§

§

RootDir

§

CurDir

§

ParentDir

§

Normal(&'a str)

Implementations§

Converts a non-UTF-8 UnixComponent to a UTF-8 Utf8UnixComponent 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, unix::{UnixComponent, Utf8UnixComponent}};

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

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

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

Incorrect bytes:

use typed_path::unix::{UnixComponent, Utf8UnixComponent};

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

assert!(Utf8UnixComponent::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 UnixComponent to a UTF-8 Utf8UnixComponent 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, unix::{UnixComponent, Utf8UnixComponent}};

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

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

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

Returns path representing this specific component

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 str slice into a Utf8UnixComponent

Examples
use typed_path::unix::Utf8UnixComponent;
use std::convert::TryFrom;

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

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

Extracts the underlying str slice

Examples
use typed_path::{Utf8Component, Utf8UnixPath};

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

Returns true if is the root dir component

Examples
use typed_path::{Utf8Component, unix::Utf8UnixComponent};
use std::convert::TryFrom;

let root_dir = Utf8UnixComponent::try_from("/").unwrap();
assert!(root_dir.is_root());

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

Returns true if is a normal component

Examples
use typed_path::{Utf8Component, unix::Utf8UnixComponent};
use std::convert::TryFrom;

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

let root_dir = Utf8UnixComponent::try_from("/").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.