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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use crate::validator::path_history::{BoundaryChecked, Canonicalized, PathHistory, Raw};
use crate::{Result, StrictPathError};
use std::cmp::Ordering;
use std::ffi::OsStr;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::Arc;
/// SUMMARY:
/// Hold a validated, system-facing filesystem path guaranteed to be within a `PathBoundary`.
///
/// DETAILS:
/// Use when you need system-facing I/O with safety proofs. For user-facing display and rooted
/// virtual operations prefer `VirtualPath`. Operations like `strict_join` and
/// `strictpath_parent` preserve guarantees. `Display` shows the real system path. String
/// accessors are prefixed with `strictpath_` to avoid confusion.
#[derive(Clone)]
pub struct StrictPath<Marker = ()> {
path: PathHistory<((Raw, Canonicalized), BoundaryChecked)>,
boundary: Arc<crate::PathBoundary<Marker>>,
_marker: PhantomData<Marker>,
}
impl<Marker> StrictPath<Marker> {
/// SUMMARY:
/// Create the base `StrictPath` anchored at the provided boundary directory.
///
/// PARAMETERS:
/// - `dir_path` (`AsRef<Path>`): Boundary directory (must exist).
///
/// RETURNS:
/// - `Result<StrictPath<Marker>>`: Base path ("" join) within the boundary.
///
/// ERRORS:
/// - `StrictPathError::InvalidRestriction`: If the boundary cannot be created/validated.
///
/// NOTE: Prefer passing `PathBoundary` in reusable flows.
pub fn with_boundary<P: AsRef<Path>>(dir_path: P) -> Result<Self> {
let boundary = crate::PathBoundary::try_new(dir_path)?;
boundary.strict_join("")
}
/// SUMMARY:
/// Create the base `StrictPath`, creating the boundary directory if missing.
pub fn with_boundary_create<P: AsRef<Path>>(dir_path: P) -> Result<Self> {
let boundary = crate::PathBoundary::try_new_create(dir_path)?;
boundary.strict_join("")
}
pub(crate) fn new(
boundary: Arc<crate::PathBoundary<Marker>>,
validated_path: PathHistory<((Raw, Canonicalized), BoundaryChecked)>,
) -> Self {
Self {
path: validated_path,
boundary,
_marker: PhantomData,
}
}
#[inline]
pub(crate) fn boundary(&self) -> &crate::PathBoundary<Marker> {
&self.boundary
}
#[inline]
pub(crate) fn path(&self) -> &Path {
&self.path
}
/// SUMMARY:
/// Return a lossy `String` view of the system path. Prefer `interop_path()` for interop.
#[inline]
pub fn strictpath_to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
self.path.to_string_lossy()
}
/// SUMMARY:
/// Return the underlying system path as `&str` if valid UTF‑8; otherwise `None`.
#[inline]
pub fn strictpath_to_str(&self) -> Option<&str> {
self.path.to_str()
}
/// SUMMARY:
/// Return the underlying system path as `&OsStr` for allocation‑free `AsRef<Path>` interop.
#[inline]
pub fn interop_path(&self) -> &OsStr {
self.path.as_os_str()
}
/// SUMMARY:
/// Return a `Display` wrapper that shows the real system path.
#[inline]
pub fn strictpath_display(&self) -> std::path::Display<'_> {
self.path.display()
}
/// SUMMARY:
/// Consume and return the inner `PathBuf` (escape hatch). Prefer `interop_path()` to borrow.
#[inline]
pub fn unstrict(self) -> PathBuf {
self.path.into_inner()
}
/// SUMMARY:
/// Convert this `StrictPath` into a user‑facing `VirtualPath`.
#[inline]
pub fn virtualize(self) -> crate::path::virtual_path::VirtualPath<Marker> {
crate::path::virtual_path::VirtualPath::new(self)
}
/// SUMMARY:
/// Consume and return the associated `PathBoundary` (infallible).
#[inline]
pub fn try_into_boundary(self) -> crate::PathBoundary<Marker> {
// Clone the underlying boundary reference (cheap, small struct)
self.boundary.as_ref().clone()
}
/// SUMMARY:
/// Consume and return the `PathBoundary`, creating the directory if missing (best‑effort).
#[inline]
pub fn try_into_boundary_create(self) -> crate::PathBoundary<Marker> {
let boundary = self.boundary.as_ref().clone();
if !boundary.exists() {
// Best-effort create; ignore error and let later operations surface it
let _ = std::fs::create_dir_all(boundary.as_ref());
}
boundary
}
/// SUMMARY:
/// Join a path segment and re-validate against the boundary.
///
/// NOTE:
/// Never call `Path::join` on a leaked system path (e.g., from `interop_path()` or `unstrict()`); always re-validate through this method.
///
/// PARAMETERS:
/// - `path` (`AsRef<Path>`): Segment or absolute path to validate.
///
/// RETURNS:
/// - `Result<StrictPath<Marker>>`: Validated path inside the boundary.
///
/// ERRORS:
/// - `StrictPathError::WindowsShortName` (windows), `StrictPathError::PathResolutionError`,
/// `StrictPathError::PathEscapesBoundary`.
#[inline]
pub fn strict_join<P: AsRef<Path>>(&self, path: P) -> Result<Self> {
let new_systempath = self.path.join(path);
self.boundary.strict_join(new_systempath)
}
/// SUMMARY:
/// Return the parent as a new `StrictPath`, or `None` at the boundary root.
pub fn strictpath_parent(&self) -> Result<Option<Self>> {
match self.path.parent() {
Some(p) => match self.boundary.strict_join(p) {
Ok(p) => Ok(Some(p)),
Err(e) => Err(e),
},
None => Ok(None),
}
}
/// SUMMARY:
/// Return a new path with file name changed, re‑validating against the boundary.
#[inline]
pub fn strictpath_with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> Result<Self> {
let new_systempath = self.path.with_file_name(file_name);
self.boundary.strict_join(new_systempath)
}
/// SUMMARY:
/// Return a new path with extension changed; error at the boundary root.
pub fn strictpath_with_extension<S: AsRef<OsStr>>(&self, extension: S) -> Result<Self> {
let system_path = &self.path;
if system_path.file_name().is_none() {
return Err(StrictPathError::path_escapes_boundary(
self.path.to_path_buf(),
self.boundary.path().to_path_buf(),
));
}
let new_systempath = system_path.with_extension(extension);
self.boundary.strict_join(new_systempath)
}
/// Returns the file name component of the system path, if any.
#[inline]
pub fn strictpath_file_name(&self) -> Option<&OsStr> {
self.path.file_name()
}
/// Returns the file stem of the system path, if any.
#[inline]
pub fn strictpath_file_stem(&self) -> Option<&OsStr> {
self.path.file_stem()
}
/// Returns the extension of the system path, if any.
#[inline]
pub fn strictpath_extension(&self) -> Option<&OsStr> {
self.path.extension()
}
/// Returns `true` if the system path starts with the given prefix.
#[inline]
pub fn strictpath_starts_with<P: AsRef<Path>>(&self, p: P) -> bool {
self.path.starts_with(p.as_ref())
}
/// Returns `true` if the system path ends with the given suffix.
#[inline]
pub fn strictpath_ends_with<P: AsRef<Path>>(&self, p: P) -> bool {
self.path.ends_with(p.as_ref())
}
/// Returns `true` if the system path exists.
pub fn exists(&self) -> bool {
self.path.exists()
}
/// Returns `true` if the system path is a file.
pub fn is_file(&self) -> bool {
self.path.is_file()
}
/// Returns `true` if the system path is a directory.
pub fn is_dir(&self) -> bool {
self.path.is_dir()
}
/// Returns the metadata for the system path.
pub fn metadata(&self) -> std::io::Result<std::fs::Metadata> {
std::fs::metadata(&self.path)
}
/// SUMMARY:
/// Read directory entries at this path (discovery). Re‑join names through strict/virtual APIs before I/O.
pub fn read_dir(&self) -> std::io::Result<std::fs::ReadDir> {
std::fs::read_dir(&self.path)
}
/// Reads the file contents as `String`.
pub fn read_to_string(&self) -> std::io::Result<String> {
std::fs::read_to_string(&self.path)
}
/// Reads the file contents as raw bytes.
#[deprecated(since = "0.1.0-alpha.5", note = "Use read() instead")]
pub fn read_bytes(&self) -> std::io::Result<Vec<u8>> {
std::fs::read(&self.path)
}
/// Writes raw bytes to the file, creating it if it does not exist.
#[deprecated(since = "0.1.0-alpha.5", note = "Use write(...) instead")]
pub fn write_bytes(&self, data: &[u8]) -> std::io::Result<()> {
std::fs::write(&self.path, data)
}
/// Writes a UTF-8 string to the file, creating it if it does not exist.
#[deprecated(since = "0.1.0-alpha.5", note = "Use write(...) instead")]
pub fn write_string(&self, data: &str) -> std::io::Result<()> {
std::fs::write(&self.path, data)
}
/// Reads the file contents as raw bytes (replacement for `read_bytes`).
#[inline]
pub fn read(&self) -> std::io::Result<Vec<u8>> {
std::fs::read(&self.path)
}
/// SUMMARY:
/// Write bytes to the file (create if missing). Accepts any `AsRef<[u8]>` (e.g., `&str`, `&[u8]`).
#[inline]
pub fn write<C: AsRef<[u8]>>(&self, contents: C) -> std::io::Result<()> {
std::fs::write(&self.path, contents)
}
/// Creates all directories in the system path if missing (like `std::fs::create_dir_all`).
pub fn create_dir_all(&self) -> std::io::Result<()> {
std::fs::create_dir_all(&self.path)
}
/// Creates the directory at the system path (non-recursive, like `std::fs::create_dir`).
///
/// Fails if the parent directory does not exist. Use `create_dir_all` to
/// create missing parent directories recursively.
pub fn create_dir(&self) -> std::io::Result<()> {
std::fs::create_dir(&self.path)
}
/// SUMMARY:
/// Create only the immediate parent directory (non‑recursive). `Ok(())` at the boundary root.
pub fn create_parent_dir(&self) -> std::io::Result<()> {
match self.strictpath_parent() {
Ok(Some(parent)) => parent.create_dir(),
Ok(None) => Ok(()),
Err(StrictPathError::PathEscapesBoundary { .. }) => Ok(()),
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
}
/// SUMMARY:
/// Recursively create all missing directories up to the immediate parent. `Ok(())` at root.
pub fn create_parent_dir_all(&self) -> std::io::Result<()> {
match self.strictpath_parent() {
Ok(Some(parent)) => parent.create_dir_all(),
Ok(None) => Ok(()),
Err(StrictPathError::PathEscapesBoundary { .. }) => Ok(()),
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
}
/// SUMMARY:
/// Create a symbolic link at this location pointing to `target` (same boundary required).
/// On Windows, file vs directory symlink is selected by target metadata (or best‑effort when missing).
pub fn strict_symlink(&self, link_path: &Self) -> std::io::Result<()> {
if self.boundary.path() != link_path.boundary.path() {
let err = StrictPathError::path_escapes_boundary(
link_path.path().to_path_buf(),
self.boundary.path().to_path_buf(),
);
return Err(std::io::Error::new(std::io::ErrorKind::Other, err));
}
#[cfg(unix)]
{
std::os::unix::fs::symlink(self.path(), link_path.path())?;
}
#[cfg(windows)]
{
create_windows_symlink(self.path(), link_path.path())?;
}
Ok(())
}
/// SUMMARY:
/// Create a hard link at `link_path` pointing to this path (same boundary; caller creates parents).
pub fn strict_hard_link(&self, link_path: &Self) -> std::io::Result<()> {
if self.boundary.path() != link_path.boundary.path() {
let err = StrictPathError::path_escapes_boundary(
link_path.path().to_path_buf(),
self.boundary.path().to_path_buf(),
);
return Err(std::io::Error::new(std::io::ErrorKind::Other, err));
}
std::fs::hard_link(self.path(), link_path.path())?;
Ok(())
}
/// SUMMARY:
/// Rename/move within the same boundary. Relative destinations are siblings; absolute are validated.
/// Parents are not created automatically.
pub fn strict_rename<P: AsRef<Path>>(&self, dest: P) -> std::io::Result<()> {
let dest_ref = dest.as_ref();
// Compute destination under the parent directory for relative paths; allow absolute too
let dest_path = if dest_ref.is_absolute() {
match self.boundary.strict_join(dest_ref) {
Ok(p) => p,
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
} else {
let parent = match self.strictpath_parent() {
Ok(Some(p)) => p,
Ok(None) => match self.boundary.strict_join("") {
Ok(root) => root,
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
},
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
};
match parent.strict_join(dest_ref) {
Ok(p) => p,
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
};
std::fs::rename(self.path(), dest_path.path())
}
/// SUMMARY:
/// Copy within the same boundary. Relative destinations are siblings; absolute are validated.
/// Parents are not created automatically. Returns bytes copied.
pub fn strict_copy<P: AsRef<Path>>(&self, dest: P) -> std::io::Result<u64> {
let dest_ref = dest.as_ref();
// Compute destination under the parent directory for relative paths; allow absolute too
let dest_path = if dest_ref.is_absolute() {
match self.boundary.strict_join(dest_ref) {
Ok(p) => p,
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
} else {
let parent = match self.strictpath_parent() {
Ok(Some(p)) => p,
Ok(None) => match self.boundary.strict_join("") {
Ok(root) => root,
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
},
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
};
match parent.strict_join(dest_ref) {
Ok(p) => p,
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
};
std::fs::copy(self.path(), dest_path.path())
}
/// SUMMARY:
/// Remove the file at this path.
pub fn remove_file(&self) -> std::io::Result<()> {
std::fs::remove_file(&self.path)
}
/// SUMMARY:
/// Remove the directory at this path.
pub fn remove_dir(&self) -> std::io::Result<()> {
std::fs::remove_dir(&self.path)
}
/// SUMMARY:
/// Recursively remove the directory and its contents.
pub fn remove_dir_all(&self) -> std::io::Result<()> {
std::fs::remove_dir_all(&self.path)
}
}
#[cfg(feature = "serde")]
impl<Marker> serde::Serialize for StrictPath<Marker> {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.strictpath_to_string_lossy().as_ref())
}
}
impl<Marker> fmt::Debug for StrictPath<Marker> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StrictPath")
.field("path", &self.path)
.field("boundary", &self.boundary.path())
.field("marker", &std::any::type_name::<Marker>())
.finish()
}
}
#[cfg(windows)]
fn create_windows_symlink(src: &Path, link: &Path) -> std::io::Result<()> {
use std::os::windows::fs::{symlink_dir, symlink_file};
match std::fs::metadata(src) {
Ok(metadata) => {
if metadata.is_dir() {
symlink_dir(src, link)
} else {
symlink_file(src, link)
}
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
match symlink_file(src, link) {
Ok(()) => Ok(()),
Err(file_err) => {
if let Some(code) = file_err.raw_os_error() {
const ERROR_DIRECTORY: i32 = 267; // target resolved as directory
if code == ERROR_DIRECTORY {
return symlink_dir(src, link);
}
}
Err(file_err)
}
}
}
Err(err) => Err(err),
}
}
impl<Marker> PartialEq for StrictPath<Marker> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.path.as_ref() == other.path.as_ref()
}
}
impl<Marker> Eq for StrictPath<Marker> {}
impl<Marker> Hash for StrictPath<Marker> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.path.hash(state);
}
}
impl<Marker> PartialOrd for StrictPath<Marker> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<Marker> Ord for StrictPath<Marker> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.path.cmp(&other.path)
}
}
impl<T: AsRef<Path>, Marker> PartialEq<T> for StrictPath<Marker> {
fn eq(&self, other: &T) -> bool {
self.path.as_ref() == other.as_ref()
}
}
impl<T: AsRef<Path>, Marker> PartialOrd<T> for StrictPath<Marker> {
fn partial_cmp(&self, other: &T) -> Option<Ordering> {
Some(self.path.as_ref().cmp(other.as_ref()))
}
}
impl<Marker> PartialEq<crate::path::virtual_path::VirtualPath<Marker>> for StrictPath<Marker> {
#[inline]
fn eq(&self, other: &crate::path::virtual_path::VirtualPath<Marker>) -> bool {
self.path.as_ref() == other.interop_path()
}
}