pub struct PathElementGeneric<'a, S> { /* private fields */ }Expand description
A validated, normalized single path element.
PathElementGeneric takes a raw path element name, validates it (rejecting empty
strings, ., .., strings containing / or \0, and unassigned Unicode characters), normalizes it through a Unicode normalization pipeline,
and computes an OS-compatible presentation form. All three views – original,
normalized, and OS-compatible – are accessible without re-computation.
The type parameter S controls case sensitivity:
CaseSensitive– compile-time case-sensitive (alias:PathElementCS).CaseInsensitive– compile-time case-insensitive (alias:PathElementCI).CaseSensitivity– runtime-selected (alias:PathElement).
Equality, ordering, and hashing are based on the normalized form, so two
PathElementGeneric values with different originals but the same normalized form
are considered equal. Comparing or ordering elements with different case
sensitivities will panic.
Where possible, the normalized and OS-compatible forms borrow from the original string to avoid allocation.
Implementations§
Source§impl<'a> PathElementGeneric<'a, CaseSensitive>
impl<'a> PathElementGeneric<'a, CaseSensitive>
Sourcepub fn from_bytes(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
pub fn from_bytes(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
Creates a new case-sensitive path element from a byte slice.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
let pe = PathElementCS::from_bytes(b"hello.txt")?;
assert_eq!(pe.normalized(), "hello.txt");
// Invalid UTF-8 is replaced with U+FFFD
let pe = PathElementCS::from_bytes(b"hello\xff.txt")?;
assert_eq!(pe.normalized(), "hello\u{FFFD}.txt");Sourcepub fn from_os_str(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
Available on crate feature std only.
pub fn from_os_str(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
std only.Creates a new case-sensitive path element from an OS string.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
let pe = PathElementCS::from_os_str(OsStr::new("hello.txt"))?;
assert_eq!(pe.normalized(), "hello.txt");Source§impl<'a> PathElementGeneric<'a, CaseInsensitive>
impl<'a> PathElementGeneric<'a, CaseInsensitive>
Sourcepub fn from_bytes(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
pub fn from_bytes(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
Creates a new case-insensitive path element from a byte slice.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
let pe = PathElementCI::from_bytes(b"Hello.TXT")?;
assert_eq!(pe.normalized(), "hello.txt");Sourcepub fn from_os_str(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
Available on crate feature std only.
pub fn from_os_str(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
std only.Creates a new case-insensitive path element from an OS string.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
Source§impl<'a> PathElementGeneric<'a, CaseSensitivity>
impl<'a> PathElementGeneric<'a, CaseSensitivity>
Sourcepub fn new(
original: impl Into<Cow<'a, str>>,
case_sensitivity: impl Into<CaseSensitivity>,
) -> Result<Self>
pub fn new( original: impl Into<Cow<'a, str>>, case_sensitivity: impl Into<CaseSensitivity>, ) -> Result<Self>
Creates a new path element with runtime-selected case sensitivity.
§Errors
Returns Error if the name is invalid.
let cs = PathElement::new("Hello.txt", CaseSensitivity::Sensitive)?;
assert_eq!(cs.normalized(), "Hello.txt");
let ci = PathElement::new("Hello.txt", CaseSensitivity::Insensitive)?;
assert_eq!(ci.normalized(), "hello.txt");Sourcepub fn from_bytes(
original: impl Into<Cow<'a, [u8]>>,
case_sensitivity: impl Into<CaseSensitivity>,
) -> Result<Self>
pub fn from_bytes( original: impl Into<Cow<'a, [u8]>>, case_sensitivity: impl Into<CaseSensitivity>, ) -> Result<Self>
Creates a new path element from a byte slice with runtime-selected case sensitivity.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
Sourcepub fn from_os_str(
original: impl Into<Cow<'a, OsStr>>,
case_sensitivity: impl Into<CaseSensitivity>,
) -> Result<Self>
Available on crate feature std only.
pub fn from_os_str( original: impl Into<Cow<'a, OsStr>>, case_sensitivity: impl Into<CaseSensitivity>, ) -> Result<Self>
std only.Creates a new path element from an OS string with runtime-selected case sensitivity.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
Sourcepub fn from_bytes_cs(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
pub fn from_bytes_cs(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
Convenience constructor for a case-sensitive PathElement from bytes.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid.
Sourcepub fn from_bytes_ci(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
pub fn from_bytes_ci(original: impl Into<Cow<'a, [u8]>>) -> Result<Self>
Convenience constructor for a case-insensitive PathElement from bytes.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid.
Sourcepub fn from_os_str_cs(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
Available on crate feature std only.
pub fn from_os_str_cs(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
std only.Convenience constructor for a case-sensitive PathElement from an OS string.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid.
Sourcepub fn from_os_str_ci(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
Available on crate feature std only.
pub fn from_os_str_ci(original: impl Into<Cow<'a, OsStr>>) -> Result<Self>
std only.Convenience constructor for a case-insensitive PathElement from an OS string.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid.
Source§impl<'a, S> PathElementGeneric<'a, S>
impl<'a, S> PathElementGeneric<'a, S>
Sourcepub fn from_bytes_with_case_sensitivity(
original: impl Into<Cow<'a, [u8]>>,
case_sensitivity: impl Into<S>,
) -> Result<Self>
pub fn from_bytes_with_case_sensitivity( original: impl Into<Cow<'a, [u8]>>, case_sensitivity: impl Into<S>, ) -> Result<Self>
Creates a new path element from a byte slice with an explicit case-sensitivity marker.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
This is the most general byte-input constructor. The typed aliases
(PathElementCS::from_bytes(), PathElementCI::from_bytes()) and the
runtime-dynamic constructors delegate to this method.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
Sourcepub fn from_os_str_with_case_sensitivity(
original: impl Into<Cow<'a, OsStr>>,
case_sensitivity: impl Into<S>,
) -> Result<Self>
Available on crate feature std only.
pub fn from_os_str_with_case_sensitivity( original: impl Into<Cow<'a, OsStr>>, case_sensitivity: impl Into<S>, ) -> Result<Self>
std only.Creates a new path element from an OS string with an explicit case-sensitivity marker.
Invalid UTF-8 is accepted; see Normalization pipeline step 0.
§Errors
Returns Error if the name is invalid (empty, ., ..,
or contains /).
Sourcepub fn with_case_sensitivity(
original: impl Into<Cow<'a, str>>,
case_sensitivity: impl Into<S>,
) -> Result<Self>
pub fn with_case_sensitivity( original: impl Into<Cow<'a, str>>, case_sensitivity: impl Into<S>, ) -> Result<Self>
Creates a new path element with an explicit case-sensitivity marker.
This is the most general string constructor. The typed aliases
(PathElementCS::new(), PathElementCI::new()) and the runtime-dynamic
constructors delegate to this method.
§Errors
Returns Error if the name is invalid (empty after
normalization, ., .., or contains /).
Sourcepub fn case_sensitivity(&self) -> CaseSensitivity
pub fn case_sensitivity(&self) -> CaseSensitivity
Returns the case sensitivity of this path element as a CaseSensitivity enum.
assert_eq!(PathElementCS::new("a")?.case_sensitivity(), CaseSensitivity::Sensitive);
assert_eq!(PathElementCI::new("a")?.case_sensitivity(), CaseSensitivity::Insensitive);Source§impl<'a, S> PathElementGeneric<'a, S>
impl<'a, S> PathElementGeneric<'a, S>
Sourcepub fn original(&self) -> &str
pub fn original(&self) -> &str
Returns the original input string, before any normalization.
let pe = PathElementCS::new(" Hello.txt ")?;
assert_eq!(pe.original(), " Hello.txt ");
assert_eq!(pe.normalized(), "Hello.txt");Sourcepub fn into_original(self) -> Cow<'a, str>
pub fn into_original(self) -> Cow<'a, str>
Consumes self and returns the original input string.
let pe = PathElementCS::new(" hello.txt ")?;
assert_eq!(pe.into_original(), " hello.txt ");Sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Returns true if the normalized form is identical to the original.
When this returns true, no allocation was needed for the normalized form.
assert!(PathElementCS::new("hello.txt")?.is_normalized());
assert!(!PathElementCS::new(" hello.txt ")?.is_normalized());Sourcepub fn normalized(&self) -> &str
pub fn normalized(&self) -> &str
Returns the normalized form of the path element name.
This is the canonical representation used for equality comparisons, ordering, and hashing. In case-sensitive mode it is NFC-normalized; in case-insensitive mode it is additionally case-folded.
let cs = PathElementCS::new("Caf\u{00E9}.txt")?;
assert_eq!(cs.normalized(), "Caf\u{00E9}.txt"); // case preserved
let ci = PathElementCI::new("Caf\u{00E9}.txt")?;
assert_eq!(ci.normalized(), "caf\u{00E9}.txt"); // case-foldedSourcepub fn into_normalized(self) -> Cow<'a, str>
pub fn into_normalized(self) -> Cow<'a, str>
Consumes self and returns the normalized form as a Cow.
Returns Cow::Borrowed when the normalized form is a substring of the original
and the original was itself borrowed.
let pe = PathElementCS::new("hello.txt")?;
assert!(matches!(pe.into_normalized(), Cow::Borrowed("hello.txt")));Sourcepub fn is_os_compatible(&self) -> bool
pub fn is_os_compatible(&self) -> bool
Returns true if the OS-compatible form is identical to the original.
assert!(PathElementCS::new("hello.txt")?.is_os_compatible());
assert!(!PathElementCS::new(" hello.txt ")?.is_os_compatible());Sourcepub fn os_compatible(&self) -> &str
pub fn os_compatible(&self) -> &str
Returns the OS-compatible presentation form of the path element name.
let pe = PathElementCS::new("hello.txt")?;
assert_eq!(pe.os_compatible(), "hello.txt");Sourcepub fn into_os_compatible(self) -> Cow<'a, str>
pub fn into_os_compatible(self) -> Cow<'a, str>
Consumes self and returns the OS-compatible form as a Cow<str>.
let pe = PathElementCS::new("hello.txt")?;
assert!(matches!(pe.into_os_compatible(), Cow::Borrowed("hello.txt")));Sourcepub fn os_str(&self) -> &OsStr
Available on crate feature std only.
pub fn os_str(&self) -> &OsStr
std only.Returns the OS-compatible form as an OsStr reference.
let pe = PathElementCS::new("hello.txt")?;
assert_eq!(pe.os_str(), OsStr::new("hello.txt"));Sourcepub fn into_os_str(self) -> Cow<'a, OsStr>
Available on crate feature std only.
pub fn into_os_str(self) -> Cow<'a, OsStr>
std only.Consumes self and returns the OS-compatible form as a Cow<OsStr>.
let pe = PathElementCS::new("hello.txt")?;
assert!(matches!(pe.into_os_str(), Cow::Borrowed(s) if s == OsStr::new("hello.txt")));Sourcepub fn is_borrowed(&self) -> bool
pub fn is_borrowed(&self) -> bool
Returns true if the original string is borrowed (not owned).
let borrowed = PathElementCS::new(Cow::Borrowed("hello"))?;
assert!(borrowed.is_borrowed());
let owned = PathElementCS::new(Cow::<str>::Owned("hello".into()))?;
assert!(!owned.is_borrowed());Sourcepub fn is_owned(&self) -> bool
pub fn is_owned(&self) -> bool
Returns true if the original string is owned (not borrowed).
let owned = PathElementCS::new(Cow::<str>::Owned("hello".into()))?;
assert!(owned.is_owned());
let borrowed = PathElementCS::new("hello")?;
assert!(!borrowed.is_owned());Sourcepub fn into_owned(self) -> PathElementGeneric<'static, S>
pub fn into_owned(self) -> PathElementGeneric<'static, S>
Consumes self and returns an equivalent PathElementGeneric with a 'static
lifetime by cloning the original string if it was borrowed.
let pe = PathElementCS::new("hello.txt")?;
let owned: PathElementCS<'static> = pe.into_owned();
assert!(owned.is_owned());
assert_eq!(owned.normalized(), "hello.txt");Trait Implementations§
Source§impl<'a, S: Clone> Clone for PathElementGeneric<'a, S>
impl<'a, S: Clone> Clone for PathElementGeneric<'a, S>
Source§fn clone(&self) -> PathElementGeneric<'a, S>
fn clone(&self) -> PathElementGeneric<'a, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<S: Debug> Debug for PathElementGeneric<'_, S>
impl<S: Debug> Debug for PathElementGeneric<'_, S>
Source§impl<'a> From<PathElementGeneric<'a, CaseInsensitive>> for PathElement<'a>
Converts a compile-time case-insensitive element into a runtime-dynamic PathElement.
impl<'a> From<PathElementGeneric<'a, CaseInsensitive>> for PathElement<'a>
Converts a compile-time case-insensitive element into a runtime-dynamic PathElement.
Source§fn from(pe: PathElementCI<'a>) -> Self
fn from(pe: PathElementCI<'a>) -> Self
Source§impl<'a> From<PathElementGeneric<'a, CaseSensitive>> for PathElement<'a>
Converts a compile-time case-sensitive element into a runtime-dynamic PathElement.
impl<'a> From<PathElementGeneric<'a, CaseSensitive>> for PathElement<'a>
Converts a compile-time case-sensitive element into a runtime-dynamic PathElement.
Source§fn from(pe: PathElementCS<'a>) -> Self
fn from(pe: PathElementCS<'a>) -> Self
Source§impl<S> Ord for PathElementGeneric<'_, S>
Compares by normalized().
impl<S> Ord for PathElementGeneric<'_, S>
Compares by normalized().
§Panics
Panics if self and other have different CaseSensitivity values.
This can only happen with the runtime-dynamic PathElement type alias.
The typed aliases PathElementCS and PathElementCI always have
matching sensitivity, so they never panic.
Source§impl<'a, S1, S2> PartialEq<PathElementGeneric<'a, S2>> for PathElementGeneric<'_, S1>
Compares by normalized().
impl<'a, S1, S2> PartialEq<PathElementGeneric<'a, S2>> for PathElementGeneric<'_, S1>
Compares by normalized().
§Panics
Panics if self and other have different CaseSensitivity values.
Use PartialOrd for a non-panicking comparison that returns None on mismatch.
Source§impl<'a, S1, S2> PartialOrd<PathElementGeneric<'a, S2>> for PathElementGeneric<'_, S1>
Compares by normalized(). Returns None if the two elements have
different CaseSensitivity values.
impl<'a, S1, S2> PartialOrd<PathElementGeneric<'a, S2>> for PathElementGeneric<'_, S1>
Compares by normalized(). Returns None if the two elements have
different CaseSensitivity values.
Source§impl<'a> TryFrom<PathElementGeneric<'a, CaseSensitivity>> for PathElementCI<'a>
Attempts to convert a runtime-dynamic PathElement into a PathElementCI.
impl<'a> TryFrom<PathElementGeneric<'a, CaseSensitivity>> for PathElementCI<'a>
Attempts to convert a runtime-dynamic PathElement into a PathElementCI.
Succeeds if the element is case-insensitive. On failure, returns the element
re-wrapped as a PathElementCS in the Err variant (no data is lost).
Source§type Error = PathElementGeneric<'a, CaseSensitive>
type Error = PathElementGeneric<'a, CaseSensitive>
Source§impl<'a> TryFrom<PathElementGeneric<'a, CaseSensitivity>> for PathElementCS<'a>
Attempts to convert a runtime-dynamic PathElement into a PathElementCS.
impl<'a> TryFrom<PathElementGeneric<'a, CaseSensitivity>> for PathElementCS<'a>
Attempts to convert a runtime-dynamic PathElement into a PathElementCS.
Succeeds if the element is case-sensitive. On failure, returns the element
re-wrapped as a PathElementCI in the Err variant (no data is lost).
Source§type Error = PathElementGeneric<'a, CaseInsensitive>
type Error = PathElementGeneric<'a, CaseInsensitive>
impl<S> Eq for PathElementGeneric<'_, S>
See PartialEq for panicking behavior on case sensitivity mismatch.