better_url/parts/path/entire/
either.rs1use crate::prelude::*;
4
5#[derive(Debug, Clone)]
7pub enum Path<'a> {
8 Segmented(SegmentedPath<'a>),
9 Opaque (OpaquePath <'a>),
10}
11
12impl<'a> Path<'a> {
13 pub fn new<T: Into<FilePath<'a>> + Into<SpecialNotFilePath<'a>> + Into<NonSpecialPath<'a>> + Into<OpaquePath<'a>>>(value: T, r#type: PathType) -> Self {
15 match r#type {
16 PathType::Segmented(x) => Self::new_segmented(value, x),
17 PathType::Opaque => Self::new_opaque (value ),
18 }
19 }
20
21 pub fn new_segmented <T: Into<FilePath<'a>> + Into<SpecialNotFilePath<'a>> + Into<NonSpecialPath<'a>>>(value: T, r#type: SegmentedPathType) -> Self {SegmentedPath::new(value, r#type).into()}
22 pub fn new_file <T: Into<FilePath <'a>>>(value: T) -> Self {FilePath ::new(value).into()}
23 pub fn new_special_not_file<T: Into<SpecialNotFilePath<'a>>>(value: T) -> Self {SpecialNotFilePath::new(value).into()}
24 pub fn new_non_special <T: Into<NonSpecialPath <'a>>>(value: T) -> Self {NonSpecialPath ::new(value).into()}
25 pub fn new_opaque <T: Into<OpaquePath <'a>>>(value: T) -> Self {OpaquePath ::new(value).into()}
26
27
28
29 pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T, r#type: PathType) -> Self {
33 match r#type {
34 PathType::Segmented(x) => unsafe {Self::new_segmented_unchecked(value, x)},
35 PathType::Opaque => unsafe {Self::new_opaque_unchecked (value )},
36 }
37 }
38
39 pub unsafe fn new_segmented_unchecked <T: Into<Cow<'a, str>>>(value: T, r#type: SegmentedPathType) -> Self {unsafe {SegmentedPath::new_unchecked(value, r#type)}.into()}
43
44 pub unsafe fn new_file_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePath ::new_unchecked(value)}.into()}
48
49 pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePath::new_unchecked(value)}.into()}
53
54 pub unsafe fn new_non_special_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {NonSpecialPath ::new_unchecked(value)}.into()}
58
59 pub unsafe fn new_opaque_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {OpaquePath ::new_unchecked(value)}.into()}
63
64
65
66
67 pub fn as_str(&self) -> &str {
69 match self {
70 Self::Segmented(x) => x.as_str(),
71 Self::Opaque (x) => x.as_str(),
72 }
73 }
74
75 pub fn r#type(&self) -> PathType {
77 match self {
78 Self::Segmented(x) => x.r#type().into(),
79 Self::Opaque (_) => PathType::Opaque,
80 }
81 }
82
83
84
85 pub fn is_segmented (&self) -> bool {matches!(self, Self::Segmented(_))}
86 pub fn is_file (&self) -> bool {matches!(self, Self::Segmented(SegmentedPath::File (_)))}
87 pub fn is_special_not_file(&self) -> bool {matches!(self, Self::Segmented(SegmentedPath::SpecialNotFile(_)))}
88 pub fn is_non_special (&self) -> bool {matches!(self, Self::Segmented(SegmentedPath::NonSpecial (_)))}
89 pub fn is_opaque (&self) -> bool {matches!(self, Self::Opaque (_))}
90
91
92
93 pub fn segmented(self) -> Option<SegmentedPath<'a>> {
95 match self {
96 Self::Segmented(x) => Some(x),
97 Self::Opaque (_) => None
98 }
99 }
100
101 pub fn file (self) -> Option<FilePath <'a>> {self.segmented()?.file ()}
102 pub fn special_not_file(self) -> Option<SpecialNotFilePath<'a>> {self.segmented()?.special_not_file()}
103 pub fn non_special (self) -> Option<NonSpecialPath <'a>> {self.segmented()?.non_special ()}
104
105 pub fn opaque(self) -> Option<OpaquePath<'a>> {
107 match self {
108 Self::Segmented(_) => None,
109 Self::Opaque (x) => Some(x),
110 }
111 }
112
113
114
115 pub fn into_inner(self) -> Cow<'a, str> {
117 match self {
118 Self::Segmented(x) => x.into_inner(),
119 Self::Opaque (x) => x.into_inner(),
120 }
121 }
122
123 pub fn into_owned(self) -> Path<'static> {
125 match self {
126 Self::Segmented(x) => x.into_owned().into(),
127 Self::Opaque (x) => x.into_owned().into(),
128 }
129 }
130
131 pub fn borrowed(&self) -> Path<'_> {
133 match self {
134 Self::Segmented(x) => x.borrowed().into() ,
135 Self::Opaque (x) => x.borrowed().into() ,
136 }
137 }
138}
139
140
141
142impl<'a> From<SegmentedPath <'a>> for Path<'a> {fn from(value: SegmentedPath <'a>) -> Self {Self::Segmented(value )}}
143impl<'a> From<FilePath <'a>> for Path<'a> {fn from(value: FilePath <'a>) -> Self {Self::Segmented(value.into())}}
144impl<'a> From<SpecialNotFilePath<'a>> for Path<'a> {fn from(value: SpecialNotFilePath<'a>) -> Self {Self::Segmented(value.into())}}
145impl<'a> From<NonSpecialPath <'a>> for Path<'a> {fn from(value: NonSpecialPath <'a>) -> Self {Self::Segmented(value.into())}}
146impl<'a> From<OpaquePath <'a>> for Path<'a> {fn from(value: OpaquePath <'a>) -> Self {Self::Opaque (value )}}