Skip to main content

better_url/parts/path/segment/
non_special.rs

1//! [`NonSpecialPathSegment`].
2
3use crate::prelude::*;
4
5/// A [`NonSpecialPath`] segment.
6#[derive(Debug, Clone)]
7pub struct NonSpecialPathSegment<'a>(Cow<'a, str>);
8
9impl<'a> NonSpecialPathSegment<'a> {
10    /// Make a new [`Self`] without doing any validity checks.
11    /// # Safety
12    /// `value` must be a valid [`Self`] literal.
13    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {
14        Self(value.into())
15    }
16
17    /// Borrow as a [`str`].
18    pub fn as_str(&self) -> &str {
19        &self.0
20    }
21
22    /// [`percent_decode`].
23    pub fn decode(self) -> Cow<'a, [u8]> {
24        let (_, value) = percent_decode(self.0);
25
26        value
27    }
28
29    /// [`try_percent_decode`].
30    /// # Errors
31    /// If [`try_percent_decode`] returns an error, that error is returned.
32    pub fn try_decode(self) -> Result<Cow<'a, str>, (std::str::Utf8Error, Cow<'a, [u8]>)> {
33        let (_, value) = try_percent_decode(self.into_inner())?;
34
35        Ok(value)
36    }
37
38    /// [`lossy_percent_decode`].
39    pub fn lossy_decode(self) -> Cow<'a, str> {
40        let (_, value) = lossy_percent_decode(self.into_inner());
41
42        value
43    }
44
45
46
47    /** [`path_segment_is_drive_letter`].                */ pub fn is_drive_letter               (&self) -> bool {path_segment_is_drive_letter               (self.as_str())}
48    /** [`path_segment_is_normalized_drive_letter`].     */ pub fn is_normalized_drive_letter    (&self) -> bool {path_segment_is_normalized_drive_letter    (self.as_str())}
49    /** [`path_segment_is_non_normalized_drive_letter`]. */ pub fn is_non_normalized_drive_letter(&self) -> bool {path_segment_is_non_normalized_drive_letter(self.as_str())}
50    /** [`path_segment_is_single_dot`].                  */ pub fn is_single_dot                 (&self) -> bool {path_segment_is_single_dot                 (self.as_str())}
51    /** [`path_segment_is_double_dot`].                  */ pub fn is_double_dot                 (&self) -> bool {path_segment_is_double_dot                 (self.as_str())}
52
53
54
55    /// Turn into the inner [`Cow`].
56    pub fn into_inner(self) -> Cow<'a, str> {
57        self.0
58    }
59
60    /// Turn into an owned [`Self`].
61    pub fn into_owned(self) -> NonSpecialPathSegment<'static> {
62        NonSpecialPathSegment(self.0.into_owned().into())
63    }
64
65    /// Make a borrowing [`Self`].
66    pub fn borrowed(&self) -> NonSpecialPathSegment<'_> {
67        NonSpecialPathSegment(Cow::Borrowed(&self.0))
68    }
69}
70
71
72
73impl<'a> From<Cow<'a, [u8]>> for NonSpecialPathSegment<'a> {
74    fn from(value: Cow<'a, [u8]>) -> Self {
75        let (_, value) = make_non_special_path_segment_bytes(value);
76
77        unsafe {
78            Self::new_unchecked(value)
79        }
80    }
81}
82
83
84
85impl<'a> From<PathSegment<'a>> for NonSpecialPathSegment<'a> {
86    fn from(value: PathSegment<'a>) -> Self {
87        match value {
88            PathSegment::File          (x) => x.into(),
89            PathSegment::SpecialNotFile(x) => x.into(),
90            PathSegment::NonSpecial    (x) => x,
91        }
92    }
93}
94
95impl<'a> From<SpecialNotFilePathSegment<'a>> for NonSpecialPathSegment<'a> {fn from(value: SpecialNotFilePathSegment<'a>) -> Self {Self(value.into_inner())}}
96impl<'a> From<FilePathSegment          <'a>> for NonSpecialPathSegment<'a> {fn from(value: FilePathSegment          <'a>) -> Self {Self(value.into_inner())}}