Skip to main content

better_url/parts/path/segment/
special_not_file.rs

1//! [`SpecialNotFilePathSegment`].
2
3use crate::prelude::*;
4
5/// A [`SpecialNotFilePath`] segment.
6#[derive(Debug, Clone)]
7pub struct SpecialNotFilePathSegment<'a>(Cow<'a, str>);
8
9impl<'a> SpecialNotFilePathSegment<'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) -> SpecialNotFilePathSegment<'static> {
62        SpecialNotFilePathSegment(self.0.into_owned().into())
63    }
64
65    /// Make a borrowing [`Self`].
66    pub fn borrowed(&self) -> SpecialNotFilePathSegment<'_> {
67        SpecialNotFilePathSegment(Cow::Borrowed(&self.0))
68    }
69}
70
71
72
73impl<'a> From<Cow<'a, [u8]>> for SpecialNotFilePathSegment<'a> {
74    fn from(value: Cow<'a, [u8]>) -> Self {
75        let (_, value) = make_special_not_file_path_segment_bytes(value);
76
77        unsafe {
78            Self::new_unchecked(value)
79        }
80    }
81}
82
83
84
85impl<'a> From<PathSegment<'a>> for SpecialNotFilePathSegment<'a> {
86    fn from(value: PathSegment<'a>) -> Self {
87        match value {
88            PathSegment::File          (x) => x.into(),
89            PathSegment::SpecialNotFile(x) => x,
90            PathSegment::NonSpecial    (x) => x.into(),
91        }
92    }
93}
94
95impl<'a> From<FilePathSegment      <'a>> for SpecialNotFilePathSegment<'a> {fn from(value: FilePathSegment      <'a>) -> Self {Self(                                                          value.into_inner()   )}}
96impl<'a> From<NonSpecialPathSegment<'a>> for SpecialNotFilePathSegment<'a> {fn from(value: NonSpecialPathSegment<'a>) -> Self {Self(non_special_path_segment_to_special_not_file_path_segment(value.into_inner()).1)}}