| 1 |
//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/relative--path-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/relative-path) |
| 2 |
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/relative-path.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/relative-path) |
| 3 |
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-relative--path-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/relative-path) |
| 4 |
//! |
| 5 |
//! Portable relative UTF-8 paths for Rust. |
| 6 |
//! |
| 7 |
//! This crate provides a module analogous to [`std::path`], with the following |
| 8 |
//! characteristics: |
| 9 |
//! |
| 10 |
//! * The path separator is set to a fixed character (`/`), regardless of |
| 11 |
//! platform. |
| 12 |
//! * Relative paths cannot represent a path in the filesystem without first |
| 13 |
//! specifying *what they are relative to* using functions such as [`to_path`] |
| 14 |
//! and [`to_logical_path`]. |
| 15 |
//! * Relative paths are always guaranteed to be valid UTF-8 strings. |
| 16 |
//! |
| 17 |
//! On top of this we support many operations that guarantee the same behavior |
| 18 |
//! across platforms. |
| 19 |
//! |
| 20 |
//! For more utilities to manipulate relative paths, see the |
| 21 |
//! [`relative-path-utils` crate]. |
| 22 |
//! |
| 23 |
//! <br> |
| 24 |
//! |
| 25 |
//! ## Usage |
| 26 |
//! |
| 27 |
//! Add `relative-path` to your `Cargo.toml`: |
| 28 |
//! |
| 29 |
//! ```toml |
| 30 |
//! relative-path = "1.9.2" |
| 31 |
//! ``` |
| 32 |
//! |
| 33 |
//! Start using relative paths: |
| 34 |
//! |
| 35 |
//! ``` |
| 36 |
//! use serde::{Serialize, Deserialize}; |
| 37 |
//! use relative_path::RelativePath; |
| 38 |
//! |
| 39 |
//! #[derive(Serialize, Deserialize)] |
| 40 |
//! struct Manifest<'a> { |
| 41 |
//! #[serde(borrow)] |
| 42 |
//! source: &'a RelativePath, |
| 43 |
//! } |
| 44 |
//! |
| 45 |
//! # Ok::<_, Box<dyn std::error::Error>>(()) |
| 46 |
//! ``` |
| 47 |
//! |
| 48 |
//! <br> |
| 49 |
//! |
| 50 |
//! ## Serde Support |
| 51 |
//! |
| 52 |
//! This library includes serde support that can be enabled with the `serde` |
| 53 |
//! feature. |
| 54 |
//! |
| 55 |
//! <br> |
| 56 |
//! |
| 57 |
//! ## Why is `std::path` a portability hazard? |
| 58 |
//! |
| 59 |
//! Path representations differ across platforms. |
| 60 |
//! |
| 61 |
//! * Windows permits using drive volumes (multiple roots) as a prefix (e.g. |
| 62 |
//! `"c:\"`) and backslash (`\`) as a separator. |
| 63 |
//! * Unix references absolute paths from a single root and uses forward slash |
| 64 |
//! (`/`) as a separator. |
| 65 |
//! |
| 66 |
//! If we use `PathBuf`, Storing paths in a manifest would allow our application |
| 67 |
//! to build and run on one platform but potentially not others. |
| 68 |
//! |
| 69 |
//! Consider the following data model and corresponding toml for a manifest: |
| 70 |
//! |
| 71 |
//! ```rust |
| 72 |
//! use std::path::PathBuf; |
| 73 |
//! |
| 74 |
//! use serde::{Serialize, Deserialize}; |
| 75 |
//! |
| 76 |
//! #[derive(Serialize, Deserialize)] |
| 77 |
//! struct Manifest { |
| 78 |
//! source: PathBuf, |
| 79 |
//! } |
| 80 |
//! ``` |
| 81 |
//! |
| 82 |
//! ```toml |
| 83 |
//! source = "C:\\Users\\udoprog\\repo\\data\\source" |
| 84 |
//! ``` |
| 85 |
//! |
| 86 |
//! This will run for you (assuming `source` exists). So you go ahead and check |
| 87 |
//! the manifest into git. The next day your Linux colleague calls you and |
| 88 |
//! wonders what they have ever done to wrong you? |
| 89 |
//! |
| 90 |
//! So what went wrong? Well two things. You forgot to make the `source` |
| 91 |
//! relative, so anyone at the company which has a different username than you |
| 92 |
//! won't be able to use it. So you go ahead and fix that: |
| 93 |
//! |
| 94 |
//! ```toml |
| 95 |
//! source = "data\\source" |
| 96 |
//! ``` |
| 97 |
//! |
| 98 |
//! But there is still one problem! A backslash (`\`) is only a legal path |
| 99 |
//! separator on Windows. Luckily you learn that forward slashes are supported |
| 100 |
//! both on Windows *and* Linux. So you opt for: |
| 101 |
//! |
| 102 |
//! ```toml |
| 103 |
//! source = "data/source" |
| 104 |
//! ``` |
| 105 |
//! |
| 106 |
//! Things are working now. So all is well... Right? Sure, but we can do better. |
| 107 |
//! |
| 108 |
//! This crate provides types that work with *portable relative paths* (hence |
| 109 |
//! the name). So by using [`RelativePath`] we can systematically help avoid |
| 110 |
//! portability issues like the one above. Avoiding issues at the source is |
| 111 |
//! preferably over spending 5 minutes of onboarding time on a theoretical |
| 112 |
//! problem, hoping that your new hires will remember what to do if they ever |
| 113 |
//! encounter it. |
| 114 |
//! |
| 115 |
//! Using [`RelativePathBuf`] we can fix our data model like this: |
| 116 |
//! |
| 117 |
//! ```rust |
| 118 |
//! use relative_path::RelativePathBuf; |
| 119 |
//! use serde::{Serialize, Deserialize}; |
| 120 |
//! |
| 121 |
//! #[derive(Serialize, Deserialize)] |
| 122 |
//! pub struct Manifest { |
| 123 |
//! source: RelativePathBuf, |
| 124 |
//! } |
| 125 |
//! ``` |
| 126 |
//! |
| 127 |
//! And where it's used: |
| 128 |
//! |
| 129 |
//! ```rust,no_run |
| 130 |
//! # use relative_path::RelativePathBuf; |
| 131 |
//! # use serde::{Serialize, Deserialize}; |
| 132 |
//! # #[derive(Serialize, Deserialize)] pub struct Manifest { source: RelativePathBuf } |
| 133 |
//! use std::fs; |
| 134 |
//! use std::env::current_dir; |
| 135 |
//! |
| 136 |
//! let manifest: Manifest = todo!(); |
| 137 |
//! |
| 138 |
//! let root = current_dir()?; |
| 139 |
//! let source = manifest.source.to_path(&root); |
| 140 |
//! let content = fs::read(&source)?; |
| 141 |
//! # Ok::<_, Box<dyn std::error::Error>>(()) |
| 142 |
//! ``` |
| 143 |
//! |
| 144 |
//! <br> |
| 145 |
//! |
| 146 |
//! ## Overview |
| 147 |
//! |
| 148 |
//! Conversion to a platform-specific [`Path`] happens through the [`to_path`] |
| 149 |
//! and [`to_logical_path`] functions. Where you are required to specify the |
| 150 |
//! path that prefixes the relative path. This can come from a function such as |
| 151 |
//! [`std::env::current_dir`]. |
| 152 |
//! |
| 153 |
//! ```rust |
| 154 |
//! use std::env::current_dir; |
| 155 |
//! use std::path::Path; |
| 156 |
//! |
| 157 |
//! use relative_path::RelativePath; |
| 158 |
//! |
| 159 |
//! let root = current_dir()?; |
| 160 |
//! |
| 161 |
//! # if cfg!(windows) { |
| 162 |
//! // to_path unconditionally concatenates a relative path with its base: |
| 163 |
//! let relative_path = RelativePath::new("../foo/./bar"); |
| 164 |
//! let full_path = relative_path.to_path(&root); |
| 165 |
//! assert_eq!(full_path, root.join("..\\foo\\.\\bar")); |
| 166 |
//! |
| 167 |
//! // to_logical_path tries to apply the logical operations that the relative |
| 168 |
//! // path corresponds to: |
| 169 |
//! let relative_path = RelativePath::new("../foo/./bar"); |
| 170 |
//! let full_path = relative_path.to_logical_path(&root); |
| 171 |
//! |
| 172 |
//! // Replicate the operation performed by `to_logical_path`. |
| 173 |
//! let mut parent = root.clone(); |
| 174 |
//! parent.pop(); |
| 175 |
//! assert_eq!(full_path, parent.join("foo\\bar")); |
| 176 |
//! # } |
| 177 |
//! # Ok::<_, std::io::Error>(()) |
| 178 |
//! ``` |
| 179 |
//! |
| 180 |
//! When two relative paths are compared to each other, their exact component |
| 181 |
//! makeup determines equality. |
| 182 |
//! |
| 183 |
//! ```rust |
| 184 |
//! use relative_path::RelativePath; |
| 185 |
//! |
| 186 |
//! assert_ne!( |
| 187 |
//! RelativePath::new("foo/bar/../baz"), |
| 188 |
//! RelativePath::new("foo/baz") |
| 189 |
//! ); |
| 190 |
//! ``` |
| 191 |
//! |
| 192 |
//! Using platform-specific path separators to construct relative paths is not |
| 193 |
//! supported. |
| 194 |
//! |
| 195 |
//! Path separators from other platforms are simply treated as part of a |
| 196 |
//! component: |
| 197 |
//! |
| 198 |
//! ```rust |
| 199 |
//! use relative_path::RelativePath; |
| 200 |
//! |
| 201 |
//! assert_ne!( |
| 202 |
//! RelativePath::new("foo/bar"), |
| 203 |
//! RelativePath::new("foo\\bar") |
| 204 |
//! ); |
| 205 |
//! |
| 206 |
//! assert_eq!(1, RelativePath::new("foo\\bar").components().count()); |
| 207 |
//! assert_eq!(2, RelativePath::new("foo/bar").components().count()); |
| 208 |
//! ``` |
| 209 |
//! |
| 210 |
//! To see if two relative paths are equivalent you can use [`normalize`]: |
| 211 |
//! |
| 212 |
//! ```rust |
| 213 |
//! use relative_path::RelativePath; |
| 214 |
//! |
| 215 |
//! assert_eq!( |
| 216 |
//! RelativePath::new("foo/bar/../baz").normalize(), |
| 217 |
//! RelativePath::new("foo/baz").normalize(), |
| 218 |
//! ); |
| 219 |
//! ``` |
| 220 |
//! |
| 221 |
//! <br> |
| 222 |
//! |
| 223 |
//! ## Additional portability notes |
| 224 |
//! |
| 225 |
//! While relative paths avoid the most egregious portability issue, that |
| 226 |
//! absolute paths will work equally unwell on all platforms. We cannot avoid |
| 227 |
//! all. This section tries to document additional portability hazards that we |
| 228 |
//! are aware of. |
| 229 |
//! |
| 230 |
//! [`RelativePath`], similarly to [`Path`], makes no guarantees that its |
| 231 |
//! constituent components make up legal file names. While components are |
| 232 |
//! strictly separated by slashes, we can still store things in them which may |
| 233 |
//! not be used as legal paths on all platforms. |
| 234 |
//! |
| 235 |
//! * A `NUL` character is not permitted on unix platforms - this is a |
| 236 |
//! terminator in C-based filesystem APIs. Slash (`/`) is also used as a path |
| 237 |
//! separator. |
| 238 |
//! * Windows has a number of [reserved characters and names][windows-reserved] |
| 239 |
//! (like `CON`, `PRN`, and `AUX`) which cannot legally be part of a |
| 240 |
//! filesystem component. |
| 241 |
//! * Windows paths are [case-insensitive by default][windows-case]. So, |
| 242 |
//! `Foo.txt` and `foo.txt` are the same files on windows. But they are |
| 243 |
//! considered different paths on most unix systems. |
| 244 |
//! |
| 245 |
//! A relative path that *accidentally* contains a platform-specific components |
| 246 |
//! will largely result in a nonsensical paths being generated in the hope that |
| 247 |
//! they will fail fast during development and testing. |
| 248 |
//! |
| 249 |
//! ```rust |
| 250 |
//! use relative_path::{RelativePath, PathExt}; |
| 251 |
//! use std::path::Path; |
| 252 |
//! |
| 253 |
//! if cfg!(windows) { |
| 254 |
//! assert_eq!( |
| 255 |
//! Path::new("foo\\c:\\bar\\baz"), |
| 256 |
//! RelativePath::new("c:\\bar\\baz").to_path("foo") |
| 257 |
//! ); |
| 258 |
//! } |
| 259 |
//! |
| 260 |
//! if cfg!(unix) { |
| 261 |
//! assert_eq!( |
| 262 |
//! Path::new("foo/bar/baz"), |
| 263 |
//! RelativePath::new("/bar/baz").to_path("foo") |
| 264 |
//! ); |
| 265 |
//! } |
| 266 |
//! |
| 267 |
//! assert_eq!( |
| 268 |
//! Path::new("foo").relative_to("bar")?, |
| 269 |
//! RelativePath::new("../foo"), |
| 270 |
//! ); |
| 271 |
//! # Ok::<_, Box<dyn std::error::Error>>(()) |
| 272 |
//! ``` |
| 273 |
//! |
| 274 |
//! [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html |
| 275 |
//! [`normalize`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html#method.normalize |
| 276 |
//! [`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html |
| 277 |
//! [`RelativePath`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html |
| 278 |
//! [`RelativePathBuf`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePathBuf.html |
| 279 |
//! [`std::env::current_dir`]: https://doc.rust-lang.org/std/env/fn.current_dir.html |
| 280 |
//! [`std::path`]: https://doc.rust-lang.org/std/path/index.html |
| 281 |
//! [`to_logical_path`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html#method.to_logical_path |
| 282 |
//! [`to_path`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html#method.to_path |
| 283 |
//! [windows-reserved]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx |
| 284 |
//! [windows-case]: https://learn.microsoft.com/en-us/windows/wsl/case-sensitivity |
| 285 |
//! [`relative-path-utils` crate]: https://docs.rs/relative-path-utils |
| 286 |
|
| 287 |
// This file contains parts that are Copyright 2015 The Rust Project Developers, copied from: |
| 288 |
// https://github.com/rust-lang/rust |
| 289 |
// cb2a656cdfb6400ac0200c661267f91fabf237e2 src/libstd/path.rs |
| 290 |
|
| 291 |
#![allow(clippy::manual_let_else)] |
| 292 |
#![deny(missing_docs)] |
| 293 |
|
| 294 |
mod path_ext; |
| 295 |
|
| 296 |
#[cfg(test)] |
| 297 |
mod tests; |
| 298 |
|
| 299 |
pub use path_ext::{PathExt, RelativeToError}; |
| 300 |
|
| 301 |
use std::borrow::{Borrow, Cow}; |
| 302 |
use std::cmp; |
| 303 |
use std::error; |
| 304 |
use std::fmt; |
| 305 |
use std::hash::{Hash, Hasher}; |
| 306 |
use std::iter::FromIterator; |
| 307 |
use std::mem; |
| 308 |
use std::ops; |
| 309 |
use std::path; |
| 310 |
use std::rc::Rc; |
| 311 |
use std::str; |
| 312 |
use std::sync::Arc; |
| 313 |
|
| 314 |
const STEM_SEP: char = '.'; |
| 315 |
const CURRENT_STR: &str = "."; |
| 316 |
const PARENT_STR: &str = ".."; |
| 317 |
|
| 318 |
const SEP: char = '/'; |
| 319 |
|
| 320 |
fn split_file_at_dot(input: &str) -> (Option<&str>, Option<&str>) { |
| 321 |
if input == PARENT_STR { |
| 322 |
return (Some(input), None); |
| 323 |
} |
| 324 |
|
| 325 |
let mut iter = input.rsplitn(2, STEM_SEP); |
| 326 |
|
| 327 |
let after = iter.next(); |
| 328 |
let before = iter.next(); |
| 329 |
|
| 330 |
if before == Some("") { |
| 331 |
(Some(input), None) |
| 332 |
} else { |
| 333 |
(before, after) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// Iterate through `iter` while it matches `prefix`; return `None` if `prefix` |
| 338 |
// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving |
| 339 |
// `iter` after having exhausted `prefix`. |
| 340 |
fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I> |
| 341 |
where |
| 342 |
I: Iterator<Item = Component<'a>> + Clone, |
| 343 |
J: Iterator<Item = Component<'b>>, |
| 344 |
{ |
| 345 |
loop { |
| 346 |
let mut iter_next = iter.clone(); |
| 347 |
match (iter_next.next(), prefix.next()) { |
| 348 |
(Some(x), Some(y)) if x == y => (), |
| 349 |
(Some(_) | None, Some(_)) => return None, |
| 350 |
(Some(_) | None, None) => return Some(iter), |
| 351 |
} |
| 352 |
iter = iter_next; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/// A single path component. |
| 357 |
/// |
| 358 |
/// Accessed using the [`RelativePath::components`] iterator. |
| 359 |
/// |
| 360 |
/// # Examples |
| 361 |
/// |
| 362 |
/// ``` |
| 363 |
/// use relative_path::{Component, RelativePath}; |
| 364 |
/// |
| 365 |
/// let path = RelativePath::new("foo/../bar/./baz"); |
| 366 |
/// let mut it = path.components(); |
| 367 |
/// |
| 368 |
/// assert_eq!(Some(Component::Normal("foo")), it.next()); |
| 369 |
/// assert_eq!(Some(Component::ParentDir), it.next()); |
| 370 |
/// assert_eq!(Some(Component::Normal("bar")), it.next()); |
| 371 |
/// assert_eq!(Some(Component::CurDir), it.next()); |
| 372 |
/// assert_eq!(Some(Component::Normal("baz")), it.next()); |
| 373 |
/// assert_eq!(None, it.next()); |
| 374 |
/// ``` |
| 375 |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] |
| 376 |
pub enum Component<'a> { |
| 377 |
/// The current directory `.`. |
| 378 |
CurDir, |
| 379 |
/// The parent directory `..`. |
| 380 |
ParentDir, |
| 381 |
/// A normal path component as a string. |
| 382 |
Normal(&'a str), |
| 383 |
} |
| 384 |
|
| 385 |
impl<'a> Component<'a> { |
| 386 |
/// Extracts the underlying [`str`] slice. |
| 387 |
/// |
| 388 |
/// [`str`]: prim@str |
| 389 |
/// |
| 390 |
/// # Examples |
| 391 |
/// |
| 392 |
/// ``` |
| 393 |
/// use relative_path::{RelativePath, Component}; |
| 394 |
/// |
| 395 |
/// let path = RelativePath::new("./tmp/../foo/bar.txt"); |
| 396 |
/// let components: Vec<_> = path.components().map(Component::as_str).collect(); |
| 397 |
/// assert_eq!(&components, &[".", "tmp", "..", "foo", "bar.txt"]); |
| 398 |
/// ``` |
| 399 |
#[must_use] |
| 400 |
pub fn as_str(self) -> &'a str { |
| 401 |
use self::Component::{CurDir, Normal, ParentDir}; |
| 402 |
|
| 403 |
match self { |
| 404 |
CurDir => CURRENT_STR, |
| 405 |
ParentDir => PARENT_STR, |
| 406 |
Normal(name) => name, |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/// [`AsRef<RelativePath>`] implementation for [`Component`]. |
| 412 |
/// |
| 413 |
/// # Examples |
| 414 |
/// |
| 415 |
/// ``` |
| 416 |
/// use relative_path::RelativePath; |
| 417 |
/// |
| 418 |
/// let mut it = RelativePath::new("../foo/bar").components(); |
| 419 |
/// |
| 420 |
/// let a = it.next().ok_or("a")?; |
| 421 |
/// let b = it.next().ok_or("b")?; |
| 422 |
/// let c = it.next().ok_or("c")?; |
| 423 |
/// |
| 424 |
/// let a: &RelativePath = a.as_ref(); |
| 425 |
/// let b: &RelativePath = b.as_ref(); |
| 426 |
/// let c: &RelativePath = c.as_ref(); |
| 427 |
/// |
| 428 |
/// assert_eq!(a, ".."); |
| 429 |
/// assert_eq!(b, "foo"); |
| 430 |
/// assert_eq!(c, "bar"); |
| 431 |
/// |
| 432 |
/// # Ok::<_, Box<dyn std::error::Error>>(()) |
| 433 |
/// ``` |
| 434 |
impl AsRef<RelativePath> for Component<'_> { |
| 435 |
#[inline] |
| 436 |
fn as_ref(&self) -> &RelativePath { |
| 437 |
self.as_str().as_ref() |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/// Traverse the given components and apply to the provided stack. |
| 442 |
/// |
| 443 |
/// This takes '.', and '..' into account. Where '.' doesn't change the stack, and '..' pops the |
| 444 |
/// last item or further adds parent components. |
| 445 |
#[inline(always)] |
| 446 |
fn relative_traversal<'a, C>(buf: &mut RelativePathBuf, components: C) |
| 447 |
where |
| 448 |
C: IntoIterator<Item = Component<'a>>, |
| 449 |
{ |
| 450 |
use self::Component::{CurDir, Normal, ParentDir}; |
| 451 |
|
| 452 |
for c in components { |
| 453 |
match c { |
| 454 |
CurDir => (), |
| 455 |
ParentDir => match buf.components().next_back() { |
| 456 |
Some(Component::ParentDir) | None => { |
| 457 |
buf.push(PARENT_STR); |
| 458 |
} |
| 459 |
_ => { |
| 460 |
buf.pop(); |
| 461 |
} |
| 462 |
}, |
| 463 |
Normal(name) => { |
| 464 |
buf.push(name); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/// Iterator over all the components in a relative path. |
| 471 |
#[derive(Clone)] |
| 472 |
pub struct Components<'a> { |
| 473 |
source: &'a str, |
| 474 |
} |
| 475 |
|
| 476 |
impl<'a> Iterator for Components<'a> { |
| 477 |
type Item = Component<'a>; |
| 478 |
|
| 479 |
fn next(&mut self) -> Option<Self::Item> { |
| 480 |
self.source = self.source.trim_start_matches(SEP); |
| 481 |
|
| 482 |
let slice = match self.source.find(SEP) { |
| 483 |
Some(i) => { |
| 484 |
let (slice, rest) = self.source.split_at(i); |
| 485 |
self.source = rest.trim_start_matches(SEP); |
| 486 |
slice |
| 487 |
} |
| 488 |
None => mem::take(&mut self.source), |
| 489 |
}; |
| 490 |
|
| 491 |
match slice { |
| 492 |
"" => None, |
| 493 |
CURRENT_STR => Some(Component::CurDir), |
| 494 |
PARENT_STR => Some(Component::ParentDir), |
| 495 |
slice => Some(Component::Normal(slice)), |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
impl<'a> DoubleEndedIterator for Components<'a> { |
| 501 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 502 |
self.source = self.source.trim_end_matches(SEP); |
| 503 |
|
| 504 |
let slice = match self.source.rfind(SEP) { |
| 505 |
Some(i) => { |
| 506 |
let (rest, slice) = self.source.split_at(i + 1); |
| 507 |
self.source = rest.trim_end_matches(SEP); |
| 508 |
slice |
| 509 |
} |
| 510 |
None => mem::take(&mut self.source), |
| 511 |
}; |
| 512 |
|
| 513 |
match slice { |
| 514 |
"" => None, |
| 515 |
CURRENT_STR => Some(Component::CurDir), |
| 516 |
PARENT_STR => Some(Component::ParentDir), |
| 517 |
slice => Some(Component::Normal(slice)), |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
impl<'a> Components<'a> { |
| 523 |
/// Construct a new component from the given string. |
| 524 |
fn new(source: &'a str) -> Components<'a> { |
| 525 |
Self { source } |
| 526 |
} |
| 527 |
|
| 528 |
/// Extracts a slice corresponding to the portion of the path remaining for iteration. |
| 529 |
/// |
| 530 |
/// # Examples |
| 531 |
/// |
| 532 |
/// ``` |
| 533 |
/// use relative_path::RelativePath; |
| 534 |
/// |
| 535 |
/// let mut components = RelativePath::new("tmp/foo/bar.txt").components(); |
| 536 |
/// components.next(); |
| 537 |
/// components.next(); |
| 538 |
/// |
| 539 |
/// assert_eq!("bar.txt", components.as_relative_path()); |
| 540 |
/// ``` |
| 541 |
#[must_use] |
| 542 |
#[inline] |
| 543 |
pub fn as_relative_path(&self) -> &'a RelativePath { |
| 544 |
RelativePath::new(self.source) |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
impl<'a> cmp::PartialEq for Components<'a> { |
| 549 |
fn eq(&self, other: &Components<'a>) -> bool { |
| 550 |
Iterator::eq(self.clone(), other.clone()) |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/// An iterator over the [`Component`]s of a [`RelativePath`], as [`str`] |
| 555 |
/// slices. |
| 556 |
/// |
| 557 |
/// This `struct` is created by the [`iter`][RelativePath::iter] method. |
| 558 |
/// |
| 559 |
/// [`str`]: prim@str |
| 560 |
#[derive(Clone)] |
| 561 |
pub struct Iter<'a> { |
| 562 |
inner: Components<'a>, |
| 563 |
} |
| 564 |
|
| 565 |
impl<'a> Iterator for Iter<'a> { |
| 566 |
type Item = &'a str; |
| 567 |
|
| 568 |
fn next(&mut self) -> Option<&'a str> { |
| 569 |
self.inner.next().map(Component::as_str) |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
impl<'a> DoubleEndedIterator for Iter<'a> { |
| 574 |
fn next_back(&mut self) -> Option<&'a str> { |
| 575 |
self.inner.next_back().map(Component::as_str) |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
/// Error kind for [`FromPathError`]. |
| 580 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 581 |
#[non_exhaustive] |
| 582 |
pub enum FromPathErrorKind { |
| 583 |
/// Non-relative component in path. |
| 584 |
NonRelative, |
| 585 |
/// Non-utf8 component in path. |
| 586 |
NonUtf8, |
| 587 |
/// Trying to convert a platform-specific path which uses a platform-specific separator. |
| 588 |
BadSeparator, |
| 589 |
} |
| 590 |
|
| 591 |
/// An error raised when attempting to convert a path using |
| 592 |
/// [`RelativePathBuf::from_path`]. |
| 593 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 594 |
pub struct FromPathError { |
| 595 |
kind: FromPathErrorKind, |
| 596 |
} |
| 597 |
|
| 598 |
impl FromPathError { |
| 599 |
/// Gets the underlying [`FromPathErrorKind`] that provides more details on |
| 600 |
/// what went wrong. |
| 601 |
/// |
| 602 |
/// # Examples |
| 603 |
/// |
| 604 |
/// ``` |
| 605 |
/// use std::path::Path; |
| 606 |
/// use relative_path::{FromPathErrorKind, RelativePathBuf}; |
| 607 |
/// |
| 608 |
/// let result = RelativePathBuf::from_path(Path::new("/hello/world")); |
| 609 |
/// let e = result.unwrap_err(); |
| 610 |
/// |
| 611 |
/// assert_eq!(FromPathErrorKind::NonRelative, e.kind()); |
| 612 |
/// ``` |
| 613 |
#[must_use] |
| 614 |
pub fn kind(&self) -> FromPathErrorKind { |
| 615 |
self.kind |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
impl From<FromPathErrorKind> for FromPathError { |
| 620 |
fn from(value: FromPathErrorKind) -> Self { |
| 621 |
Self { kind: value } |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
impl fmt::Display for FromPathError { |
| 626 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 627 |
match self.kind { |
| 628 |
FromPathErrorKind::NonRelative => "path contains non-relative component".fmt(fmt), |
| 629 |
FromPathErrorKind::NonUtf8 => "path contains non-utf8 component".fmt(fmt), |
| 630 |
FromPathErrorKind::BadSeparator => { |
| 631 |
"path contains platform-specific path separator".fmt(fmt) |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
impl error::Error for FromPathError {} |
| 638 |
|
| 639 |
/// An owned, mutable relative path. |
| 640 |
/// |
| 641 |
/// This type provides methods to manipulate relative path objects. |
| 642 |
#[derive(Clone)] |
| 643 |
pub struct RelativePathBuf { |
| 644 |
inner: String, |
| 645 |
} |
| 646 |
|
| 647 |
impl RelativePathBuf { |
| 648 |
/// Create a new relative path buffer. |
| 649 |
#[must_use] |
| 650 |
pub fn new() -> RelativePathBuf { |
| 651 |
RelativePathBuf { |
| 652 |
inner: String::new(), |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
/// Internal constructor to allocate a relative path buf with the given capacity. |
| 657 |
fn with_capacity(cap: usize) -> RelativePathBuf { |
| 658 |
RelativePathBuf { |
| 659 |
inner: String::with_capacity(cap), |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
/// Try to convert a [`Path`] to a [`RelativePathBuf`]. |
| 664 |
/// |
| 665 |
/// [`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html |
| 666 |
/// |
| 667 |
/// # Examples |
| 668 |
/// |
| 669 |
/// ``` |
| 670 |
/// use relative_path::{RelativePath, RelativePathBuf, FromPathErrorKind}; |
| 671 |
/// use std::path::Path; |
| 672 |
/// |
| 673 |
/// assert_eq!( |
| 674 |
/// Ok(RelativePath::new("foo/bar").to_owned()), |
| 675 |
/// RelativePathBuf::from_path(Path::new("foo/bar")) |
| 676 |
/// ); |
| 677 |
/// ``` |
| 678 |
/// |
| 679 |
/// # Errors |
| 680 |
/// |
| 681 |
/// This will error in case the provided path is not a relative path, which |
| 682 |
/// is identifier by it having a [`Prefix`] or [`RootDir`] component. |
| 683 |
/// |
| 684 |
/// [`Prefix`]: std::path::Component::Prefix |
| 685 |
/// [`RootDir`]: std::path::Component::RootDir |
| 686 |
pub fn from_path<P: AsRef<path::Path>>(path: P) -> Result<RelativePathBuf, FromPathError> { |
| 687 |
use std::path::Component::{CurDir, Normal, ParentDir, Prefix, RootDir}; |
| 688 |
|
| 689 |
let mut buffer = RelativePathBuf::new(); |
| 690 |
|
| 691 |
for c in path.as_ref().components() { |
| 692 |
match c { |
| 693 |
Prefix(_) | RootDir => return Err(FromPathErrorKind::NonRelative.into()), |
| 694 |
CurDir => continue, |
| 695 |
ParentDir => buffer.push(PARENT_STR), |
| 696 |
Normal(s) => buffer.push(s.to_str().ok_or(FromPathErrorKind::NonUtf8)?), |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
Ok(buffer) |
| 701 |
} |
| 702 |
|
| 703 |
/// Extends `self` with `path`. |
| 704 |
/// |
| 705 |
/// # Examples |
| 706 |
/// |
| 707 |
/// ``` |
| 708 |
/// use relative_path::RelativePathBuf; |
| 709 |
/// |
| 710 |
/// let mut path = RelativePathBuf::new(); |
| 711 |
/// path.push("foo"); |
| 712 |
/// path.push("bar"); |
| 713 |
/// |
| 714 |
/// assert_eq!("foo/bar", path); |
| 715 |
/// |
| 716 |
/// let mut path = RelativePathBuf::new(); |
| 717 |
/// path.push("foo"); |
| 718 |
/// path.push("/bar"); |
| 719 |
/// |
| 720 |
/// assert_eq!("foo/bar", path); |
| 721 |
/// ``` |
| 722 |
pub fn push<P>(&mut self, path: P) |
| 723 |
where |
| 724 |
P: AsRef<RelativePath>, |
| 725 |
{ |
| 726 |
let other = path.as_ref(); |
| 727 |
|
| 728 |
let other = if other.starts_with_sep() { |
| 729 |
&other.inner[1..] |
| 730 |
} else { |
| 731 |
&other.inner[..] |
| 732 |
}; |
| 733 |
|
| 734 |
if !self.inner.is_empty() && !self.ends_with_sep() { |
| 735 |
self.inner.push(SEP); |
| 736 |
} |
| 737 |
|
| 738 |
self.inner.push_str(other); |
| 739 |
} |
| 740 |
|
| 741 |
/// Updates [`file_name`] to `file_name`. |
| 742 |
/// |
| 743 |
/// If [`file_name`] was [`None`], this is equivalent to pushing |
| 744 |
/// `file_name`. |
| 745 |
/// |
| 746 |
/// Otherwise it is equivalent to calling [`pop`] and then pushing |
| 747 |
/// `file_name`. The new path will be a sibling of the original path. (That |
| 748 |
/// is, it will have the same parent.) |
| 749 |
/// |
| 750 |
/// [`file_name`]: RelativePath::file_name |
| 751 |
/// [`pop`]: RelativePathBuf::pop |
| 752 |
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html |
| 753 |
/// |
| 754 |
/// # Examples |
| 755 |
/// |
| 756 |
/// ``` |
| 757 |
/// use relative_path::RelativePathBuf; |
| 758 |
/// |
| 759 |
/// let mut buf = RelativePathBuf::from(""); |
| 760 |
/// assert!(buf.file_name() == None); |
| 761 |
/// buf.set_file_name("bar"); |
| 762 |
/// assert_eq!(RelativePathBuf::from("bar"), buf); |
| 763 |
/// |
| 764 |
/// assert!(buf.file_name().is_some()); |
| 765 |
/// buf.set_file_name("baz.txt"); |
| 766 |
/// assert_eq!(RelativePathBuf::from("baz.txt"), buf); |
| 767 |
/// |
| 768 |
/// buf.push("bar"); |
| 769 |
/// assert!(buf.file_name().is_some()); |
| 770 |
/// buf.set_file_name("bar.txt"); |
| 771 |
/// assert_eq!(RelativePathBuf::from("baz.txt/bar.txt"), buf); |
| 772 |
/// ``` |
| 773 |
pub fn set_file_name<S: AsRef<str>>(&mut self, file_name: S) { |
| 774 |
if self.file_name().is_some() { |
| 775 |
let popped = self.pop(); |
| 776 |
debug_assert!(popped); |
| 777 |
} |
| 778 |
|
| 779 |
self.push(file_name.as_ref()); |
| 780 |
} |
| 781 |
|
| 782 |
/// Updates [`extension`] to `extension`. |
| 783 |
/// |
| 784 |
/// Returns `false` and does nothing if |
| 785 |
/// [`file_name`][RelativePath::file_name] is [`None`], returns `true` and |
| 786 |
/// updates the extension otherwise. |
| 787 |
/// |
| 788 |
/// If [`extension`] is [`None`], the extension is added; otherwise it is |
| 789 |
/// replaced. |
| 790 |
/// |
| 791 |
/// [`extension`]: RelativePath::extension |
| 792 |
/// |
| 793 |
/// # Examples |
| 794 |
/// |
| 795 |
/// ``` |
| 796 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 797 |
/// |
| 798 |
/// let mut p = RelativePathBuf::from("feel/the"); |
| 799 |
/// |
| 800 |
/// p.set_extension("force"); |
| 801 |
/// assert_eq!(RelativePath::new("feel/the.force"), p); |
| 802 |
/// |
| 803 |
/// p.set_extension("dark_side"); |
| 804 |
/// assert_eq!(RelativePath::new("feel/the.dark_side"), p); |
| 805 |
/// |
| 806 |
/// assert!(p.pop()); |
| 807 |
/// p.set_extension("nothing"); |
| 808 |
/// assert_eq!(RelativePath::new("feel.nothing"), p); |
| 809 |
/// ``` |
| 810 |
pub fn set_extension<S: AsRef<str>>(&mut self, extension: S) -> bool { |
| 811 |
let file_stem = match self.file_stem() { |
| 812 |
Some(stem) => stem, |
| 813 |
None => return false, |
| 814 |
}; |
| 815 |
|
| 816 |
let end_file_stem = file_stem[file_stem.len()..].as_ptr() as usize; |
| 817 |
let start = self.inner.as_ptr() as usize; |
| 818 |
self.inner.truncate(end_file_stem.wrapping_sub(start)); |
| 819 |
|
| 820 |
let extension = extension.as_ref(); |
| 821 |
|
| 822 |
if !extension.is_empty() { |
| 823 |
self.inner.push(STEM_SEP); |
| 824 |
self.inner.push_str(extension); |
| 825 |
} |
| 826 |
|
| 827 |
true |
| 828 |
} |
| 829 |
|
| 830 |
/// Truncates `self` to [`parent`][RelativePath::parent]. |
| 831 |
/// |
| 832 |
/// # Examples |
| 833 |
/// |
| 834 |
/// ``` |
| 835 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 836 |
/// |
| 837 |
/// let mut p = RelativePathBuf::from("test/test.rs"); |
| 838 |
/// |
| 839 |
/// assert_eq!(true, p.pop()); |
| 840 |
/// assert_eq!(RelativePath::new("test"), p); |
| 841 |
/// assert_eq!(true, p.pop()); |
| 842 |
/// assert_eq!(RelativePath::new(""), p); |
| 843 |
/// assert_eq!(false, p.pop()); |
| 844 |
/// assert_eq!(RelativePath::new(""), p); |
| 845 |
/// ``` |
| 846 |
pub fn pop(&mut self) -> bool { |
| 847 |
match self.parent().map(|p| p.inner.len()) { |
| 848 |
Some(len) => { |
| 849 |
self.inner.truncate(len); |
| 850 |
true |
| 851 |
} |
| 852 |
None => false, |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
/// Coerce to a [`RelativePath`] slice. |
| 857 |
#[must_use] |
| 858 |
pub fn as_relative_path(&self) -> &RelativePath { |
| 859 |
self |
| 860 |
} |
| 861 |
|
| 862 |
/// Consumes the `RelativePathBuf`, yielding its internal [`String`] storage. |
| 863 |
/// |
| 864 |
/// # Examples |
| 865 |
/// |
| 866 |
/// ``` |
| 867 |
/// use relative_path::RelativePathBuf; |
| 868 |
/// |
| 869 |
/// let p = RelativePathBuf::from("/the/head"); |
| 870 |
/// let string = p.into_string(); |
| 871 |
/// assert_eq!(string, "/the/head".to_owned()); |
| 872 |
/// ``` |
| 873 |
#[must_use] |
| 874 |
pub fn into_string(self) -> String { |
| 875 |
self.inner |
| 876 |
} |
| 877 |
|
| 878 |
/// Converts this `RelativePathBuf` into a [boxed][std::boxed::Box] |
| 879 |
/// [`RelativePath`]. |
| 880 |
#[must_use] |
| 881 |
pub fn into_boxed_relative_path(self) -> Box<RelativePath> { |
| 882 |
let rw = Box::into_raw(self.inner.into_boxed_str()) as *mut RelativePath; |
| 883 |
unsafe { Box::from_raw(rw) } |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
impl Default for RelativePathBuf { |
| 888 |
fn default() -> Self { |
| 889 |
RelativePathBuf::new() |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
impl<'a> From<&'a RelativePath> for Cow<'a, RelativePath> { |
| 894 |
#[inline] |
| 895 |
fn from(s: &'a RelativePath) -> Cow<'a, RelativePath> { |
| 896 |
Cow::Borrowed(s) |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
impl<'a> From<RelativePathBuf> for Cow<'a, RelativePath> { |
| 901 |
#[inline] |
| 902 |
fn from(s: RelativePathBuf) -> Cow<'a, RelativePath> { |
| 903 |
Cow::Owned(s) |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
impl fmt::Debug for RelativePathBuf { |
| 908 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 909 |
write!(fmt, "{:?}", &self.inner) |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
impl AsRef<RelativePath> for RelativePathBuf { |
| 914 |
fn as_ref(&self) -> &RelativePath { |
| 915 |
RelativePath::new(&self.inner) |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
impl AsRef<str> for RelativePath { |
| 920 |
fn as_ref(&self) -> &str { |
| 921 |
&self.inner |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
impl Borrow<RelativePath> for RelativePathBuf { |
| 926 |
#[inline] |
| 927 |
fn borrow(&self) -> &RelativePath { |
| 928 |
self |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
impl<'a, T: ?Sized + AsRef<str>> From<&'a T> for RelativePathBuf { |
| 933 |
fn from(path: &'a T) -> RelativePathBuf { |
| 934 |
RelativePathBuf { |
| 935 |
inner: path.as_ref().to_owned(), |
| 936 |
} |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
impl From<String> for RelativePathBuf { |
| 941 |
fn from(path: String) -> RelativePathBuf { |
| 942 |
RelativePathBuf { inner: path } |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
impl From<RelativePathBuf> for String { |
| 947 |
fn from(path: RelativePathBuf) -> String { |
| 948 |
path.into_string() |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
impl ops::Deref for RelativePathBuf { |
| 953 |
type Target = RelativePath; |
| 954 |
|
| 955 |
fn deref(&self) -> &RelativePath { |
| 956 |
RelativePath::new(&self.inner) |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
impl cmp::PartialEq for RelativePathBuf { |
| 961 |
fn eq(&self, other: &RelativePathBuf) -> bool { |
| 962 |
self.components() == other.components() |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
impl cmp::Eq for RelativePathBuf {} |
| 967 |
|
| 968 |
impl cmp::PartialOrd for RelativePathBuf { |
| 969 |
#[inline] |
| 970 |
fn partial_cmp(&self, other: &RelativePathBuf) -> Option<cmp::Ordering> { |
| 971 |
Some(self.cmp(other)) |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
impl cmp::Ord for RelativePathBuf { |
| 976 |
#[inline] |
| 977 |
fn cmp(&self, other: &RelativePathBuf) -> cmp::Ordering { |
| 978 |
self.components().cmp(other.components()) |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
impl Hash for RelativePathBuf { |
| 983 |
fn hash<H: Hasher>(&self, h: &mut H) { |
| 984 |
self.as_relative_path().hash(h); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
impl<P> Extend<P> for RelativePathBuf |
| 989 |
where |
| 990 |
P: AsRef<RelativePath>, |
| 991 |
{ |
| 992 |
#[inline] |
| 993 |
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) { |
| 994 |
iter.into_iter().for_each(move |p| self.push(p.as_ref())); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
impl<P> FromIterator<P> for RelativePathBuf |
| 999 |
where |
| 1000 |
P: AsRef<RelativePath>, |
| 1001 |
{ |
| 1002 |
#[inline] |
| 1003 |
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> RelativePathBuf { |
| 1004 |
let mut buf = RelativePathBuf::new(); |
| 1005 |
buf.extend(iter); |
| 1006 |
buf |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
/// A borrowed, immutable relative path. |
| 1011 |
#[repr(transparent)] |
| 1012 |
pub struct RelativePath { |
| 1013 |
inner: str, |
| 1014 |
} |
| 1015 |
|
| 1016 |
/// An error returned from [`strip_prefix`] if the prefix was not found. |
| 1017 |
/// |
| 1018 |
/// [`strip_prefix`]: RelativePath::strip_prefix |
| 1019 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 1020 |
pub struct StripPrefixError(()); |
| 1021 |
|
| 1022 |
impl RelativePath { |
| 1023 |
/// Directly wraps a string slice as a `RelativePath` slice. |
| 1024 |
pub fn new<S: AsRef<str> + ?Sized>(s: &S) -> &RelativePath { |
| 1025 |
unsafe { &*(s.as_ref() as *const str as *const RelativePath) } |
| 1026 |
} |
| 1027 |
|
| 1028 |
/// Try to convert a [`Path`] to a [`RelativePath`] without allocating a buffer. |
| 1029 |
/// |
| 1030 |
/// [`Path`]: std::path::Path |
| 1031 |
/// |
| 1032 |
/// # Errors |
| 1033 |
/// |
| 1034 |
/// This requires the path to be a legal, platform-neutral relative path. |
| 1035 |
/// Otherwise various forms of [`FromPathError`] will be returned as an |
| 1036 |
/// [`Err`]. |
| 1037 |
/// |
| 1038 |
/// # Examples |
| 1039 |
/// |
| 1040 |
/// ``` |
| 1041 |
/// use relative_path::{RelativePath, FromPathErrorKind}; |
| 1042 |
/// |
| 1043 |
/// assert_eq!( |
| 1044 |
/// Ok(RelativePath::new("foo/bar")), |
| 1045 |
/// RelativePath::from_path("foo/bar") |
| 1046 |
/// ); |
| 1047 |
/// |
| 1048 |
/// // Note: absolute paths are different depending on platform. |
| 1049 |
/// if cfg!(windows) { |
| 1050 |
/// let e = RelativePath::from_path("c:\\foo\\bar").unwrap_err(); |
| 1051 |
/// assert_eq!(FromPathErrorKind::NonRelative, e.kind()); |
| 1052 |
/// } |
| 1053 |
/// |
| 1054 |
/// if cfg!(unix) { |
| 1055 |
/// let e = RelativePath::from_path("/foo/bar").unwrap_err(); |
| 1056 |
/// assert_eq!(FromPathErrorKind::NonRelative, e.kind()); |
| 1057 |
/// } |
| 1058 |
/// ``` |
| 1059 |
pub fn from_path<P: ?Sized + AsRef<path::Path>>( |
| 1060 |
path: &P, |
| 1061 |
) -> Result<&RelativePath, FromPathError> { |
| 1062 |
use std::path::Component::{CurDir, Normal, ParentDir, Prefix, RootDir}; |
| 1063 |
|
| 1064 |
let other = path.as_ref(); |
| 1065 |
|
| 1066 |
let s = match other.to_str() { |
| 1067 |
Some(s) => s, |
| 1068 |
None => return Err(FromPathErrorKind::NonUtf8.into()), |
| 1069 |
}; |
| 1070 |
|
| 1071 |
let rel = RelativePath::new(s); |
| 1072 |
|
| 1073 |
// check that the component compositions are equal. |
| 1074 |
for (a, b) in other.components().zip(rel.components()) { |
| 1075 |
match (a, b) { |
| 1076 |
(Prefix(_) | RootDir, _) => return Err(FromPathErrorKind::NonRelative.into()), |
| 1077 |
(CurDir, Component::CurDir) | (ParentDir, Component::ParentDir) => continue, |
| 1078 |
(Normal(a), Component::Normal(b)) if a == b => continue, |
| 1079 |
_ => return Err(FromPathErrorKind::BadSeparator.into()), |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
Ok(rel) |
| 1084 |
} |
| 1085 |
|
| 1086 |
/// Yields the underlying [`str`] slice. |
| 1087 |
/// |
| 1088 |
/// [`str`]: prim@str |
| 1089 |
/// |
| 1090 |
/// # Examples |
| 1091 |
/// |
| 1092 |
/// ``` |
| 1093 |
/// use relative_path::RelativePath; |
| 1094 |
/// |
| 1095 |
/// assert_eq!(RelativePath::new("foo.txt").as_str(), "foo.txt"); |
| 1096 |
/// ``` |
| 1097 |
#[must_use] |
| 1098 |
pub fn as_str(&self) -> &str { |
| 1099 |
&self.inner |
| 1100 |
} |
| 1101 |
|
| 1102 |
/// Returns an object that implements [`Display`][std::fmt::Display]. |
| 1103 |
/// |
| 1104 |
/// # Examples |
| 1105 |
/// |
| 1106 |
/// ``` |
| 1107 |
/// use relative_path::RelativePath; |
| 1108 |
/// |
| 1109 |
/// let path = RelativePath::new("tmp/foo.rs"); |
| 1110 |
/// |
| 1111 |
/// println!("{}", path.display()); |
| 1112 |
/// ``` |
| 1113 |
#[deprecated(note = "RelativePath implements std::fmt::Display directly")] |
| 1114 |
#[must_use] |
| 1115 |
pub fn display(&self) -> Display { |
| 1116 |
Display { path: self } |
| 1117 |
} |
| 1118 |
|
| 1119 |
/// Creates an owned [`RelativePathBuf`] with path adjoined to self. |
| 1120 |
/// |
| 1121 |
/// # Examples |
| 1122 |
/// |
| 1123 |
/// ``` |
| 1124 |
/// use relative_path::RelativePath; |
| 1125 |
/// |
| 1126 |
/// let path = RelativePath::new("foo/bar"); |
| 1127 |
/// assert_eq!("foo/bar/baz", path.join("baz")); |
| 1128 |
/// ``` |
| 1129 |
pub fn join<P>(&self, path: P) -> RelativePathBuf |
| 1130 |
where |
| 1131 |
P: AsRef<RelativePath>, |
| 1132 |
{ |
| 1133 |
let mut out = self.to_relative_path_buf(); |
| 1134 |
out.push(path); |
| 1135 |
out |
| 1136 |
} |
| 1137 |
|
| 1138 |
/// Iterate over all components in this relative path. |
| 1139 |
/// |
| 1140 |
/// # Examples |
| 1141 |
/// |
| 1142 |
/// ``` |
| 1143 |
/// use relative_path::{Component, RelativePath}; |
| 1144 |
/// |
| 1145 |
/// let path = RelativePath::new("foo/bar/baz"); |
| 1146 |
/// let mut it = path.components(); |
| 1147 |
/// |
| 1148 |
/// assert_eq!(Some(Component::Normal("foo")), it.next()); |
| 1149 |
/// assert_eq!(Some(Component::Normal("bar")), it.next()); |
| 1150 |
/// assert_eq!(Some(Component::Normal("baz")), it.next()); |
| 1151 |
/// assert_eq!(None, it.next()); |
| 1152 |
/// ``` |
| 1153 |
#[must_use] |
| 1154 |
pub fn components(&self) -> Components { |
| 1155 |
Components::new(&self.inner) |
| 1156 |
} |
| 1157 |
|
| 1158 |
/// Produces an iterator over the path's components viewed as [`str`] |
| 1159 |
/// slices. |
| 1160 |
/// |
| 1161 |
/// For more information about the particulars of how the path is separated |
| 1162 |
/// into components, see [`components`][Self::components]. |
| 1163 |
/// |
| 1164 |
/// [`str`]: prim@str |
| 1165 |
/// |
| 1166 |
/// # Examples |
| 1167 |
/// |
| 1168 |
/// ``` |
| 1169 |
/// use relative_path::RelativePath; |
| 1170 |
/// |
| 1171 |
/// let mut it = RelativePath::new("/tmp/foo.txt").iter(); |
| 1172 |
/// assert_eq!(it.next(), Some("tmp")); |
| 1173 |
/// assert_eq!(it.next(), Some("foo.txt")); |
| 1174 |
/// assert_eq!(it.next(), None) |
| 1175 |
/// ``` |
| 1176 |
#[must_use] |
| 1177 |
pub fn iter(&self) -> Iter { |
| 1178 |
Iter { |
| 1179 |
inner: self.components(), |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
/// Convert to an owned [`RelativePathBuf`]. |
| 1184 |
#[must_use] |
| 1185 |
pub fn to_relative_path_buf(&self) -> RelativePathBuf { |
| 1186 |
RelativePathBuf::from(self.inner.to_owned()) |
| 1187 |
} |
| 1188 |
|
| 1189 |
/// Build an owned [`PathBuf`] relative to `base` for the current relative |
| 1190 |
/// path. |
| 1191 |
/// |
| 1192 |
/// # Examples |
| 1193 |
/// |
| 1194 |
/// ``` |
| 1195 |
/// use relative_path::RelativePath; |
| 1196 |
/// use std::path::Path; |
| 1197 |
/// |
| 1198 |
/// let path = RelativePath::new("foo/bar").to_path("."); |
| 1199 |
/// assert_eq!(Path::new("./foo/bar"), path); |
| 1200 |
/// |
| 1201 |
/// let path = RelativePath::new("foo/bar").to_path(""); |
| 1202 |
/// assert_eq!(Path::new("foo/bar"), path); |
| 1203 |
/// ``` |
| 1204 |
/// |
| 1205 |
/// # Encoding an absolute path |
| 1206 |
/// |
| 1207 |
/// Absolute paths are, in contrast to when using [`PathBuf::push`] *ignored* |
| 1208 |
/// and will be added unchanged to the buffer. |
| 1209 |
/// |
| 1210 |
/// This is to preserve the probability of a path conversion failing if the |
| 1211 |
/// relative path contains platform-specific absolute path components. |
| 1212 |
/// |
| 1213 |
/// ``` |
| 1214 |
/// use relative_path::RelativePath; |
| 1215 |
/// use std::path::Path; |
| 1216 |
/// |
| 1217 |
/// if cfg!(windows) { |
| 1218 |
/// let path = RelativePath::new("/bar/baz").to_path("foo"); |
| 1219 |
/// assert_eq!(Path::new("foo\\bar\\baz"), path); |
| 1220 |
/// |
| 1221 |
/// let path = RelativePath::new("c:\\bar\\baz").to_path("foo"); |
| 1222 |
/// assert_eq!(Path::new("foo\\c:\\bar\\baz"), path); |
| 1223 |
/// } |
| 1224 |
/// |
| 1225 |
/// if cfg!(unix) { |
| 1226 |
/// let path = RelativePath::new("/bar/baz").to_path("foo"); |
| 1227 |
/// assert_eq!(Path::new("foo/bar/baz"), path); |
| 1228 |
/// |
| 1229 |
/// let path = RelativePath::new("c:\\bar\\baz").to_path("foo"); |
| 1230 |
/// assert_eq!(Path::new("foo/c:\\bar\\baz"), path); |
| 1231 |
/// } |
| 1232 |
/// ``` |
| 1233 |
/// |
| 1234 |
/// [`PathBuf`]: std::path::PathBuf |
| 1235 |
/// [`PathBuf::push`]: std::path::PathBuf::push |
| 1236 |
pub fn to_path<P: AsRef<path::Path>>(&self, base: P) -> path::PathBuf { |
| 1237 |
let mut p = base.as_ref().to_path_buf().into_os_string(); |
| 1238 |
|
| 1239 |
for c in self.components() { |
| 1240 |
if !p.is_empty() { |
| 1241 |
p.push(path::MAIN_SEPARATOR.encode_utf8(&mut [0u8, 0u8, 0u8, 0u8])); |
| 1242 |
} |
| 1243 |
|
| 1244 |
p.push(c.as_str()); |
| 1245 |
} |
| 1246 |
|
| 1247 |
path::PathBuf::from(p) |
| 1248 |
} |
| 1249 |
|
| 1250 |
/// Build an owned [`PathBuf`] relative to `base` for the current relative |
| 1251 |
/// path. |
| 1252 |
/// |
| 1253 |
/// This is similar to [`to_path`] except that it doesn't just |
| 1254 |
/// unconditionally append one path to the other, instead it performs the |
| 1255 |
/// following operations depending on its own components: |
| 1256 |
/// |
| 1257 |
/// * [`Component::CurDir`] leaves the `base` unmodified. |
| 1258 |
/// * [`Component::ParentDir`] removes a component from `base` using |
| 1259 |
/// [`path::PathBuf::pop`]. |
| 1260 |
/// * [`Component::Normal`] pushes the given path component onto `base` |
| 1261 |
/// using the same mechanism as [`to_path`]. |
| 1262 |
/// |
| 1263 |
/// [`to_path`]: RelativePath::to_path |
| 1264 |
/// |
| 1265 |
/// Note that the exact semantics of the path operation is determined by the |
| 1266 |
/// corresponding [`PathBuf`] operation. E.g. popping a component off a path |
| 1267 |
/// like `.` will result in an empty path. |
| 1268 |
/// |
| 1269 |
/// ``` |
| 1270 |
/// use relative_path::RelativePath; |
| 1271 |
/// use std::path::Path; |
| 1272 |
/// |
| 1273 |
/// let path = RelativePath::new("..").to_logical_path("."); |
| 1274 |
/// assert_eq!(path, Path::new("")); |
| 1275 |
/// ``` |
| 1276 |
/// |
| 1277 |
/// # Examples |
| 1278 |
/// |
| 1279 |
/// ``` |
| 1280 |
/// use relative_path::RelativePath; |
| 1281 |
/// use std::path::Path; |
| 1282 |
/// |
| 1283 |
/// let path = RelativePath::new("..").to_logical_path("foo/bar"); |
| 1284 |
/// assert_eq!(path, Path::new("foo")); |
| 1285 |
/// ``` |
| 1286 |
/// |
| 1287 |
/// # Encoding an absolute path |
| 1288 |
/// |
| 1289 |
/// Behaves the same as [`to_path`][RelativePath::to_path] when encoding |
| 1290 |
/// absolute paths. |
| 1291 |
/// |
| 1292 |
/// Absolute paths are, in contrast to when using [`PathBuf::push`] *ignored* |
| 1293 |
/// and will be added unchanged to the buffer. |
| 1294 |
/// |
| 1295 |
/// This is to preserve the probability of a path conversion failing if the |
| 1296 |
/// relative path contains platform-specific absolute path components. |
| 1297 |
/// |
| 1298 |
/// ``` |
| 1299 |
/// use relative_path::RelativePath; |
| 1300 |
/// use std::path::Path; |
| 1301 |
/// |
| 1302 |
/// if cfg!(windows) { |
| 1303 |
/// let path = RelativePath::new("/bar/baz").to_logical_path("foo"); |
| 1304 |
/// assert_eq!(Path::new("foo\\bar\\baz"), path); |
| 1305 |
/// |
| 1306 |
/// let path = RelativePath::new("c:\\bar\\baz").to_logical_path("foo"); |
| 1307 |
/// assert_eq!(Path::new("foo\\c:\\bar\\baz"), path); |
| 1308 |
/// |
| 1309 |
/// let path = RelativePath::new("foo/bar").to_logical_path(""); |
| 1310 |
/// assert_eq!(Path::new("foo\\bar"), path); |
| 1311 |
/// } |
| 1312 |
/// |
| 1313 |
/// if cfg!(unix) { |
| 1314 |
/// let path = RelativePath::new("/bar/baz").to_logical_path("foo"); |
| 1315 |
/// assert_eq!(Path::new("foo/bar/baz"), path); |
| 1316 |
/// |
| 1317 |
/// let path = RelativePath::new("c:\\bar\\baz").to_logical_path("foo"); |
| 1318 |
/// assert_eq!(Path::new("foo/c:\\bar\\baz"), path); |
| 1319 |
/// |
| 1320 |
/// let path = RelativePath::new("foo/bar").to_logical_path(""); |
| 1321 |
/// assert_eq!(Path::new("foo/bar"), path); |
| 1322 |
/// } |
| 1323 |
/// ``` |
| 1324 |
/// |
| 1325 |
/// [`PathBuf`]: std::path::PathBuf |
| 1326 |
/// [`PathBuf::push`]: std::path::PathBuf::push |
| 1327 |
pub fn to_logical_path<P: AsRef<path::Path>>(&self, base: P) -> path::PathBuf { |
| 1328 |
use self::Component::{CurDir, Normal, ParentDir}; |
| 1329 |
|
| 1330 |
let mut p = base.as_ref().to_path_buf().into_os_string(); |
| 1331 |
|
| 1332 |
for c in self.components() { |
| 1333 |
match c { |
| 1334 |
CurDir => continue, |
| 1335 |
ParentDir => { |
| 1336 |
let mut temp = path::PathBuf::from(std::mem::take(&mut p)); |
| 1337 |
temp.pop(); |
| 1338 |
p = temp.into_os_string(); |
| 1339 |
} |
| 1340 |
Normal(c) => { |
| 1341 |
if !p.is_empty() { |
| 1342 |
p.push(path::MAIN_SEPARATOR.encode_utf8(&mut [0u8, 0u8, 0u8, 0u8])); |
| 1343 |
} |
| 1344 |
|
| 1345 |
p.push(c); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
path::PathBuf::from(p) |
| 1351 |
} |
| 1352 |
|
| 1353 |
/// Returns a relative path, without its final [`Component`] if there is one. |
| 1354 |
/// |
| 1355 |
/// # Examples |
| 1356 |
/// |
| 1357 |
/// ``` |
| 1358 |
/// use relative_path::RelativePath; |
| 1359 |
/// |
| 1360 |
/// assert_eq!(Some(RelativePath::new("foo")), RelativePath::new("foo/bar").parent()); |
| 1361 |
/// assert_eq!(Some(RelativePath::new("")), RelativePath::new("foo").parent()); |
| 1362 |
/// assert_eq!(None, RelativePath::new("").parent()); |
| 1363 |
/// ``` |
| 1364 |
#[must_use] |
| 1365 |
pub fn parent(&self) -> Option<&RelativePath> { |
| 1366 |
use self::Component::CurDir; |
| 1367 |
|
| 1368 |
if self.inner.is_empty() { |
| 1369 |
return None; |
| 1370 |
} |
| 1371 |
|
| 1372 |
let mut it = self.components(); |
| 1373 |
while let Some(CurDir) = it.next_back() {} |
| 1374 |
Some(it.as_relative_path()) |
| 1375 |
} |
| 1376 |
|
| 1377 |
/// Returns the final component of the `RelativePath`, if there is one. |
| 1378 |
/// |
| 1379 |
/// If the path is a normal file, this is the file name. If it's the path of |
| 1380 |
/// a directory, this is the directory name. |
| 1381 |
/// |
| 1382 |
/// Returns [`None`] If the path terminates in `..`. |
| 1383 |
/// |
| 1384 |
/// # Examples |
| 1385 |
/// |
| 1386 |
/// ``` |
| 1387 |
/// use relative_path::RelativePath; |
| 1388 |
/// |
| 1389 |
/// assert_eq!(Some("bin"), RelativePath::new("usr/bin/").file_name()); |
| 1390 |
/// assert_eq!(Some("foo.txt"), RelativePath::new("tmp/foo.txt").file_name()); |
| 1391 |
/// assert_eq!(Some("foo.txt"), RelativePath::new("tmp/foo.txt/").file_name()); |
| 1392 |
/// assert_eq!(Some("foo.txt"), RelativePath::new("foo.txt/.").file_name()); |
| 1393 |
/// assert_eq!(Some("foo.txt"), RelativePath::new("foo.txt/.//").file_name()); |
| 1394 |
/// assert_eq!(None, RelativePath::new("foo.txt/..").file_name()); |
| 1395 |
/// assert_eq!(None, RelativePath::new("/").file_name()); |
| 1396 |
/// ``` |
| 1397 |
#[must_use] |
| 1398 |
pub fn file_name(&self) -> Option<&str> { |
| 1399 |
use self::Component::{CurDir, Normal, ParentDir}; |
| 1400 |
|
| 1401 |
let mut it = self.components(); |
| 1402 |
|
| 1403 |
while let Some(c) = it.next_back() { |
| 1404 |
return match c { |
| 1405 |
CurDir => continue, |
| 1406 |
Normal(name) => Some(name), |
| 1407 |
ParentDir => None, |
| 1408 |
}; |
| 1409 |
} |
| 1410 |
|
| 1411 |
None |
| 1412 |
} |
| 1413 |
|
| 1414 |
/// Returns a relative path that, when joined onto `base`, yields `self`. |
| 1415 |
/// |
| 1416 |
/// # Errors |
| 1417 |
/// |
| 1418 |
/// If `base` is not a prefix of `self` (i.e. [`starts_with`] returns |
| 1419 |
/// `false`), returns [`Err`]. |
| 1420 |
/// |
| 1421 |
/// [`starts_with`]: Self::starts_with |
| 1422 |
/// |
| 1423 |
/// # Examples |
| 1424 |
/// |
| 1425 |
/// ``` |
| 1426 |
/// use relative_path::RelativePath; |
| 1427 |
/// |
| 1428 |
/// let path = RelativePath::new("test/haha/foo.txt"); |
| 1429 |
/// |
| 1430 |
/// assert_eq!(path.strip_prefix("test"), Ok(RelativePath::new("haha/foo.txt"))); |
| 1431 |
/// assert_eq!(path.strip_prefix("test").is_ok(), true); |
| 1432 |
/// assert_eq!(path.strip_prefix("haha").is_ok(), false); |
| 1433 |
/// ``` |
| 1434 |
pub fn strip_prefix<P>(&self, base: P) -> Result<&RelativePath, StripPrefixError> |
| 1435 |
where |
| 1436 |
P: AsRef<RelativePath>, |
| 1437 |
{ |
| 1438 |
iter_after(self.components(), base.as_ref().components()) |
| 1439 |
.map(|c| c.as_relative_path()) |
| 1440 |
.ok_or(StripPrefixError(())) |
| 1441 |
} |
| 1442 |
|
| 1443 |
/// Determines whether `base` is a prefix of `self`. |
| 1444 |
/// |
| 1445 |
/// Only considers whole path components to match. |
| 1446 |
/// |
| 1447 |
/// # Examples |
| 1448 |
/// |
| 1449 |
/// ``` |
| 1450 |
/// use relative_path::RelativePath; |
| 1451 |
/// |
| 1452 |
/// let path = RelativePath::new("etc/passwd"); |
| 1453 |
/// |
| 1454 |
/// assert!(path.starts_with("etc")); |
| 1455 |
/// |
| 1456 |
/// assert!(!path.starts_with("e")); |
| 1457 |
/// ``` |
| 1458 |
pub fn starts_with<P>(&self, base: P) -> bool |
| 1459 |
where |
| 1460 |
P: AsRef<RelativePath>, |
| 1461 |
{ |
| 1462 |
iter_after(self.components(), base.as_ref().components()).is_some() |
| 1463 |
} |
| 1464 |
|
| 1465 |
/// Determines whether `child` is a suffix of `self`. |
| 1466 |
/// |
| 1467 |
/// Only considers whole path components to match. |
| 1468 |
/// |
| 1469 |
/// # Examples |
| 1470 |
/// |
| 1471 |
/// ``` |
| 1472 |
/// use relative_path::RelativePath; |
| 1473 |
/// |
| 1474 |
/// let path = RelativePath::new("etc/passwd"); |
| 1475 |
/// |
| 1476 |
/// assert!(path.ends_with("passwd")); |
| 1477 |
/// ``` |
| 1478 |
pub fn ends_with<P>(&self, child: P) -> bool |
| 1479 |
where |
| 1480 |
P: AsRef<RelativePath>, |
| 1481 |
{ |
| 1482 |
iter_after(self.components().rev(), child.as_ref().components().rev()).is_some() |
| 1483 |
} |
| 1484 |
|
| 1485 |
/// Determines whether `self` is normalized. |
| 1486 |
/// |
| 1487 |
/// # Examples |
| 1488 |
/// |
| 1489 |
/// ``` |
| 1490 |
/// use relative_path::RelativePath; |
| 1491 |
/// |
| 1492 |
/// // These are normalized. |
| 1493 |
/// assert!(RelativePath::new("").is_normalized()); |
| 1494 |
/// assert!(RelativePath::new("baz.txt").is_normalized()); |
| 1495 |
/// assert!(RelativePath::new("foo/bar/baz.txt").is_normalized()); |
| 1496 |
/// assert!(RelativePath::new("..").is_normalized()); |
| 1497 |
/// assert!(RelativePath::new("../..").is_normalized()); |
| 1498 |
/// assert!(RelativePath::new("../../foo/bar/baz.txt").is_normalized()); |
| 1499 |
/// |
| 1500 |
/// // These are not normalized. |
| 1501 |
/// assert!(!RelativePath::new(".").is_normalized()); |
| 1502 |
/// assert!(!RelativePath::new("./baz.txt").is_normalized()); |
| 1503 |
/// assert!(!RelativePath::new("foo/..").is_normalized()); |
| 1504 |
/// assert!(!RelativePath::new("foo/../baz.txt").is_normalized()); |
| 1505 |
/// assert!(!RelativePath::new("foo/.").is_normalized()); |
| 1506 |
/// assert!(!RelativePath::new("foo/./baz.txt").is_normalized()); |
| 1507 |
/// assert!(!RelativePath::new("../foo/./bar/../baz.txt").is_normalized()); |
| 1508 |
/// ``` |
| 1509 |
#[must_use] |
| 1510 |
pub fn is_normalized(&self) -> bool { |
| 1511 |
self.components() |
| 1512 |
.skip_while(|c| matches!(c, Component::ParentDir)) |
| 1513 |
.all(|c| matches!(c, Component::Normal(_))) |
| 1514 |
} |
| 1515 |
|
| 1516 |
/// Creates an owned [`RelativePathBuf`] like `self` but with the given file |
| 1517 |
/// name. |
| 1518 |
/// |
| 1519 |
/// See [`set_file_name`] for more details. |
| 1520 |
/// |
| 1521 |
/// [`set_file_name`]: RelativePathBuf::set_file_name |
| 1522 |
/// |
| 1523 |
/// # Examples |
| 1524 |
/// |
| 1525 |
/// ``` |
| 1526 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 1527 |
/// |
| 1528 |
/// let path = RelativePath::new("tmp/foo.txt"); |
| 1529 |
/// assert_eq!(path.with_file_name("bar.txt"), RelativePathBuf::from("tmp/bar.txt")); |
| 1530 |
/// |
| 1531 |
/// let path = RelativePath::new("tmp"); |
| 1532 |
/// assert_eq!(path.with_file_name("var"), RelativePathBuf::from("var")); |
| 1533 |
/// ``` |
| 1534 |
pub fn with_file_name<S: AsRef<str>>(&self, file_name: S) -> RelativePathBuf { |
| 1535 |
let mut buf = self.to_relative_path_buf(); |
| 1536 |
buf.set_file_name(file_name); |
| 1537 |
buf |
| 1538 |
} |
| 1539 |
|
| 1540 |
/// Extracts the stem (non-extension) portion of [`file_name`][Self::file_name]. |
| 1541 |
/// |
| 1542 |
/// The stem is: |
| 1543 |
/// |
| 1544 |
/// * [`None`], if there is no file name; |
| 1545 |
/// * The entire file name if there is no embedded `.`; |
| 1546 |
/// * The entire file name if the file name begins with `.` and has no other `.`s within; |
| 1547 |
/// * Otherwise, the portion of the file name before the final `.` |
| 1548 |
/// |
| 1549 |
/// # Examples |
| 1550 |
/// |
| 1551 |
/// ``` |
| 1552 |
/// use relative_path::RelativePath; |
| 1553 |
/// |
| 1554 |
/// let path = RelativePath::new("foo.rs"); |
| 1555 |
/// |
| 1556 |
/// assert_eq!("foo", path.file_stem().unwrap()); |
| 1557 |
/// ``` |
| 1558 |
pub fn file_stem(&self) -> Option<&str> { |
| 1559 |
self.file_name() |
| 1560 |
.map(split_file_at_dot) |
| 1561 |
.and_then(|(before, after)| before.or(after)) |
| 1562 |
} |
| 1563 |
|
| 1564 |
/// Extracts the extension of [`file_name`][Self::file_name], if possible. |
| 1565 |
/// |
| 1566 |
/// The extension is: |
| 1567 |
/// |
| 1568 |
/// * [`None`], if there is no file name; |
| 1569 |
/// * [`None`], if there is no embedded `.`; |
| 1570 |
/// * [`None`], if the file name begins with `.` and has no other `.`s within; |
| 1571 |
/// * Otherwise, the portion of the file name after the final `.` |
| 1572 |
/// |
| 1573 |
/// # Examples |
| 1574 |
/// |
| 1575 |
/// ``` |
| 1576 |
/// use relative_path::RelativePath; |
| 1577 |
/// |
| 1578 |
/// assert_eq!(Some("rs"), RelativePath::new("foo.rs").extension()); |
| 1579 |
/// assert_eq!(None, RelativePath::new(".rs").extension()); |
| 1580 |
/// assert_eq!(Some("rs"), RelativePath::new("foo.rs/.").extension()); |
| 1581 |
/// ``` |
| 1582 |
pub fn extension(&self) -> Option<&str> { |
| 1583 |
self.file_name() |
| 1584 |
.map(split_file_at_dot) |
| 1585 |
.and_then(|(before, after)| before.and(after)) |
| 1586 |
} |
| 1587 |
|
| 1588 |
/// Creates an owned [`RelativePathBuf`] like `self` but with the given |
| 1589 |
/// extension. |
| 1590 |
/// |
| 1591 |
/// See [`set_extension`] for more details. |
| 1592 |
/// |
| 1593 |
/// [`set_extension`]: RelativePathBuf::set_extension |
| 1594 |
/// |
| 1595 |
/// # Examples |
| 1596 |
/// |
| 1597 |
/// ``` |
| 1598 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 1599 |
/// |
| 1600 |
/// let path = RelativePath::new("foo.rs"); |
| 1601 |
/// assert_eq!(path.with_extension("txt"), RelativePathBuf::from("foo.txt")); |
| 1602 |
/// ``` |
| 1603 |
pub fn with_extension<S: AsRef<str>>(&self, extension: S) -> RelativePathBuf { |
| 1604 |
let mut buf = self.to_relative_path_buf(); |
| 1605 |
buf.set_extension(extension); |
| 1606 |
buf |
| 1607 |
} |
| 1608 |
|
| 1609 |
/// Build an owned [`RelativePathBuf`], joined with the given path and |
| 1610 |
/// normalized. |
| 1611 |
/// |
| 1612 |
/// # Examples |
| 1613 |
/// |
| 1614 |
/// ``` |
| 1615 |
/// use relative_path::RelativePath; |
| 1616 |
/// |
| 1617 |
/// assert_eq!( |
| 1618 |
/// RelativePath::new("foo/baz.txt"), |
| 1619 |
/// RelativePath::new("foo/bar").join_normalized("../baz.txt").as_relative_path() |
| 1620 |
/// ); |
| 1621 |
/// |
| 1622 |
/// assert_eq!( |
| 1623 |
/// RelativePath::new("../foo/baz.txt"), |
| 1624 |
/// RelativePath::new("../foo/bar").join_normalized("../baz.txt").as_relative_path() |
| 1625 |
/// ); |
| 1626 |
/// ``` |
| 1627 |
pub fn join_normalized<P>(&self, path: P) -> RelativePathBuf |
| 1628 |
where |
| 1629 |
P: AsRef<RelativePath>, |
| 1630 |
{ |
| 1631 |
let mut buf = RelativePathBuf::new(); |
| 1632 |
relative_traversal(&mut buf, self.components()); |
| 1633 |
relative_traversal(&mut buf, path.as_ref().components()); |
| 1634 |
buf |
| 1635 |
} |
| 1636 |
|
| 1637 |
/// Return an owned [`RelativePathBuf`], with all non-normal components |
| 1638 |
/// moved to the beginning of the path. |
| 1639 |
/// |
| 1640 |
/// This permits for a normalized representation of different relative |
| 1641 |
/// components. |
| 1642 |
/// |
| 1643 |
/// Normalization is a _destructive_ operation if the path references an |
| 1644 |
/// actual filesystem path. An example of this is symlinks under unix, a |
| 1645 |
/// path like `foo/../bar` might reference a different location other than |
| 1646 |
/// `./bar`. |
| 1647 |
/// |
| 1648 |
/// Normalization is a logical operation and does not guarantee that the |
| 1649 |
/// constructed path corresponds to what the filesystem would do. On Linux |
| 1650 |
/// for example symbolic links could mean that the logical path doesn't |
| 1651 |
/// correspond to the filesystem path. |
| 1652 |
/// |
| 1653 |
/// # Examples |
| 1654 |
/// |
| 1655 |
/// ``` |
| 1656 |
/// use relative_path::RelativePath; |
| 1657 |
/// |
| 1658 |
/// assert_eq!( |
| 1659 |
/// "../foo/baz.txt", |
| 1660 |
/// RelativePath::new("../foo/./bar/../baz.txt").normalize() |
| 1661 |
/// ); |
| 1662 |
/// |
| 1663 |
/// assert_eq!( |
| 1664 |
/// "", |
| 1665 |
/// RelativePath::new(".").normalize() |
| 1666 |
/// ); |
| 1667 |
/// ``` |
| 1668 |
#[must_use] |
| 1669 |
pub fn normalize(&self) -> RelativePathBuf { |
| 1670 |
let mut buf = RelativePathBuf::with_capacity(self.inner.len()); |
| 1671 |
relative_traversal(&mut buf, self.components()); |
| 1672 |
buf |
| 1673 |
} |
| 1674 |
|
| 1675 |
/// Constructs a relative path from the current path, to `path`. |
| 1676 |
/// |
| 1677 |
/// This function will return the empty [`RelativePath`] `""` if this source |
| 1678 |
/// contains unnamed components like `..` that would have to be traversed to |
| 1679 |
/// reach the destination `path`. This is necessary since we have no way of |
| 1680 |
/// knowing what the names of those components are when we're building the |
| 1681 |
/// new relative path. |
| 1682 |
/// |
| 1683 |
/// ``` |
| 1684 |
/// use relative_path::RelativePath; |
| 1685 |
/// |
| 1686 |
/// // Here we don't know what directories `../..` refers to, so there's no |
| 1687 |
/// // way to construct a path back to `bar` in the current directory from |
| 1688 |
/// // `../..`. |
| 1689 |
/// let from = RelativePath::new("../../foo/relative-path"); |
| 1690 |
/// let to = RelativePath::new("bar"); |
| 1691 |
/// assert_eq!("", from.relative(to)); |
| 1692 |
/// ``` |
| 1693 |
/// |
| 1694 |
/// One exception to this is when two paths contains a common prefix at |
| 1695 |
/// which point there's no need to know what the names of those unnamed |
| 1696 |
/// components are. |
| 1697 |
/// |
| 1698 |
/// ``` |
| 1699 |
/// use relative_path::RelativePath; |
| 1700 |
/// |
| 1701 |
/// let from = RelativePath::new("../../foo/bar"); |
| 1702 |
/// let to = RelativePath::new("../../foo/baz"); |
| 1703 |
/// |
| 1704 |
/// assert_eq!("../baz", from.relative(to)); |
| 1705 |
/// |
| 1706 |
/// let from = RelativePath::new("../a/../../foo/bar"); |
| 1707 |
/// let to = RelativePath::new("../../foo/baz"); |
| 1708 |
/// |
| 1709 |
/// assert_eq!("../baz", from.relative(to)); |
| 1710 |
/// ``` |
| 1711 |
/// |
| 1712 |
/// # Examples |
| 1713 |
/// |
| 1714 |
/// ``` |
| 1715 |
/// use relative_path::RelativePath; |
| 1716 |
/// |
| 1717 |
/// assert_eq!( |
| 1718 |
/// "../../e/f", |
| 1719 |
/// RelativePath::new("a/b/c/d").relative(RelativePath::new("a/b/e/f")) |
| 1720 |
/// ); |
| 1721 |
/// |
| 1722 |
/// assert_eq!( |
| 1723 |
/// "../bbb", |
| 1724 |
/// RelativePath::new("a/../aaa").relative(RelativePath::new("b/../bbb")) |
| 1725 |
/// ); |
| 1726 |
/// |
| 1727 |
/// let a = RelativePath::new("git/relative-path"); |
| 1728 |
/// let b = RelativePath::new("git"); |
| 1729 |
/// assert_eq!("relative-path", b.relative(a)); |
| 1730 |
/// assert_eq!("..", a.relative(b)); |
| 1731 |
/// |
| 1732 |
/// let a = RelativePath::new("foo/bar/bap/foo.h"); |
| 1733 |
/// let b = RelativePath::new("../arch/foo.h"); |
| 1734 |
/// assert_eq!("../../../../../arch/foo.h", a.relative(b)); |
| 1735 |
/// assert_eq!("", b.relative(a)); |
| 1736 |
/// ``` |
| 1737 |
pub fn relative<P>(&self, path: P) -> RelativePathBuf |
| 1738 |
where |
| 1739 |
P: AsRef<RelativePath>, |
| 1740 |
{ |
| 1741 |
let mut from = RelativePathBuf::with_capacity(self.inner.len()); |
| 1742 |
let mut to = RelativePathBuf::with_capacity(path.as_ref().inner.len()); |
| 1743 |
|
| 1744 |
relative_traversal(&mut from, self.components()); |
| 1745 |
relative_traversal(&mut to, path.as_ref().components()); |
| 1746 |
|
| 1747 |
let mut it_from = from.components(); |
| 1748 |
let mut it_to = to.components(); |
| 1749 |
|
| 1750 |
// Strip a common prefixes - if any. |
| 1751 |
let (lead_from, lead_to) = loop { |
| 1752 |
match (it_from.next(), it_to.next()) { |
| 1753 |
(Some(f), Some(t)) if f == t => continue, |
| 1754 |
(f, t) => { |
| 1755 |
break (f, t); |
| 1756 |
} |
| 1757 |
} |
| 1758 |
}; |
| 1759 |
|
| 1760 |
// Special case: The path we are traversing from can't contain unnamed |
| 1761 |
// components. A relative path might be any path, like `/`, or |
| 1762 |
// `/foo/bar/baz`, and these components cannot be named in the relative |
| 1763 |
// traversal. |
| 1764 |
// |
| 1765 |
// Also note that `relative_traversal` guarantees that all ParentDir |
| 1766 |
// components are at the head of the path being built. |
| 1767 |
if lead_from == Some(Component::ParentDir) { |
| 1768 |
return RelativePathBuf::new(); |
| 1769 |
} |
| 1770 |
|
| 1771 |
let head = lead_from.into_iter().chain(it_from); |
| 1772 |
let tail = lead_to.into_iter().chain(it_to); |
| 1773 |
|
| 1774 |
let mut buf = RelativePathBuf::with_capacity(usize::max(from.inner.len(), to.inner.len())); |
| 1775 |
|
| 1776 |
for c in head.map(|_| Component::ParentDir).chain(tail) { |
| 1777 |
buf.push(c.as_str()); |
| 1778 |
} |
| 1779 |
|
| 1780 |
buf |
| 1781 |
} |
| 1782 |
|
| 1783 |
/// Check if path starts with a path separator. |
| 1784 |
#[inline] |
| 1785 |
fn starts_with_sep(&self) -> bool { |
| 1786 |
self.inner.starts_with(SEP) |
| 1787 |
} |
| 1788 |
|
| 1789 |
/// Check if path ends with a path separator. |
| 1790 |
#[inline] |
| 1791 |
fn ends_with_sep(&self) -> bool { |
| 1792 |
self.inner.ends_with(SEP) |
| 1793 |
} |
| 1794 |
} |
| 1795 |
|
| 1796 |
impl<'a> IntoIterator for &'a RelativePath { |
| 1797 |
type IntoIter = Iter<'a>; |
| 1798 |
type Item = &'a str; |
| 1799 |
|
| 1800 |
#[inline] |
| 1801 |
fn into_iter(self) -> Self::IntoIter { |
| 1802 |
self.iter() |
| 1803 |
} |
| 1804 |
} |
| 1805 |
|
| 1806 |
/// Conversion from a [`Box<str>`] reference to a [`Box<RelativePath>`]. |
| 1807 |
/// |
| 1808 |
/// # Examples |
| 1809 |
/// |
| 1810 |
/// ``` |
| 1811 |
/// use relative_path::RelativePath; |
| 1812 |
/// |
| 1813 |
/// let path: Box<RelativePath> = Box::<str>::from("foo/bar").into(); |
| 1814 |
/// assert_eq!(&*path, "foo/bar"); |
| 1815 |
/// ``` |
| 1816 |
impl From<Box<str>> for Box<RelativePath> { |
| 1817 |
#[inline] |
| 1818 |
fn from(boxed: Box<str>) -> Box<RelativePath> { |
| 1819 |
let rw = Box::into_raw(boxed) as *mut RelativePath; |
| 1820 |
unsafe { Box::from_raw(rw) } |
| 1821 |
} |
| 1822 |
} |
| 1823 |
|
| 1824 |
/// Conversion from a [`str`] reference to a [`Box<RelativePath>`]. |
| 1825 |
/// |
| 1826 |
/// [`str`]: prim@str |
| 1827 |
/// |
| 1828 |
/// # Examples |
| 1829 |
/// |
| 1830 |
/// ``` |
| 1831 |
/// use relative_path::RelativePath; |
| 1832 |
/// |
| 1833 |
/// let path: Box<RelativePath> = "foo/bar".into(); |
| 1834 |
/// assert_eq!(&*path, "foo/bar"); |
| 1835 |
/// |
| 1836 |
/// let path: Box<RelativePath> = RelativePath::new("foo/bar").into(); |
| 1837 |
/// assert_eq!(&*path, "foo/bar"); |
| 1838 |
/// ``` |
| 1839 |
impl<T> From<&T> for Box<RelativePath> |
| 1840 |
where |
| 1841 |
T: ?Sized + AsRef<str>, |
| 1842 |
{ |
| 1843 |
#[inline] |
| 1844 |
fn from(path: &T) -> Box<RelativePath> { |
| 1845 |
Box::<RelativePath>::from(Box::<str>::from(path.as_ref())) |
| 1846 |
} |
| 1847 |
} |
| 1848 |
|
| 1849 |
/// Conversion from [`RelativePathBuf`] to [`Box<RelativePath>`]. |
| 1850 |
/// |
| 1851 |
/// # Examples |
| 1852 |
/// |
| 1853 |
/// ``` |
| 1854 |
/// use std::sync::Arc; |
| 1855 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 1856 |
/// |
| 1857 |
/// let path = RelativePathBuf::from("foo/bar"); |
| 1858 |
/// let path: Box<RelativePath> = path.into(); |
| 1859 |
/// assert_eq!(&*path, "foo/bar"); |
| 1860 |
/// ``` |
| 1861 |
impl From<RelativePathBuf> for Box<RelativePath> { |
| 1862 |
#[inline] |
| 1863 |
fn from(path: RelativePathBuf) -> Box<RelativePath> { |
| 1864 |
let boxed: Box<str> = path.inner.into(); |
| 1865 |
let rw = Box::into_raw(boxed) as *mut RelativePath; |
| 1866 |
unsafe { Box::from_raw(rw) } |
| 1867 |
} |
| 1868 |
} |
| 1869 |
|
| 1870 |
/// Clone implementation for [`Box<RelativePath>`]. |
| 1871 |
/// |
| 1872 |
/// # Examples |
| 1873 |
/// |
| 1874 |
/// ``` |
| 1875 |
/// use relative_path::RelativePath; |
| 1876 |
/// |
| 1877 |
/// let path: Box<RelativePath> = RelativePath::new("foo/bar").into(); |
| 1878 |
/// let path2 = path.clone(); |
| 1879 |
/// assert_eq!(&*path, &*path2); |
| 1880 |
/// ``` |
| 1881 |
impl Clone for Box<RelativePath> { |
| 1882 |
#[inline] |
| 1883 |
fn clone(&self) -> Self { |
| 1884 |
self.to_relative_path_buf().into_boxed_relative_path() |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
/// Conversion from [`RelativePath`] to [`Arc<RelativePath>`]. |
| 1889 |
/// |
| 1890 |
/// # Examples |
| 1891 |
/// |
| 1892 |
/// ``` |
| 1893 |
/// use std::sync::Arc; |
| 1894 |
/// use relative_path::RelativePath; |
| 1895 |
/// |
| 1896 |
/// let path: Arc<RelativePath> = RelativePath::new("foo/bar").into(); |
| 1897 |
/// assert_eq!(&*path, "foo/bar"); |
| 1898 |
/// ``` |
| 1899 |
impl From<&RelativePath> for Arc<RelativePath> { |
| 1900 |
#[inline] |
| 1901 |
fn from(path: &RelativePath) -> Arc<RelativePath> { |
| 1902 |
let arc: Arc<str> = path.inner.into(); |
| 1903 |
let rw = Arc::into_raw(arc) as *const RelativePath; |
| 1904 |
unsafe { Arc::from_raw(rw) } |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
/// Conversion from [`RelativePathBuf`] to [`Arc<RelativePath>`]. |
| 1909 |
/// |
| 1910 |
/// # Examples |
| 1911 |
/// |
| 1912 |
/// ``` |
| 1913 |
/// use std::sync::Arc; |
| 1914 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 1915 |
/// |
| 1916 |
/// let path = RelativePathBuf::from("foo/bar"); |
| 1917 |
/// let path: Arc<RelativePath> = path.into(); |
| 1918 |
/// assert_eq!(&*path, "foo/bar"); |
| 1919 |
/// ``` |
| 1920 |
impl From<RelativePathBuf> for Arc<RelativePath> { |
| 1921 |
#[inline] |
| 1922 |
fn from(path: RelativePathBuf) -> Arc<RelativePath> { |
| 1923 |
let arc: Arc<str> = path.inner.into(); |
| 1924 |
let rw = Arc::into_raw(arc) as *const RelativePath; |
| 1925 |
unsafe { Arc::from_raw(rw) } |
| 1926 |
} |
| 1927 |
} |
| 1928 |
|
| 1929 |
/// Conversion from [`RelativePathBuf`] to [`Arc<RelativePath>`]. |
| 1930 |
/// |
| 1931 |
/// # Examples |
| 1932 |
/// |
| 1933 |
/// ``` |
| 1934 |
/// use std::rc::Rc; |
| 1935 |
/// use relative_path::RelativePath; |
| 1936 |
/// |
| 1937 |
/// let path: Rc<RelativePath> = RelativePath::new("foo/bar").into(); |
| 1938 |
/// assert_eq!(&*path, "foo/bar"); |
| 1939 |
/// ``` |
| 1940 |
impl From<&RelativePath> for Rc<RelativePath> { |
| 1941 |
#[inline] |
| 1942 |
fn from(path: &RelativePath) -> Rc<RelativePath> { |
| 1943 |
let rc: Rc<str> = path.inner.into(); |
| 1944 |
let rw = Rc::into_raw(rc) as *const RelativePath; |
| 1945 |
unsafe { Rc::from_raw(rw) } |
| 1946 |
} |
| 1947 |
} |
| 1948 |
|
| 1949 |
/// Conversion from [`RelativePathBuf`] to [`Rc<RelativePath>`]. |
| 1950 |
/// |
| 1951 |
/// # Examples |
| 1952 |
/// |
| 1953 |
/// ``` |
| 1954 |
/// use std::rc::Rc; |
| 1955 |
/// use relative_path::{RelativePath, RelativePathBuf}; |
| 1956 |
/// |
| 1957 |
/// let path = RelativePathBuf::from("foo/bar"); |
| 1958 |
/// let path: Rc<RelativePath> = path.into(); |
| 1959 |
/// assert_eq!(&*path, "foo/bar"); |
| 1960 |
/// ``` |
| 1961 |
impl From<RelativePathBuf> for Rc<RelativePath> { |
| 1962 |
#[inline] |
| 1963 |
fn from(path: RelativePathBuf) -> Rc<RelativePath> { |
| 1964 |
let rc: Rc<str> = path.inner.into(); |
| 1965 |
let rw = Rc::into_raw(rc) as *const RelativePath; |
| 1966 |
unsafe { Rc::from_raw(rw) } |
| 1967 |
} |
| 1968 |
} |
| 1969 |
|
| 1970 |
/// [`ToOwned`] implementation for [`RelativePath`]. |
| 1971 |
/// |
| 1972 |
/// # Examples |
| 1973 |
/// |
| 1974 |
/// ``` |
| 1975 |
/// use relative_path::RelativePath; |
| 1976 |
/// |
| 1977 |
/// let path = RelativePath::new("foo/bar").to_owned(); |
| 1978 |
/// assert_eq!(path, "foo/bar"); |
| 1979 |
/// ``` |
| 1980 |
impl ToOwned for RelativePath { |
| 1981 |
type Owned = RelativePathBuf; |
| 1982 |
|
| 1983 |
#[inline] |
| 1984 |
fn to_owned(&self) -> RelativePathBuf { |
| 1985 |
self.to_relative_path_buf() |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
impl fmt::Debug for RelativePath { |
| 1990 |
#[inline] |
| 1991 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 1992 |
write!(fmt, "{:?}", &self.inner) |
| 1993 |
} |
| 1994 |
} |
| 1995 |
|
| 1996 |
/// [`AsRef<str>`] implementation for [`RelativePathBuf`]. |
| 1997 |
/// |
| 1998 |
/// # Examples |
| 1999 |
/// |
| 2000 |
/// ``` |
| 2001 |
/// use relative_path::RelativePathBuf; |
| 2002 |
/// |
| 2003 |
/// let path = RelativePathBuf::from("foo/bar"); |
| 2004 |
/// let string: &str = path.as_ref(); |
| 2005 |
/// assert_eq!(string, "foo/bar"); |
| 2006 |
/// ``` |
| 2007 |
impl AsRef<str> for RelativePathBuf { |
| 2008 |
#[inline] |
| 2009 |
fn as_ref(&self) -> &str { |
| 2010 |
&self.inner |
| 2011 |
} |
| 2012 |
} |
| 2013 |
|
| 2014 |
/// [`AsRef<RelativePath>`] implementation for [String]. |
| 2015 |
/// |
| 2016 |
/// # Examples |
| 2017 |
/// |
| 2018 |
/// ``` |
| 2019 |
/// use relative_path::RelativePath; |
| 2020 |
/// |
| 2021 |
/// let path: String = format!("foo/bar"); |
| 2022 |
/// let path: &RelativePath = path.as_ref(); |
| 2023 |
/// assert_eq!(path, "foo/bar"); |
| 2024 |
/// ``` |
| 2025 |
impl AsRef<RelativePath> for String { |
| 2026 |
#[inline] |
| 2027 |
fn as_ref(&self) -> &RelativePath { |
| 2028 |
RelativePath::new(self) |
| 2029 |
} |
| 2030 |
} |
| 2031 |
|
| 2032 |
/// [`AsRef<RelativePath>`] implementation for [`str`]. |
| 2033 |
/// |
| 2034 |
/// [`str`]: prim@str |
| 2035 |
/// |
| 2036 |
/// # Examples |
| 2037 |
/// |
| 2038 |
/// ``` |
| 2039 |
/// use relative_path::RelativePath; |
| 2040 |
/// |
| 2041 |
/// let path: &RelativePath = "foo/bar".as_ref(); |
| 2042 |
/// assert_eq!(path, RelativePath::new("foo/bar")); |
| 2043 |
/// ``` |
| 2044 |
impl AsRef<RelativePath> for str { |
| 2045 |
#[inline] |
| 2046 |
fn as_ref(&self) -> &RelativePath { |
| 2047 |
RelativePath::new(self) |
| 2048 |
} |
| 2049 |
} |
| 2050 |
|
| 2051 |
impl AsRef<RelativePath> for RelativePath { |
| 2052 |
#[inline] |
| 2053 |
fn as_ref(&self) -> &RelativePath { |
| 2054 |
self |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
impl cmp::PartialEq for RelativePath { |
| 2059 |
#[inline] |
| 2060 |
fn eq(&self, other: &RelativePath) -> bool { |
| 2061 |
self.components() == other.components() |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
impl cmp::Eq for RelativePath {} |
| 2066 |
|
| 2067 |
impl cmp::PartialOrd for RelativePath { |
| 2068 |
#[inline] |
| 2069 |
fn partial_cmp(&self, other: &RelativePath) -> Option<cmp::Ordering> { |
| 2070 |
Some(self.cmp(other)) |
| 2071 |
} |
| 2072 |
} |
| 2073 |
|
| 2074 |
impl cmp::Ord for RelativePath { |
| 2075 |
#[inline] |
| 2076 |
fn cmp(&self, other: &RelativePath) -> cmp::Ordering { |
| 2077 |
self.components().cmp(other.components()) |
| 2078 |
} |
| 2079 |
} |
| 2080 |
|
| 2081 |
impl Hash for RelativePath { |
| 2082 |
#[inline] |
| 2083 |
fn hash<H: Hasher>(&self, h: &mut H) { |
| 2084 |
for c in self.components() { |
| 2085 |
c.hash(h); |
| 2086 |
} |
| 2087 |
} |
| 2088 |
} |
| 2089 |
|
| 2090 |
impl fmt::Display for RelativePath { |
| 2091 |
#[inline] |
| 2092 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 2093 |
fmt::Display::fmt(&self.inner, f) |
| 2094 |
} |
| 2095 |
} |
| 2096 |
|
| 2097 |
impl fmt::Display for RelativePathBuf { |
| 2098 |
#[inline] |
| 2099 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 2100 |
fmt::Display::fmt(&self.inner, f) |
| 2101 |
} |
| 2102 |
} |
| 2103 |
|
| 2104 |
/// Helper struct for printing relative paths. |
| 2105 |
/// |
| 2106 |
/// This is not strictly necessary in the same sense as it is for [`Display`], |
| 2107 |
/// because relative paths are guaranteed to be valid UTF-8. But the behavior is |
| 2108 |
/// preserved to simplify the transition between [`Path`] and [`RelativePath`]. |
| 2109 |
/// |
| 2110 |
/// [`Path`]: std::path::Path |
| 2111 |
/// [`Display`]: std::fmt::Display |
| 2112 |
pub struct Display<'a> { |
| 2113 |
path: &'a RelativePath, |
| 2114 |
} |
| 2115 |
|
| 2116 |
impl<'a> fmt::Debug for Display<'a> { |
| 2117 |
#[inline] |
| 2118 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 2119 |
fmt::Debug::fmt(&self.path, f) |
| 2120 |
} |
| 2121 |
} |
| 2122 |
|
| 2123 |
impl<'a> fmt::Display for Display<'a> { |
| 2124 |
#[inline] |
| 2125 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 2126 |
fmt::Display::fmt(&self.path, f) |
| 2127 |
} |
| 2128 |
} |
| 2129 |
|
| 2130 |
/// [`serde::ser::Serialize`] implementation for [`RelativePathBuf`]. |
| 2131 |
/// |
| 2132 |
/// ``` |
| 2133 |
/// use serde::Serialize; |
| 2134 |
/// use relative_path::RelativePathBuf; |
| 2135 |
/// |
| 2136 |
/// #[derive(Serialize)] |
| 2137 |
/// struct Document { |
| 2138 |
/// path: RelativePathBuf, |
| 2139 |
/// } |
| 2140 |
/// ``` |
| 2141 |
#[cfg(feature = "serde")] |
| 2142 |
impl serde::ser::Serialize for RelativePathBuf { |
| 2143 |
#[inline] |
| 2144 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 2145 |
where |
| 2146 |
S: serde::ser::Serializer, |
| 2147 |
{ |
| 2148 |
serializer.serialize_str(&self.inner) |
| 2149 |
} |
| 2150 |
} |
| 2151 |
|
| 2152 |
/// [`serde::de::Deserialize`] implementation for [`RelativePathBuf`]. |
| 2153 |
/// |
| 2154 |
/// ``` |
| 2155 |
/// use serde::Deserialize; |
| 2156 |
/// use relative_path::RelativePathBuf; |
| 2157 |
/// |
| 2158 |
/// #[derive(Deserialize)] |
| 2159 |
/// struct Document { |
| 2160 |
/// path: RelativePathBuf, |
| 2161 |
/// } |
| 2162 |
/// ``` |
| 2163 |
#[cfg(feature = "serde")] |
| 2164 |
impl<'de> serde::de::Deserialize<'de> for RelativePathBuf { |
| 2165 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2166 |
where |
| 2167 |
D: serde::de::Deserializer<'de>, |
| 2168 |
{ |
| 2169 |
struct Visitor; |
| 2170 |
|
| 2171 |
impl<'de> serde::de::Visitor<'de> for Visitor { |
| 2172 |
type Value = RelativePathBuf; |
| 2173 |
|
| 2174 |
#[inline] |
| 2175 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2176 |
formatter.write_str("a relative path") |
| 2177 |
} |
| 2178 |
|
| 2179 |
#[inline] |
| 2180 |
fn visit_string<E>(self, input: String) -> Result<Self::Value, E> |
| 2181 |
where |
| 2182 |
E: serde::de::Error, |
| 2183 |
{ |
| 2184 |
Ok(RelativePathBuf::from(input)) |
| 2185 |
} |
| 2186 |
|
| 2187 |
#[inline] |
| 2188 |
fn visit_str<E>(self, input: &str) -> Result<Self::Value, E> |
| 2189 |
where |
| 2190 |
E: serde::de::Error, |
| 2191 |
{ |
| 2192 |
Ok(RelativePathBuf::from(input.to_owned())) |
| 2193 |
} |
| 2194 |
} |
| 2195 |
|
| 2196 |
deserializer.deserialize_str(Visitor) |
| 2197 |
} |
| 2198 |
} |
| 2199 |
|
| 2200 |
/// [`serde::de::Deserialize`] implementation for [`Box<RelativePath>`]. |
| 2201 |
/// |
| 2202 |
/// ``` |
| 2203 |
/// use serde::Deserialize; |
| 2204 |
/// use relative_path::RelativePath; |
| 2205 |
/// |
| 2206 |
/// #[derive(Deserialize)] |
| 2207 |
/// struct Document { |
| 2208 |
/// path: Box<RelativePath>, |
| 2209 |
/// } |
| 2210 |
/// ``` |
| 2211 |
#[cfg(feature = "serde")] |
| 2212 |
impl<'de> serde::de::Deserialize<'de> for Box<RelativePath> { |
| 2213 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2214 |
where |
| 2215 |
D: serde::de::Deserializer<'de>, |
| 2216 |
{ |
| 2217 |
struct Visitor; |
| 2218 |
|
| 2219 |
impl<'de> serde::de::Visitor<'de> for Visitor { |
| 2220 |
type Value = Box<RelativePath>; |
| 2221 |
|
| 2222 |
#[inline] |
| 2223 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2224 |
formatter.write_str("a relative path") |
| 2225 |
} |
| 2226 |
|
| 2227 |
#[inline] |
| 2228 |
fn visit_string<E>(self, input: String) -> Result<Self::Value, E> |
| 2229 |
where |
| 2230 |
E: serde::de::Error, |
| 2231 |
{ |
| 2232 |
Ok(Box::<RelativePath>::from(input.into_boxed_str())) |
| 2233 |
} |
| 2234 |
|
| 2235 |
#[inline] |
| 2236 |
fn visit_str<E>(self, input: &str) -> Result<Self::Value, E> |
| 2237 |
where |
| 2238 |
E: serde::de::Error, |
| 2239 |
{ |
| 2240 |
Ok(Box::<RelativePath>::from(input)) |
| 2241 |
} |
| 2242 |
} |
| 2243 |
|
| 2244 |
deserializer.deserialize_str(Visitor) |
| 2245 |
} |
| 2246 |
} |
| 2247 |
|
| 2248 |
/// [`serde::de::Deserialize`] implementation for a [`RelativePath`] reference. |
| 2249 |
/// |
| 2250 |
/// ``` |
| 2251 |
/// use serde::Deserialize; |
| 2252 |
/// use relative_path::RelativePath; |
| 2253 |
/// |
| 2254 |
/// #[derive(Deserialize)] |
| 2255 |
/// struct Document<'a> { |
| 2256 |
/// #[serde(borrow)] |
| 2257 |
/// path: &'a RelativePath, |
| 2258 |
/// } |
| 2259 |
/// ``` |
| 2260 |
#[cfg(feature = "serde")] |
| 2261 |
impl<'de: 'a, 'a> serde::de::Deserialize<'de> for &'a RelativePath { |
| 2262 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 2263 |
where |
| 2264 |
D: serde::de::Deserializer<'de>, |
| 2265 |
{ |
| 2266 |
struct Visitor; |
| 2267 |
|
| 2268 |
impl<'a> serde::de::Visitor<'a> for Visitor { |
| 2269 |
type Value = &'a RelativePath; |
| 2270 |
|
| 2271 |
#[inline] |
| 2272 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 2273 |
formatter.write_str("a borrowed relative path") |
| 2274 |
} |
| 2275 |
|
| 2276 |
#[inline] |
| 2277 |
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> |
| 2278 |
where |
| 2279 |
E: serde::de::Error, |
| 2280 |
{ |
| 2281 |
Ok(RelativePath::new(v)) |
| 2282 |
} |
| 2283 |
|
| 2284 |
#[inline] |
| 2285 |
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> |
| 2286 |
where |
| 2287 |
E: serde::de::Error, |
| 2288 |
{ |
| 2289 |
let string = str::from_utf8(v).map_err(|_| { |
| 2290 |
serde::de::Error::invalid_value(serde::de::Unexpected::Bytes(v), &self) |
| 2291 |
})?; |
| 2292 |
Ok(RelativePath::new(string)) |
| 2293 |
} |
| 2294 |
} |
| 2295 |
|
| 2296 |
deserializer.deserialize_str(Visitor) |
| 2297 |
} |
| 2298 |
} |
| 2299 |
|
| 2300 |
/// [`serde::ser::Serialize`] implementation for [`RelativePath`]. |
| 2301 |
/// |
| 2302 |
/// ``` |
| 2303 |
/// use serde::Serialize; |
| 2304 |
/// use relative_path::RelativePath; |
| 2305 |
/// |
| 2306 |
/// #[derive(Serialize)] |
| 2307 |
/// struct Document<'a> { |
| 2308 |
/// path: &'a RelativePath, |
| 2309 |
/// } |
| 2310 |
/// ``` |
| 2311 |
#[cfg(feature = "serde")] |
| 2312 |
impl serde::ser::Serialize for RelativePath { |
| 2313 |
#[inline] |
| 2314 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 2315 |
where |
| 2316 |
S: serde::ser::Serializer, |
| 2317 |
{ |
| 2318 |
serializer.serialize_str(&self.inner) |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
macro_rules! impl_cmp { |
| 2323 |
($lhs:ty, $rhs:ty) => { |
| 2324 |
impl<'a, 'b> PartialEq<$rhs> for $lhs { |
| 2325 |
#[inline] |
| 2326 |
fn eq(&self, other: &$rhs) -> bool { |
| 2327 |
<RelativePath as PartialEq>::eq(self, other) |
| 2328 |
} |
| 2329 |
} |
| 2330 |
|
| 2331 |
impl<'a, 'b> PartialEq<$lhs> for $rhs { |
| 2332 |
#[inline] |
| 2333 |
fn eq(&self, other: &$lhs) -> bool { |
| 2334 |
<RelativePath as PartialEq>::eq(self, other) |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
impl<'a, 'b> PartialOrd<$rhs> for $lhs { |
| 2339 |
#[inline] |
| 2340 |
fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> { |
| 2341 |
<RelativePath as PartialOrd>::partial_cmp(self, other) |
| 2342 |
} |
| 2343 |
} |
| 2344 |
|
| 2345 |
impl<'a, 'b> PartialOrd<$lhs> for $rhs { |
| 2346 |
#[inline] |
| 2347 |
fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> { |
| 2348 |
<RelativePath as PartialOrd>::partial_cmp(self, other) |
| 2349 |
} |
| 2350 |
} |
| 2351 |
}; |
| 2352 |
} |
| 2353 |
|
| 2354 |
impl_cmp!(RelativePathBuf, RelativePath); |
| 2355 |
impl_cmp!(RelativePathBuf, &'a RelativePath); |
| 2356 |
impl_cmp!(Cow<'a, RelativePath>, RelativePath); |
| 2357 |
impl_cmp!(Cow<'a, RelativePath>, &'b RelativePath); |
| 2358 |
impl_cmp!(Cow<'a, RelativePath>, RelativePathBuf); |
| 2359 |
|
| 2360 |
macro_rules! impl_cmp_str { |
| 2361 |
($lhs:ty, $rhs:ty) => { |
| 2362 |
impl<'a, 'b> PartialEq<$rhs> for $lhs { |
| 2363 |
#[inline] |
| 2364 |
fn eq(&self, other: &$rhs) -> bool { |
| 2365 |
<RelativePath as PartialEq>::eq(self, other.as_ref()) |
| 2366 |
} |
| 2367 |
} |
| 2368 |
|
| 2369 |
impl<'a, 'b> PartialEq<$lhs> for $rhs { |
| 2370 |
#[inline] |
| 2371 |
fn eq(&self, other: &$lhs) -> bool { |
| 2372 |
<RelativePath as PartialEq>::eq(self.as_ref(), other) |
| 2373 |
} |
| 2374 |
} |
| 2375 |
|
| 2376 |
impl<'a, 'b> PartialOrd<$rhs> for $lhs { |
| 2377 |
#[inline] |
| 2378 |
fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> { |
| 2379 |
<RelativePath as PartialOrd>::partial_cmp(self, other.as_ref()) |
| 2380 |
} |
| 2381 |
} |
| 2382 |
|
| 2383 |
impl<'a, 'b> PartialOrd<$lhs> for $rhs { |
| 2384 |
#[inline] |
| 2385 |
fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> { |
| 2386 |
<RelativePath as PartialOrd>::partial_cmp(self.as_ref(), other) |
| 2387 |
} |
| 2388 |
} |
| 2389 |
}; |
| 2390 |
} |
| 2391 |
|
| 2392 |
impl_cmp_str!(RelativePathBuf, str); |
| 2393 |
impl_cmp_str!(RelativePathBuf, &'a str); |
| 2394 |
impl_cmp_str!(RelativePathBuf, String); |
| 2395 |
impl_cmp_str!(RelativePath, str); |
| 2396 |
impl_cmp_str!(RelativePath, &'a str); |
| 2397 |
impl_cmp_str!(RelativePath, String); |
| 2398 |
impl_cmp_str!(&'a RelativePath, str); |
| 2399 |
impl_cmp_str!(&'a RelativePath, String); |