Skip to main content

better_url/parts/path/entire/segmented/file/
mod.rs

1//! [`FilePath`].
2
3use crate::prelude::*;
4
5mod get;
6mod set;
7mod remove;
8
9/// A [`SchemeType::File`] path.
10#[derive(Debug, Clone)]
11pub struct FilePath<'a>(Cow<'a, str>);
12
13impl<'a> FilePath<'a> {
14    /// Make a new [`Self`] without doing any validity checks.
15    /// # Safety
16    /// `value` must be a valid [`Self`] literal.
17    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {
18        Self(value.into())
19    }
20
21    /// Borrow as a [`str`].
22    pub fn as_str(&self) -> &str {
23        &self.0
24    }
25
26    /// The [`FilePathSegments`].
27    pub fn segments(&self) -> FilePathSegments<'_> {
28        unsafe {FilePathSegments::new_unchecked(self.as_str().get_unchecked(1..))}
29    }
30
31
32
33    /// Turn into the inner [`Cow`].
34    pub fn into_inner(self) -> Cow<'a, str> {
35        self.0
36    }
37
38    /// Turn into an owned [`Self`].
39    pub fn into_owned(self) -> FilePath<'static> {
40        FilePath(self.0.into_owned().into())
41    }
42
43    /// Make a borrowing [`Self`].
44    pub fn borrowed(&self) -> FilePath<'_> {
45        FilePath(Cow::Borrowed(&self.0))
46    }
47}
48
49
50
51impl<'a> From<Cow<'a, [u8]>> for FilePath<'a> {
52    fn from(value: Cow<'a, [u8]>) -> Self {
53        let (_, value) = make_file_path_bytes(value);
54
55        unsafe {
56            Self::new_unchecked(value)
57        }
58    }
59}
60
61
62
63impl<'a> From<Path<'a>> for FilePath<'a> {
64    fn from(value: Path<'a>) -> Self {
65        match value {
66            Path::Segmented(x) => x.into(),
67            Path::Opaque   (x) => x.into(),
68        }
69    }
70}
71
72impl<'a> From<SegmentedPath<'a>> for FilePath<'a> {
73    fn from(value: SegmentedPath<'a>) -> Self {
74        match value {
75            SegmentedPath::File          (x) => x,
76            SegmentedPath::SpecialNotFile(x) => x.into(),
77            SegmentedPath::NonSpecial    (x) => x.into(),
78        }
79    }
80}
81
82
83
84impl<'a> From<SpecialNotFilePath<'a>> for FilePath<'a> {fn from(value: SpecialNotFilePath<'a>) -> Self {Self(special_not_file_path_to_file_path(value.into_inner()).1)}}
85impl<'a> From<NonSpecialPath    <'a>> for FilePath<'a> {fn from(value: NonSpecialPath    <'a>) -> Self {Self(non_special_path_to_file_path     (value.into_inner()).1)}}
86impl<'a> From<OpaquePath        <'a>> for FilePath<'a> {fn from(value: OpaquePath        <'a>) -> Self {Self(opaque_path_to_file_path          (value.into_inner()).1)}}
87
88impl From<PathSegments              <'_>> for FilePath<'static> {fn from(value: PathSegments              <'_>) -> Self {Self(resolve_file_path(                value.into_inner().with_insert_str(0, "/")   ).1)}}
89impl From<FilePathSegments          <'_>> for FilePath<'static> {fn from(value: FilePathSegments          <'_>) -> Self {Self(resolve_file_path(                value.into_inner().with_insert_str(0, "/")   ).1)}}
90impl From<SpecialNotFilePathSegments<'_>> for FilePath<'static> {fn from(value: SpecialNotFilePathSegments<'_>) -> Self {Self(resolve_file_path(                value.into_inner().with_insert_str(0, "/")   ).1)}}
91impl From<NonSpecialPathSegments    <'_>> for FilePath<'static> {fn from(value: NonSpecialPathSegments    <'_>) -> Self {Self(resolve_file_path(forward_slashes(value.into_inner().with_insert_str(0, "/")).1).1)}}