Skip to main content

yazi_shared/strand/
conversion.rs

1use std::{borrow::Cow, ffi::{OsStr, OsString}};
2
3use crate::{path::{PathBufDyn, PathCow, PathDyn}, strand::{Strand, StrandBuf, StrandCow}, url::AsUrl};
4
5// --- AsStrand
6pub trait AsStrand {
7	fn as_strand(&self) -> Strand<'_>;
8}
9
10impl AsStrand for [u8] {
11	fn as_strand(&self) -> Strand<'_> { Strand::Bytes(self) }
12}
13
14impl AsStrand for &[u8] {
15	fn as_strand(&self) -> Strand<'_> { Strand::Bytes(self) }
16}
17
18impl AsStrand for str {
19	fn as_strand(&self) -> Strand<'_> { Strand::Utf8(self) }
20}
21
22impl AsStrand for &str {
23	fn as_strand(&self) -> Strand<'_> { Strand::Utf8(self) }
24}
25
26impl AsStrand for String {
27	fn as_strand(&self) -> Strand<'_> { Strand::Utf8(self) }
28}
29
30impl AsStrand for &String {
31	fn as_strand(&self) -> Strand<'_> { Strand::Utf8(self) }
32}
33
34impl AsStrand for OsStr {
35	fn as_strand(&self) -> Strand<'_> { Strand::Os(self) }
36}
37
38impl AsStrand for &OsStr {
39	fn as_strand(&self) -> Strand<'_> { Strand::Os(self) }
40}
41
42impl AsStrand for OsString {
43	fn as_strand(&self) -> Strand<'_> { Strand::Os(self) }
44}
45
46impl AsStrand for &std::path::Path {
47	fn as_strand(&self) -> Strand<'_> { Strand::Os(self.as_os_str()) }
48}
49
50impl AsStrand for &std::path::PathBuf {
51	fn as_strand(&self) -> Strand<'_> { Strand::Os(self.as_os_str()) }
52}
53
54impl AsStrand for &typed_path::UnixPath {
55	fn as_strand(&self) -> Strand<'_> { Strand::Bytes(self.as_bytes()) }
56}
57
58impl AsStrand for crate::path::Components<'_> {
59	fn as_strand(&self) -> Strand<'_> { self.strand() }
60}
61
62impl AsStrand for Cow<'_, [u8]> {
63	fn as_strand(&self) -> Strand<'_> { Strand::Bytes(self) }
64}
65
66impl AsStrand for Cow<'_, OsStr> {
67	fn as_strand(&self) -> Strand<'_> { Strand::Os(self) }
68}
69
70impl AsStrand for PathDyn<'_> {
71	fn as_strand(&self) -> Strand<'_> {
72		match self {
73			Self::Os(p) => Strand::Os(p.as_os_str()),
74			Self::Unix(p) => Strand::Bytes(p.as_bytes()),
75		}
76	}
77}
78
79impl AsStrand for PathBufDyn {
80	fn as_strand(&self) -> Strand<'_> {
81		match self {
82			Self::Os(p) => Strand::Os(p.as_os_str()),
83			Self::Unix(p) => Strand::Bytes(p.as_bytes()),
84		}
85	}
86}
87
88impl AsStrand for &PathBufDyn {
89	fn as_strand(&self) -> Strand<'_> { (**self).as_strand() }
90}
91
92impl AsStrand for PathCow<'_> {
93	fn as_strand(&self) -> Strand<'_> {
94		match self {
95			Self::Borrowed(p) => p.as_strand(),
96			Self::Owned(p) => p.as_strand(),
97		}
98	}
99}
100
101impl AsStrand for &PathCow<'_> {
102	fn as_strand(&self) -> Strand<'_> { (**self).as_strand() }
103}
104
105impl AsStrand for Strand<'_> {
106	fn as_strand(&self) -> Strand<'_> { *self }
107}
108
109impl AsStrand for StrandBuf {
110	fn as_strand(&self) -> Strand<'_> {
111		match self {
112			Self::Os(s) => Strand::Os(s),
113			Self::Utf8(s) => Strand::Utf8(s),
114			Self::Bytes(b) => Strand::Bytes(b),
115		}
116	}
117}
118
119impl AsStrand for &StrandBuf {
120	fn as_strand(&self) -> Strand<'_> { (**self).as_strand() }
121}
122
123impl AsStrand for StrandCow<'_> {
124	fn as_strand(&self) -> Strand<'_> {
125		match self {
126			StrandCow::Borrowed(s) => *s,
127			StrandCow::Owned(s) => s.as_strand(),
128		}
129	}
130}
131
132impl AsStrand for &StrandCow<'_> {
133	fn as_strand(&self) -> Strand<'_> { (**self).as_strand() }
134}
135
136// --- ToStrand
137pub trait ToStrand {
138	fn to_strand(&self) -> StrandCow<'_>;
139}
140
141impl ToStrand for String {
142	fn to_strand(&self) -> StrandCow<'_> { self.as_strand().into() }
143}
144
145impl<T> ToStrand for T
146where
147	T: AsUrl,
148{
149	fn to_strand(&self) -> StrandCow<'_> { self.as_url().components().strand() }
150}
151
152// --- IntoStrand
153pub trait IntoStrand {
154	fn into_strand(self) -> StrandBuf;
155}
156
157impl IntoStrand for PathBufDyn {
158	fn into_strand(self) -> StrandBuf {
159		match self {
160			Self::Os(p) => StrandBuf::Os(p.into_os_string()),
161			Self::Unix(p) => StrandBuf::Bytes(p.into_vec()),
162		}
163	}
164}
165
166impl IntoStrand for &PathBufDyn {
167	fn into_strand(self) -> StrandBuf {
168		match self {
169			PathBufDyn::Os(p) => StrandBuf::Os(p.as_os_str().to_owned()),
170			PathBufDyn::Unix(p) => StrandBuf::Bytes(p.as_bytes().to_owned()),
171		}
172	}
173}