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, ., .., and /), 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.
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 /).
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 /).
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 /).
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>
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.
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.
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.
Sourcepub 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.
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.
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>.
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.
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>.
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 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.
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.