Skip to main content

better_url/parts/path/entire/
opaque.rs

1//! [`OpaquePath`].
2
3use crate::prelude::*;
4
5/// An opaque path.
6/// # Examples
7/// ```
8/// use better_url::prelude::*;
9///
10/// assert_eq!(OpaquePath::new("  "), " %20");
11/// ```
12#[derive(Debug, Clone)]
13pub struct OpaquePath<'a>(Cow<'a, str>);
14
15impl<'a> OpaquePath<'a> {
16    /// Make a new [`Self`] without doing any validity checks.
17    /// # Safety
18    /// `value` must be a valid [`Self`] literal.
19    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {
20        Self(value.into())
21    }
22
23    /// Borrow as a [`str`].
24    pub fn as_str(&self) -> &str {
25        &self.0
26    }
27
28
29
30
31    /// Turn into the inner [`Cow`].
32    pub fn into_inner(self) -> Cow<'a, str> {
33        self.0
34    }
35
36    /// Turn into an owned [`Self`].
37    pub fn into_owned(self) -> OpaquePath<'static> {
38        OpaquePath(self.0.into_owned().into())
39    }
40
41    /// Make a borrowing [`Self`].
42    pub fn borrowed(&self) -> OpaquePath<'_> {
43        OpaquePath(Cow::Borrowed(&self.0))
44    }
45}
46
47
48
49impl<'a> From<Cow<'a, [u8]>> for OpaquePath<'a> {
50    fn from(value: Cow<'a, [u8]>) -> Self {
51        let (_, value) = make_opaque_path_bytes(value);
52
53        unsafe {
54            Self::new_unchecked(value)
55        }
56    }
57}
58
59
60
61impl<'a> From<Path<'a>> for OpaquePath<'a> {
62    fn from(value: Path<'a>) -> Self {
63        match value {
64            Path::Segmented(x) => x.into(),
65            Path::Opaque   (x) => x,
66        }
67    }
68}
69
70impl<'a> From<SegmentedPath     <'a>> for OpaquePath<'a> {fn from(value: SegmentedPath     <'a>) -> Self {Self(segmented_path_to_opaque_path(value.into_inner()).1)}}
71impl<'a> From<FilePath          <'a>> for OpaquePath<'a> {fn from(value: FilePath          <'a>) -> Self {Self(segmented_path_to_opaque_path(value.into_inner()).1)}}
72impl<'a> From<SpecialNotFilePath<'a>> for OpaquePath<'a> {fn from(value: SpecialNotFilePath<'a>) -> Self {Self(segmented_path_to_opaque_path(value.into_inner()).1)}}
73impl<'a> From<NonSpecialPath    <'a>> for OpaquePath<'a> {fn from(value: NonSpecialPath    <'a>) -> Self {Self(segmented_path_to_opaque_path(value.into_inner()).1)}}