Skip to main content

better_url/parts/path/segments/
special_not_file.rs

1//! [`SpecialNotFilePathSegments`].
2
3use crate::prelude::*;
4
5/// One or more [`SpecialNotFilePathSegment`]s.
6///
7/// Please note that single and double dot segments are not resolved until placed into a [`SpecialNotFilePath`].
8/// # Examples
9/// ```
10/// use better_url::prelude::*;
11///
12/// let segments = SpecialNotFilePathSegments::new("abc/def/../ghi");
13///
14/// assert_eq!(segments, "abc/def/../ghi");
15///
16/// let path = SpecialNotFilePath::new(segments);
17///
18/// assert_eq!(path, "/abc/ghi");
19/// ```
20#[derive(Debug, Clone)]
21pub struct SpecialNotFilePathSegments<'a>(Cow<'a, str>);
22
23impl<'a> SpecialNotFilePathSegments<'a> {
24    /// Make a new [`Self`] without doing any validity checks.
25    /// # Safety
26    /// `value` must be a valid [`Self`] literal.
27    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {
28        Self(value.into())
29    }
30
31    /// Borrow as a [`str`].
32    pub fn as_str(&self) -> &str {
33        &self.0
34    }
35
36
37
38    /// The [`SpecialNotFilePathSegmentsIter`].
39    pub fn iter(&self) -> SpecialNotFilePathSegmentsIter<'_> {
40        self.into_iter()
41    }
42
43    /// The `index`th [`SpecialNotFilePathSegment`].
44    pub fn get(&self, index: isize) -> Option<SpecialNotFilePathSegment<'_>> {
45        self.iter().neg_nth(index)
46    }
47
48
49
50    /// Turn into the inner [`Cow`].
51    pub fn into_inner(self) -> Cow<'a, str> {
52        self.0
53    }
54
55    /// Turn into an owned [`Self`].
56    pub fn into_owned(self) -> SpecialNotFilePathSegments<'static> {
57        SpecialNotFilePathSegments(self.0.into_owned().into())
58    }
59
60    /// Make a borrowing [`Self`].
61    pub fn borrowed(&self) -> SpecialNotFilePathSegments<'_> {
62        SpecialNotFilePathSegments(Cow::Borrowed(&self.0))
63    }
64}
65
66
67
68impl<'a> From<Cow<'a, [u8]>> for SpecialNotFilePathSegments<'a> {
69    fn from(value: Cow<'a, [u8]>) -> Self {
70        let (_, value) = make_special_not_file_path_segments_bytes(value);
71
72        unsafe {
73            Self::new_unchecked(value)
74        }
75    }
76}
77
78
79
80impl<'a> From<PathSegments<'a>> for SpecialNotFilePathSegments<'a> {
81    fn from(value: PathSegments<'a>) -> Self {
82        match value {
83            PathSegments::File          (x) => x.into(),
84            PathSegments::SpecialNotFile(x) => x,
85            PathSegments::NonSpecial    (x) => x.into(),
86        }
87    }
88}
89
90impl<'a> From<FilePathSegments      <'a>> for SpecialNotFilePathSegments<'a> {fn from(value: FilePathSegments      <'a>) -> Self {Self(                value.into_inner()   )}}
91impl<'a> From<NonSpecialPathSegments<'a>> for SpecialNotFilePathSegments<'a> {fn from(value: NonSpecialPathSegments<'a>) -> Self {Self(forward_slashes(value.into_inner()).1)}}
92
93impl<'a> From<PathSegment              <'a>> for SpecialNotFilePathSegments<'a> {fn from(value: PathSegment              <'a>) -> Self {Self(                value.into_inner()   )}}
94impl<'a> From<FilePathSegment          <'a>> for SpecialNotFilePathSegments<'a> {fn from(value: FilePathSegment          <'a>) -> Self {Self(                value.into_inner()   )}}
95impl<'a> From<SpecialNotFilePathSegment<'a>> for SpecialNotFilePathSegments<'a> {fn from(value: SpecialNotFilePathSegment<'a>) -> Self {Self(                value.into_inner()   )}}
96impl<'a> From<NonSpecialPathSegment    <'a>> for SpecialNotFilePathSegments<'a> {fn from(value: NonSpecialPathSegment    <'a>) -> Self {Self(forward_slashes(value.into_inner()).1)}}
97
98impl<'a, 'b> Extend<SpecialNotFilePathSegment<'b>> for SpecialNotFilePathSegments<'a> {
99    fn extend<I: IntoIterator<Item = SpecialNotFilePathSegment<'b>>>(&mut self, iter: I) {
100        self.0.to_mut().extend(iter.into_iter().flat_map(|x| ["/".into(), x.into_inner()]))
101    }
102}