1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
pub use self::non_utf8::*;
pub use self::utf8::*;
mod non_utf8 {
use core::any::TypeId;
use core::fmt;
use core::hash::Hasher;
use crate::common::{CheckedPathError, Encoding, Path, PathBuf};
use crate::native::NativeEncoding;
use crate::no_std_compat::*;
use crate::private;
/// [`Path`] that has the platform's encoding during compilation.
///
/// # Examples
///
/// ```
/// use typed_path::PlatformPath;
///
/// // You can create the path like normal, but it is a distinct encoding from Unix/Windows
/// let path = PlatformPath::new("some/path");
///
/// // The path will still behave like normal and even report its underlying encoding
/// assert_eq!(path.has_unix_encoding(), cfg!(unix));
/// assert_eq!(path.has_windows_encoding(), cfg!(windows));
///
/// // It can still be converted into specific platform paths
/// let unix_path = path.with_unix_encoding();
/// let win_path = path.with_windows_encoding();
/// ```
pub type PlatformPath = Path<PlatformEncoding>;
/// [`PathBuf`] that has the platform's encoding during compilation.
///
/// # Examples
///
/// ```
/// use typed_path::PlatformPathBuf;
///
/// // You can create the pathbuf like normal, but it is a distinct encoding from Unix/Windows
/// let path = PlatformPathBuf::from("some/path");
///
/// // The path will still behave like normal and even report its underlying encoding
/// assert_eq!(path.has_unix_encoding(), cfg!(unix));
/// assert_eq!(path.has_windows_encoding(), cfg!(windows));
///
/// // It can still be converted into specific platform paths
/// let unix_path = path.with_unix_encoding();
/// let win_path = path.with_windows_encoding();
/// ```
pub type PlatformPathBuf = PathBuf<PlatformEncoding>;
/// Represents an abstraction of [`Encoding`] that represents the current platform encoding.
///
/// This differs from [`NativeEncoding`] in that it is its own struct instead of a type alias
/// to the platform-specific encoding, and can therefore be used to enforce more strict
/// compile-time checks of encodings without needing to leverage conditional configs.
///
/// # Examples
///
/// ```
/// use core::any::TypeId;
/// use typed_path::{PlatformEncoding, UnixEncoding, WindowsEncoding};
///
/// // The platform encoding is considered a distinct type from Unix/Windows encodings.
/// assert_ne!(TypeId::of::<PlatformEncoding>(), TypeId::of::<UnixEncoding>());
/// assert_ne!(TypeId::of::<PlatformEncoding>(), TypeId::of::<WindowsEncoding>());
/// ```
#[derive(Copy, Clone)]
pub struct PlatformEncoding;
impl private::Sealed for PlatformEncoding {}
impl Encoding for PlatformEncoding {
type Components<'a> = <NativeEncoding as Encoding>::Components<'a>;
fn label() -> &'static str {
NativeEncoding::label()
}
fn components(path: &[u8]) -> Self::Components<'_> {
<NativeEncoding as Encoding>::components(path)
}
fn hash<H: Hasher>(path: &[u8], h: &mut H) {
<NativeEncoding as Encoding>::hash(path, h)
}
fn push(current_path: &mut Vec<u8>, path: &[u8]) {
<NativeEncoding as Encoding>::push(current_path, path);
}
fn push_checked(current_path: &mut Vec<u8>, path: &[u8]) -> Result<(), CheckedPathError> {
<NativeEncoding as Encoding>::push_checked(current_path, path)
}
}
impl fmt::Debug for PlatformEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformEncoding").finish()
}
}
impl fmt::Display for PlatformEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PlatformEncoding")
}
}
impl<T> Path<T>
where
T: Encoding,
{
/// Returns true if the encoding is the platform abstraction ([`PlatformEncoding`]),
/// otherwise returns false.
///
/// # Examples
///
/// ```
/// use typed_path::{PlatformPath, UnixPath, WindowsPath};
///
/// assert!(PlatformPath::new("/some/path").has_platform_encoding());
/// assert!(!UnixPath::new("/some/path").has_platform_encoding());
/// assert!(!WindowsPath::new("/some/path").has_platform_encoding());
/// ```
pub fn has_platform_encoding(&self) -> bool
where
T: 'static,
{
TypeId::of::<T>() == TypeId::of::<PlatformEncoding>()
}
/// Creates an owned [`PathBuf`] like `self` but using [`PlatformEncoding`].
///
/// See [`Path::with_encoding`] for more information.
pub fn with_platform_encoding(&self) -> PathBuf<PlatformEncoding> {
self.with_encoding()
}
/// Creates an owned [`PathBuf`] like `self` but using [`PlatformEncoding`], ensuring it is
/// a valid platform path.
///
/// See [`Path::with_encoding_checked`] for more information.
pub fn with_platform_encoding_checked(
&self,
) -> Result<PathBuf<PlatformEncoding>, CheckedPathError> {
self.with_encoding_checked()
}
}
}
mod utf8 {
use core::any::TypeId;
use core::fmt;
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::path::{Path as StdPath, PathBuf as StdPathBuf};
use crate::common::{CheckedPathError, Utf8Encoding, Utf8Path, Utf8PathBuf};
use crate::native::Utf8NativeEncoding;
use crate::no_std_compat::*;
use crate::private;
/// [`Utf8Path`] that has the platform's encoding during compilation.
///
/// # Examples
///
/// ```
/// use typed_path::Utf8PlatformPath;
///
/// // You can create the path like normal, but it is a distinct encoding from Unix/Windows
/// let path = Utf8PlatformPath::new("some/path");
///
/// // The path will still behave like normal and even report its underlying encoding
/// assert_eq!(path.has_unix_encoding(), cfg!(unix));
/// assert_eq!(path.has_windows_encoding(), cfg!(windows));
///
/// // It can still be converted into specific platform paths
/// let unix_path = path.with_unix_encoding();
/// let win_path = path.with_windows_encoding();
/// ```
pub type Utf8PlatformPath = Utf8Path<Utf8PlatformEncoding>;
/// [`Utf8PathBuf`] that has the platform's encoding during compilation.
///
/// # Examples
///
/// ```
/// use typed_path::Utf8PlatformPathBuf;
///
/// // You can create the pathbuf like normal, but it is a distinct encoding from Unix/Windows
/// let path = Utf8PlatformPathBuf::from("some/path");
///
/// // The path will still behave like normal and even report its underlying encoding
/// assert_eq!(path.has_unix_encoding(), cfg!(unix));
/// assert_eq!(path.has_windows_encoding(), cfg!(windows));
///
/// // It can still be converted into specific platform paths
/// let unix_path = path.with_unix_encoding();
/// let win_path = path.with_windows_encoding();
/// ```
pub type Utf8PlatformPathBuf = Utf8PathBuf<Utf8PlatformEncoding>;
/// Represents an abstraction of [`Utf8Encoding`] that represents the current platform
/// encoding.
///
/// This differs from [`Utf8NativeEncoding`] in that it is its own struct instead of a type
/// alias to the platform-specific encoding, and can therefore be used to enforce more strict
/// compile-time checks of encodings without needing to leverage conditional configs.
///
/// # Examples
///
/// ```
/// use core::any::TypeId;
/// use typed_path::{Utf8PlatformEncoding, Utf8UnixEncoding, Utf8WindowsEncoding};
///
/// // The UTF8 platform encoding is considered a distinct type from UTF8 Unix/Windows encodings.
/// assert_ne!(TypeId::of::<Utf8PlatformEncoding>(), TypeId::of::<Utf8UnixEncoding>());
/// assert_ne!(TypeId::of::<Utf8PlatformEncoding>(), TypeId::of::<Utf8WindowsEncoding>());
/// ```
#[derive(Copy, Clone)]
pub struct Utf8PlatformEncoding;
impl private::Sealed for Utf8PlatformEncoding {}
impl Utf8Encoding for Utf8PlatformEncoding {
type Components<'a> = <Utf8NativeEncoding as Utf8Encoding>::Components<'a>;
fn label() -> &'static str {
Utf8NativeEncoding::label()
}
fn components(path: &str) -> Self::Components<'_> {
<Utf8NativeEncoding as Utf8Encoding>::components(path)
}
fn hash<H: Hasher>(path: &str, h: &mut H) {
<Utf8NativeEncoding as Utf8Encoding>::hash(path, h)
}
fn push(current_path: &mut String, path: &str) {
<Utf8NativeEncoding as Utf8Encoding>::push(current_path, path);
}
fn push_checked(current_path: &mut String, path: &str) -> Result<(), CheckedPathError> {
<Utf8NativeEncoding as Utf8Encoding>::push_checked(current_path, path)
}
}
impl fmt::Debug for Utf8PlatformEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Utf8PlatformEncoding").finish()
}
}
impl fmt::Display for Utf8PlatformEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Utf8PlatformEncoding")
}
}
impl<T> Utf8Path<T>
where
T: Utf8Encoding,
{
/// Returns true if the encoding is the platform abstraction ([`Utf8PlatformEncoding`]),
/// otherwise returns false.
///
/// # Examples
///
/// ```
/// use typed_path::{Utf8PlatformPath, Utf8UnixPath, Utf8WindowsPath};
///
/// assert!(Utf8PlatformPath::new("/some/path").has_platform_encoding());
/// assert!(!Utf8UnixPath::new("/some/path").has_platform_encoding());
/// assert!(!Utf8WindowsPath::new("/some/path").has_platform_encoding());
/// ```
pub fn has_platform_encoding(&self) -> bool
where
T: 'static,
{
TypeId::of::<T>() == TypeId::of::<Utf8PlatformEncoding>()
}
/// Creates an owned [`Utf8PathBuf`] like `self` but using [`Utf8PlatformEncoding`].
///
/// See [`Utf8Path::with_encoding`] for more information.
pub fn with_platform_encoding(&self) -> Utf8PathBuf<Utf8PlatformEncoding> {
self.with_encoding()
}
/// Creates an owned [`Utf8PathBuf`] like `self` but using [`Utf8PlatformEncoding`],
/// ensuring it is a valid platform path.
///
/// See [`Utf8Path::with_encoding_checked`] for more information.
pub fn with_platform_encoding_checked(
&self,
) -> Result<Utf8PathBuf<Utf8PlatformEncoding>, CheckedPathError> {
self.with_encoding_checked()
}
}
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl AsRef<StdPath> for Utf8PlatformPath {
/// Converts a platform utf8 path (based on compilation family) into [`std::path::Path`].
///
/// ```
/// use typed_path::Utf8PlatformPath;
/// use std::path::Path;
///
/// let platform_path = Utf8PlatformPath::new("some_file.txt");
/// let std_path: &Path = platform_path.as_ref();
///
/// assert_eq!(std_path, Path::new("some_file.txt"));
/// ```
fn as_ref(&self) -> &StdPath {
StdPath::new(self.as_str())
}
}
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl AsRef<StdPath> for Utf8PlatformPathBuf {
/// Converts a platform utf8 pathbuf (based on compilation family) into [`std::path::Path`].
///
/// ```
/// use typed_path::Utf8PlatformPathBuf;
/// use std::path::Path;
///
/// let platform_path_buf = Utf8PlatformPathBuf::from("some_file.txt");
/// let std_path: &Path = platform_path_buf.as_ref();
///
/// assert_eq!(std_path, Path::new("some_file.txt"));
/// ```
fn as_ref(&self) -> &StdPath {
StdPath::new(self.as_str())
}
}
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl From<Utf8PlatformPathBuf> for StdPathBuf {
/// Converts a platform utf8 pathbuf (based on compilation family) into [`std::path::PathBuf`].
///
/// ```
/// use typed_path::Utf8PlatformPathBuf;
/// use std::path::PathBuf;
///
/// let platform_path_buf = Utf8PlatformPathBuf::from("some_file.txt");
/// let std_path_buf = PathBuf::from(platform_path_buf);
///
/// assert_eq!(std_path_buf, PathBuf::from("some_file.txt"));
/// ```
fn from(utf8_platform_path_buf: Utf8PlatformPathBuf) -> StdPathBuf {
StdPathBuf::from(utf8_platform_path_buf.into_string())
}
}
}