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
use std::convert::TryFrom;
use std::path::{Path, PathBuf};
use crate::convert::TryAsRef;
use crate::unix::{UnixPath, UnixPathBuf, Utf8UnixPath, Utf8UnixPathBuf};
use crate::windows::{Utf8WindowsPath, Utf8WindowsPathBuf, WindowsPath, WindowsPathBuf};
/// Represents a path with a known type that can be one of:
///
/// * [`UnixPath`]
/// * [`WindowsPath`]
pub enum TypedPath<'a> {
Unix(&'a UnixPath),
Windows(&'a WindowsPath),
}
impl<'a> TypedPath<'a> {
/// Creates a new typed path from a byte slice by determining if the path represents a Windows
/// or Unix path. This is accomplished by first trying to parse as a Windows path. If the
/// resulting path contains a prefix such as `C:` or begins with a `\`, it is assumed to be a
/// [`WindowsPath`]; otherwise, the slice will be represented as a [`UnixPath`].
///
/// # Examples
///
/// ```
/// use typed_path::TypedPath;
///
/// assert!(TypedPath::new(br#"C:\some\path\to\file.txt"#).is_windows());
/// assert!(TypedPath::new(br#"\some\path\to\file.txt"#).is_windows());
/// assert!(TypedPath::new(br#"/some/path/to/file.txt"#).is_unix());
///
/// // NOTE: If we don't start with a backslash, it's too difficult to
/// // determine and we therefore just assume a Unix/POSIX path.
/// assert!(TypedPath::new(br#"some\path\to\file.txt"#).is_unix());
/// assert!(TypedPath::new(b"file.txt").is_unix());
/// assert!(TypedPath::new(b"").is_unix());
/// ```
pub fn new(s: &'a [u8]) -> Self {
let winpath = WindowsPath::new(s);
if winpath.components().has_prefix() || s.first() == Some(&b'\\') {
Self::Windows(winpath)
} else {
Self::Unix(UnixPath::new(s))
}
}
/// Returns true if this path represents a Unix path.
#[inline]
pub fn is_unix(&self) -> bool {
matches!(self, Self::Unix(_))
}
/// Returns true if this path represents a Windows path.
#[inline]
pub fn is_windows(&self) -> bool {
matches!(self, Self::Windows(_))
}
/// Converts into a [`TypedPathBuf`].
pub fn to_path_buf(&self) -> TypedPathBuf {
match self {
Self::Unix(path) => TypedPathBuf::Unix(path.to_path_buf()),
Self::Windows(path) => TypedPathBuf::Windows(path.to_path_buf()),
}
}
}
impl<'a> From<&'a [u8]> for TypedPath<'a> {
#[inline]
fn from(s: &'a [u8]) -> Self {
TypedPath::new(s)
}
}
impl<'a> From<&'a str> for TypedPath<'a> {
#[inline]
fn from(s: &'a str) -> Self {
TypedPath::new(s.as_bytes())
}
}
impl TryAsRef<UnixPath> for TypedPath<'_> {
fn try_as_ref(&self) -> Option<&UnixPath> {
match self {
Self::Unix(path) => Some(path),
_ => None,
}
}
}
impl TryAsRef<WindowsPath> for TypedPath<'_> {
fn try_as_ref(&self) -> Option<&WindowsPath> {
match self {
Self::Windows(path) => Some(path),
_ => None,
}
}
}
/// Represents a pathbuf with a known type that can be one of:
///
/// * [`UnixPathBuf`]
/// * [`WindowsPathBuf`]
pub enum TypedPathBuf {
Unix(UnixPathBuf),
Windows(WindowsPathBuf),
}
impl TypedPathBuf {
/// Returns true if this path represents a Unix path.
#[inline]
pub fn is_unix(&self) -> bool {
matches!(self, Self::Unix(_))
}
/// Returns true if this path represents a Windows path.
#[inline]
pub fn is_windows(&self) -> bool {
matches!(self, Self::Windows(_))
}
/// Converts into a [`TypedPath`].
pub fn as_path(&self) -> TypedPath<'_> {
match self {
Self::Unix(path) => TypedPath::Unix(path.as_path()),
Self::Windows(path) => TypedPath::Windows(path.as_path()),
}
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for TypedPathBuf {
#[inline]
fn from(s: &'a [u8; N]) -> Self {
TypedPathBuf::from(s.as_slice())
}
}
impl<'a> From<&'a [u8]> for TypedPathBuf {
/// Creates a new typed pathbuf from a byte slice by determining if the path represents a
/// Windows or Unix path. This is accomplished by first trying to parse as a Windows path. If
/// the resulting path contains a prefix such as `C:` or begins with a `\`, it is assumed to be
/// a [`WindowsPathBuf`]; otherwise, the slice will be represented as a [`UnixPathBuf`].
///
/// # Examples
///
/// ```
/// use typed_path::TypedPathBuf;
///
/// assert!(TypedPathBuf::from(br#"C:\some\path\to\file.txt"#).is_windows());
/// assert!(TypedPathBuf::from(br#"\some\path\to\file.txt"#).is_windows());
/// assert!(TypedPathBuf::from(br#"/some/path/to/file.txt"#).is_unix());
///
/// // NOTE: If we don't start with a backslash, it's too difficult to
/// // determine and we therefore just assume a Unix/POSIX path.
/// assert!(TypedPathBuf::from(br#"some\path\to\file.txt"#).is_unix());
/// assert!(TypedPathBuf::from(b"file.txt").is_unix());
/// assert!(TypedPathBuf::from(b"").is_unix());
/// ```
#[inline]
fn from(s: &'a [u8]) -> Self {
TypedPath::new(s).to_path_buf()
}
}
impl From<Vec<u8>> for TypedPathBuf {
#[inline]
fn from(s: Vec<u8>) -> Self {
// NOTE: We use the typed path to check the underlying format, and then
// create it manually to avoid a clone of the vec itself
match TypedPath::new(s.as_slice()) {
TypedPath::Unix(_) => TypedPathBuf::Unix(UnixPathBuf::from(s)),
TypedPath::Windows(_) => TypedPathBuf::Windows(WindowsPathBuf::from(s)),
}
}
}
impl<'a> From<&'a str> for TypedPathBuf {
#[inline]
fn from(s: &'a str) -> Self {
TypedPathBuf::from(s.as_bytes())
}
}
impl From<String> for TypedPathBuf {
#[inline]
fn from(s: String) -> Self {
// NOTE: We use the typed path to check the underlying format, and then
// create it manually to avoid a clone of the string itself
match TypedPath::new(s.as_bytes()) {
TypedPath::Unix(_) => TypedPathBuf::Unix(UnixPathBuf::from(s)),
TypedPath::Windows(_) => TypedPathBuf::Windows(WindowsPathBuf::from(s)),
}
}
}
impl TryFrom<TypedPathBuf> for UnixPathBuf {
type Error = TypedPathBuf;
fn try_from(path: TypedPathBuf) -> Result<Self, Self::Error> {
match path {
TypedPathBuf::Unix(path) => Ok(path),
path => Err(path),
}
}
}
impl TryFrom<TypedPathBuf> for WindowsPathBuf {
type Error = TypedPathBuf;
fn try_from(path: TypedPathBuf) -> Result<Self, Self::Error> {
match path {
TypedPathBuf::Windows(path) => Ok(path),
path => Err(path),
}
}
}
/// Represents a UTF-8 path with a known type that can be one of:
///
/// * [`Utf8UnixPath`]
/// * [`Utf8WindowsPath`]
pub enum Utf8TypedPath<'a> {
Unix(&'a Utf8UnixPath),
Windows(&'a Utf8WindowsPath),
}
impl<'a> Utf8TypedPath<'a> {
/// Creates a new UTF* typed path from a byte slice by determining if the path represents a
/// Windows or Unix path. This is accomplished by first trying to parse as a Windows path. If
/// the resulting path contains a prefix such as `C:` or begins with a `\`, it is assumed to be
/// a [`Utf8WindowsPath`]; otherwise, the slice will be represented as a [`Utf8UnixPath`].
///
/// # Examples
///
/// ```
/// use typed_path::Utf8TypedPath;
///
/// assert!(Utf8TypedPath::new(r#"C:\some\path\to\file.txt"#).is_windows());
/// assert!(Utf8TypedPath::new(r#"\some\path\to\file.txt"#).is_windows());
/// assert!(Utf8TypedPath::new(r#"/some/path/to/file.txt"#).is_unix());
///
/// // NOTE: If we don't start with a backslash, it's too difficult to
/// // determine and we therefore just assume a Unix/POSIX path.
/// assert!(Utf8TypedPath::new(r#"some\path\to\file.txt"#).is_unix());
/// assert!(Utf8TypedPath::new("file.txt").is_unix());
/// assert!(Utf8TypedPath::new("").is_unix());
/// ```
pub fn new(s: &'a str) -> Self {
let winpath = Utf8WindowsPath::new(s);
if winpath.components().has_prefix() || s.starts_with('\\') {
Self::Windows(winpath)
} else {
Self::Unix(Utf8UnixPath::new(s))
}
}
/// Returns true if this path represents a Unix path.
#[inline]
pub fn is_unix(&self) -> bool {
matches!(self, Self::Unix(_))
}
/// Returns true if this path represents a Windows path.
#[inline]
pub fn is_windows(&self) -> bool {
matches!(self, Self::Windows(_))
}
/// Converts into a [`Utf8TypedPathBuf`].
pub fn to_path_buf(&self) -> Utf8TypedPathBuf {
match self {
Self::Unix(path) => Utf8TypedPathBuf::Unix(path.to_path_buf()),
Self::Windows(path) => Utf8TypedPathBuf::Windows(path.to_path_buf()),
}
}
}
impl<'a> From<&'a str> for Utf8TypedPath<'a> {
#[inline]
fn from(s: &'a str) -> Self {
Utf8TypedPath::new(s)
}
}
impl TryAsRef<Utf8UnixPath> for Utf8TypedPath<'_> {
fn try_as_ref(&self) -> Option<&Utf8UnixPath> {
match self {
Self::Unix(path) => Some(path),
_ => None,
}
}
}
impl TryAsRef<Utf8WindowsPath> for Utf8TypedPath<'_> {
fn try_as_ref(&self) -> Option<&Utf8WindowsPath> {
match self {
Self::Windows(path) => Some(path),
_ => None,
}
}
}
/// Represents a UTF-8 pathbuf with a known type that can be one of:
///
/// * [`Utf8UnixPathBuf`]
/// * [`Utf8WindowsPathBuf`]
pub enum Utf8TypedPathBuf {
Unix(Utf8UnixPathBuf),
Windows(Utf8WindowsPathBuf),
}
impl Utf8TypedPathBuf {
/// Returns true if this path represents a Unix path.
#[inline]
pub fn is_unix(&self) -> bool {
matches!(self, Self::Unix(_))
}
/// Returns true if this path represents a Windows path.
#[inline]
pub fn is_windows(&self) -> bool {
matches!(self, Self::Windows(_))
}
/// Converts into a [`Utf8TypedPath`].
pub fn as_path(&self) -> Utf8TypedPath<'_> {
match self {
Self::Unix(path) => Utf8TypedPath::Unix(path.as_path()),
Self::Windows(path) => Utf8TypedPath::Windows(path.as_path()),
}
}
}
impl<'a> From<&'a str> for Utf8TypedPathBuf {
/// Creates a new UTF-8 typed pathbuf from a byte slice by determining if the path represents a
/// Windows or Unix path. This is accomplished by first trying to parse as a Windows path. If
/// the resulting path contains a prefix such as `C:` or begins with a `\`, it is assumed to be
/// a [`Utf8WindowsPathBuf`]; otherwise, the slice will be represented as a
/// [`Utf8UnixPathBuf`].
///
/// # Examples
///
/// ```
/// use typed_path::Utf8TypedPathBuf;
///
/// assert!(Utf8TypedPathBuf::from(r#"C:\some\path\to\file.txt"#).is_windows());
/// assert!(Utf8TypedPathBuf::from(r#"\some\path\to\file.txt"#).is_windows());
/// assert!(Utf8TypedPathBuf::from(r#"/some/path/to/file.txt"#).is_unix());
///
/// // NOTE: If we don't start with a backslash, it's too difficult to
/// // determine and we therefore just assume a Unix/POSIX path.
/// assert!(Utf8TypedPathBuf::from(r#"some\path\to\file.txt"#).is_unix());
/// assert!(Utf8TypedPathBuf::from("file.txt").is_unix());
/// assert!(Utf8TypedPathBuf::from("").is_unix());
/// ```
#[inline]
fn from(s: &'a str) -> Self {
Utf8TypedPath::new(s).to_path_buf()
}
}
impl From<String> for Utf8TypedPathBuf {
#[inline]
fn from(s: String) -> Self {
// NOTE: We use the typed path to check the underlying format, and then
// create it manually to avoid a clone of the string itself
match Utf8TypedPath::new(s.as_str()) {
Utf8TypedPath::Unix(_) => Utf8TypedPathBuf::Unix(Utf8UnixPathBuf::from(s)),
Utf8TypedPath::Windows(_) => Utf8TypedPathBuf::Windows(Utf8WindowsPathBuf::from(s)),
}
}
}
impl TryFrom<Utf8TypedPathBuf> for Utf8UnixPathBuf {
type Error = Utf8TypedPathBuf;
fn try_from(path: Utf8TypedPathBuf) -> Result<Self, Self::Error> {
match path {
Utf8TypedPathBuf::Unix(path) => Ok(path),
path => Err(path),
}
}
}
impl TryFrom<Utf8TypedPathBuf> for Utf8WindowsPathBuf {
type Error = Utf8TypedPathBuf;
fn try_from(path: Utf8TypedPathBuf) -> Result<Self, Self::Error> {
match path {
Utf8TypedPathBuf::Windows(path) => Ok(path),
path => Err(path),
}
}
}
impl<'a> TryAsRef<Path> for TypedPath<'a> {
fn try_as_ref(&self) -> Option<&Path> {
match self {
Self::Unix(path) => path.try_as_ref(),
Self::Windows(path) => path.try_as_ref(),
}
}
}
impl<'a> TryAsRef<Path> for Utf8TypedPath<'a> {
fn try_as_ref(&self) -> Option<&Path> {
match self {
#[cfg(unix)]
Self::Unix(path) => Some(AsRef::<Path>::as_ref(path)),
#[cfg(windows)]
Self::Windows(path) => Some(AsRef::<Path>::as_ref(path)),
_ => None,
}
}
}
impl TryFrom<TypedPathBuf> for PathBuf {
type Error = TypedPathBuf;
fn try_from(path: TypedPathBuf) -> Result<Self, Self::Error> {
match path {
#[cfg(unix)]
TypedPathBuf::Unix(path) => PathBuf::try_from(path).map_err(TypedPathBuf::Unix),
#[cfg(windows)]
TypedPathBuf::Windows(path) => PathBuf::try_from(path).map_err(TypedPathBuf::Windows),
path => Err(path),
}
}
}
impl TryFrom<Utf8TypedPathBuf> for PathBuf {
type Error = Utf8TypedPathBuf;
fn try_from(path: Utf8TypedPathBuf) -> Result<Self, Self::Error> {
match path {
#[cfg(unix)]
Utf8TypedPathBuf::Unix(path) => Ok(PathBuf::from(path)),
#[cfg(windows)]
Utf8TypedPathBuf::Windows(path) => Ok(PathBuf::from(path)),
path => Err(path),
}
}
}