pub enum WindowsComponent<'a> {
Prefix(WindowsPrefixComponent<'a>),
RootDir,
CurDir,
ParentDir,
Normal(&'a [u8]),
}
Expand description
Byte slice version of std::path::Component
that represents a Windows-specific component
Variants§
Implementations§
Source§impl<'a> WindowsComponent<'a>
impl<'a> WindowsComponent<'a>
Sourcepub fn as_path<T>(&self) -> &Path<T>where
T: Encoding,
pub fn as_path<T>(&self) -> &Path<T>where
T: Encoding,
Returns path representing this specific component
Sourcepub fn prefix(self) -> Option<WindowsPrefixComponent<'a>>
pub fn prefix(self) -> Option<WindowsPrefixComponent<'a>>
Converts from WindowsComponent
to Option<WindowsPrefixComponent>
Converts self
into an Option<WindowsPrefixComponent>
, consuming self
, and
discarding if not a WindowsPrefixComponent
Sourcepub fn prefix_kind(self) -> Option<WindowsPrefix<'a>>
pub fn prefix_kind(self) -> Option<WindowsPrefix<'a>>
Converts from WindowsComponent
to Option<WindowsPrefix>
Converts self
into an Option<WindowsPrefix>
, consuming self
, and
discarding if not a WindowsPrefixComponent
whose kind
method we invoke
Trait Implementations§
Source§impl AsRef<[u8]> for WindowsComponent<'_>
impl AsRef<[u8]> for WindowsComponent<'_>
Source§impl<'a> Clone for WindowsComponent<'a>
impl<'a> Clone for WindowsComponent<'a>
Source§fn clone(&self) -> WindowsComponent<'a>
fn clone(&self) -> WindowsComponent<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> Component<'a> for WindowsComponent<'a>
impl<'a> Component<'a> for WindowsComponent<'a>
Source§fn as_bytes(&self) -> &'a [u8] ⓘ
fn as_bytes(&self) -> &'a [u8] ⓘ
Extracts the underlying [[u8]
] slice
§Examples
use typed_path::{Component, WindowsPath};
let path = WindowsPath::new(br"C:\tmp\foo\..\bar.txt");
let components: Vec<_> = path.components().map(|comp| comp.as_bytes()).collect();
assert_eq!(&components, &[
b"C:".as_slice(),
br"\".as_slice(),
b"tmp".as_slice(),
b"foo".as_slice(),
b"..".as_slice(),
b"bar.txt".as_slice(),
]);
Source§fn is_root(&self) -> bool
fn is_root(&self) -> bool
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::{Component, WindowsComponent};
use std::convert::TryFrom;
let root_dir = WindowsComponent::try_from(br"\").unwrap();
assert!(root_dir.is_root());
let non_disk_prefix = WindowsComponent::try_from(br"\\?\pictures").unwrap();
assert!(non_disk_prefix.is_root());
let disk_prefix = WindowsComponent::try_from(b"C:").unwrap();
assert!(!disk_prefix.is_root());
let normal = WindowsComponent::try_from(b"file.txt").unwrap();
assert!(!normal.is_root());
Source§fn is_normal(&self) -> bool
fn is_normal(&self) -> bool
Returns true if component is normal
§Examples
use typed_path::{Component, WindowsComponent};
use std::convert::TryFrom;
let normal = WindowsComponent::try_from(b"file.txt").unwrap();
assert!(normal.is_normal());
let root_dir = WindowsComponent::try_from(br"\").unwrap();
assert!(!root_dir.is_normal());
Source§fn is_parent(&self) -> bool
fn is_parent(&self) -> bool
Returns true if is a parent directory component
§Examples
use typed_path::{Component, WindowsComponent};
use std::convert::TryFrom;
let parent = WindowsComponent::try_from("..").unwrap();
assert!(parent.is_parent());
let root_dir = WindowsComponent::try_from(r"\").unwrap();
assert!(!root_dir.is_parent());
Source§fn is_current(&self) -> bool
fn is_current(&self) -> bool
Returns true if is the current directory component
§Examples
use typed_path::{Component, WindowsComponent};
use std::convert::TryFrom;
let current = WindowsComponent::try_from(".").unwrap();
assert!(current.is_current());
let root_dir = WindowsComponent::try_from(r"\").unwrap();
assert!(!root_dir.is_current());
Source§fn is_valid(&self) -> bool
fn is_valid(&self) -> bool
Returns true if this component is valid.
A component can only be invalid if it represents a normal component with bytes that are disallowed by the encoding.
§Examples
use typed_path::{Component, WindowsComponent};
use std::convert::TryFrom;
assert!(WindowsComponent::try_from("c:").unwrap().is_valid());
assert!(WindowsComponent::RootDir.is_valid());
assert!(WindowsComponent::ParentDir.is_valid());
assert!(WindowsComponent::CurDir.is_valid());
assert!(WindowsComponent::Normal(b"abc").is_valid());
assert!(!WindowsComponent::Normal(b"|").is_valid());
Source§fn root() -> Self
fn root() -> Self
Returns the root directory component.
§Examples
use typed_path::{Component, WindowsComponent};
assert_eq!(WindowsComponent::root(), WindowsComponent::RootDir);
Source§fn parent() -> Self
fn parent() -> Self
Returns the parent directory component.
§Examples
use typed_path::{Component, WindowsComponent};
assert_eq!(WindowsComponent::parent(), WindowsComponent::ParentDir);
Source§impl<'a> Debug for WindowsComponent<'a>
impl<'a> Debug for WindowsComponent<'a>
Source§impl<'a> Hash for WindowsComponent<'a>
impl<'a> Hash for WindowsComponent<'a>
Source§impl<'a> Ord for WindowsComponent<'a>
impl<'a> Ord for WindowsComponent<'a>
Source§fn cmp(&self, other: &WindowsComponent<'a>) -> Ordering
fn cmp(&self, other: &WindowsComponent<'a>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a> PartialEq for WindowsComponent<'a>
impl<'a> PartialEq for WindowsComponent<'a>
Source§impl<'a> PartialOrd for WindowsComponent<'a>
impl<'a> PartialOrd for WindowsComponent<'a>
Source§impl<'a> TryFrom<&'a [u8]> for WindowsComponent<'a>
impl<'a> TryFrom<&'a [u8]> for WindowsComponent<'a>
Source§fn try_from(path: &'a [u8]) -> Result<Self, Self::Error>
fn try_from(path: &'a [u8]) -> Result<Self, Self::Error>
Parses the byte slice into a WindowsComponent
§Examples
use typed_path::{WindowsComponent, WindowsPrefix};
use std::convert::TryFrom;
// Supports parsing Windows prefixes
let component = WindowsComponent::try_from(b"c:").unwrap();
assert_eq!(component.prefix_kind(), Some(WindowsPrefix::Disk(b'C')));
// Supports parsing standard windows path components
assert_eq!(WindowsComponent::try_from(br"\"), Ok(WindowsComponent::RootDir));
assert_eq!(WindowsComponent::try_from(b"."), Ok(WindowsComponent::CurDir));
assert_eq!(WindowsComponent::try_from(b".."), Ok(WindowsComponent::ParentDir));
assert_eq!(WindowsComponent::try_from(br"file.txt"), Ok(WindowsComponent::Normal(b"file.txt")));
assert_eq!(WindowsComponent::try_from(br"dir\"), Ok(WindowsComponent::Normal(b"dir")));
// Parsing more than one component will fail
assert!(WindowsComponent::try_from(br"\file").is_err());
Source§impl<'a> TryFrom<&'a str> for WindowsComponent<'a>
impl<'a> TryFrom<&'a str> for WindowsComponent<'a>
Source§impl<'a> TryFrom<Component<'a>> for WindowsComponent<'a>
impl<'a> TryFrom<Component<'a>> for WindowsComponent<'a>
Source§fn try_from(component: Component<'a>) -> Result<Self, Self::Error>
fn try_from(component: Component<'a>) -> Result<Self, Self::Error>
Attempts to convert a std::path::Component
into a WindowsComponent
, returning a
result containing the new component when successful or the original component when failed
§Examples
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::Component;
use typed_path::WindowsComponent;
let component = WindowsComponent::try_from(Component::RootDir).unwrap();
assert_eq!(component, WindowsComponent::RootDir);
let component = WindowsComponent::try_from(Component::CurDir).unwrap();
assert_eq!(component, WindowsComponent::CurDir);
let component = WindowsComponent::try_from(Component::ParentDir).unwrap();
assert_eq!(component, WindowsComponent::ParentDir);
let component = WindowsComponent::try_from(Component::Normal(OsStr::new("file.txt"))).unwrap();
assert_eq!(component, WindowsComponent::Normal(b"file.txt"));
Alongside the traditional path components, the Component::Prefix
variant is also
supported, but only when compiling on Windows. When on a non-Windows platform, the
conversion will always fail.
Source§impl<'a> TryFrom<WindowsComponent<'a>> for Component<'a>
impl<'a> TryFrom<WindowsComponent<'a>> for Component<'a>
Source§fn try_from(component: WindowsComponent<'a>) -> Result<Self, Self::Error>
fn try_from(component: WindowsComponent<'a>) -> Result<Self, Self::Error>
Attempts to convert a WindowsComponent
into a std::path::Component
, returning a
result containing the new path when successful or the original path when failed
§Examples
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::Component;
use typed_path::WindowsComponent;
let component = Component::try_from(WindowsComponent::RootDir).unwrap();
assert_eq!(component, Component::RootDir);
let component = Component::try_from(WindowsComponent::CurDir).unwrap();
assert_eq!(component, Component::CurDir);
let component = Component::try_from(WindowsComponent::ParentDir).unwrap();
assert_eq!(component, Component::ParentDir);
let component = Component::try_from(WindowsComponent::Normal(b"file.txt")).unwrap();
assert_eq!(component, Component::Normal(OsStr::new("file.txt")));
Alongside the traditional path components, the Component::Prefix
variant is also
supported, but only when compiling on Windows. When on a non-Windows platform, the
conversion will always fail.