better_url/parts/path/segment/
file.rs1use crate::prelude::*;
4
5#[derive(Debug, Clone)]
7pub struct FilePathSegment<'a>(Cow<'a, str>);
8
9impl<'a> FilePathSegment<'a> {
10 pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {
14 Self(value.into())
15 }
16
17 pub fn as_str(&self) -> &str {
19 &self.0
20 }
21
22 pub fn decode(self) -> Cow<'a, [u8]> {
24 let (_, value) = percent_decode(self.0);
25
26 value
27 }
28
29 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 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 pub fn is_drive_letter (&self) -> bool {path_segment_is_drive_letter (self.as_str())}
48 pub fn is_normalized_drive_letter (&self) -> bool {path_segment_is_normalized_drive_letter (self.as_str())}
49 pub fn is_non_normalized_drive_letter(&self) -> bool {path_segment_is_non_normalized_drive_letter(self.as_str())}
50 pub fn is_single_dot (&self) -> bool {path_segment_is_single_dot (self.as_str())}
51 pub fn is_double_dot (&self) -> bool {path_segment_is_double_dot (self.as_str())}
52
53
54
55 pub fn into_inner(self) -> Cow<'a, str> {
57 self.0
58 }
59
60 pub fn into_owned(self) -> FilePathSegment<'static> {
62 FilePathSegment(self.0.into_owned().into())
63 }
64
65 pub fn borrowed(&self) -> FilePathSegment<'_> {
67 FilePathSegment(Cow::Borrowed(&self.0))
68 }
69}
70
71
72
73impl<'a> From<Cow<'a, [u8]>> for FilePathSegment<'a> {
74 fn from(value: Cow<'a, [u8]>) -> Self {
75 let (_, value) = make_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 FilePathSegment<'a> {
86 fn from(value: PathSegment<'a>) -> Self {
87 match value {
88 PathSegment::File (x) => x,
89 PathSegment::SpecialNotFile(x) => x.into(),
90 PathSegment::NonSpecial (x) => x.into(),
91 }
92 }
93}
94
95impl<'a> From<SpecialNotFilePathSegment<'a>> for FilePathSegment<'a> {fn from(value: SpecialNotFilePathSegment<'a>) -> Self {Self(special_not_file_path_segment_to_file_path_segment(value.into_inner()).1)}}
96impl<'a> From<NonSpecialPathSegment <'a>> for FilePathSegment<'a> {fn from(value: NonSpecialPathSegment <'a>) -> Self {Self(non_special_path_segment_to_file_path_segment (value.into_inner()).1)}}