-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
lib.rs
59 lines (50 loc) · 1.71 KB
/
lib.rs
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
#![feature(assert_matches)]
mod absolute_system_path_buf;
mod anchored_system_path_buf;
mod relative_system_path_buf;
mod relative_unix_path;
use std::path::{Path, PathBuf};
pub use absolute_system_path_buf::AbsoluteSystemPathBuf;
pub use anchored_system_path_buf::AnchoredSystemPathBuf;
use path_slash::{PathBufExt, PathExt};
pub use relative_system_path_buf::RelativeSystemPathBuf;
pub use relative_unix_path::RelativeUnixPath;
use thiserror::Error;
// Custom error type for path validation errors
#[derive(Debug, Error)]
pub enum PathValidationError {
#[error("Path is non-UTF-8: {0}")]
InvalidUnicode(PathBuf),
#[error("Path is not absolute: {0}")]
NotAbsolute(PathBuf),
#[error("Path is not relative: {0}")]
NotRelative(PathBuf),
#[error("Path {0} is not parent of {1}")]
NotParent(String, String),
}
trait IntoSystem {
fn into_system(self) -> Result<PathBuf, PathValidationError>;
}
trait IntoUnix {
fn into_unix(self) -> Result<PathBuf, PathValidationError>;
}
impl IntoSystem for &Path {
fn into_system(self) -> Result<PathBuf, PathValidationError> {
let path_str = self
.to_str()
.ok_or_else(|| PathValidationError::InvalidUnicode(self.to_owned()))?;
Ok(PathBuf::from_slash(path_str))
}
}
impl IntoUnix for &Path {
/// NOTE: `into_unix` *only* converts Windows paths to Unix paths *on* a
/// Windows system. Do not pass a Windows path on a Unix system and
/// assume it'll be converted.
fn into_unix(self) -> Result<PathBuf, PathValidationError> {
Ok(PathBuf::from(
self.to_slash()
.ok_or_else(|| PathValidationError::InvalidUnicode(self.to_owned()))?
.as_ref(),
))
}
}