| 1 |
# relative-path |
| 2 |
|
| 3 |
[<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) |
| 4 |
[<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) |
| 5 |
[<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) |
| 6 |
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/udoprog/relative-path/ci.yml?branch=main&style=for-the-badge" height="20">](https://github.com/udoprog/relative-path/actions?query=branch%3Amain) |
| 7 |
|
| 8 |
Portable relative UTF-8 paths for Rust. |
| 9 |
|
| 10 |
This crate provides a module analogous to [`std::path`], with the following |
| 11 |
characteristics: |
| 12 |
|
| 13 |
* The path separator is set to a fixed character (`/`), regardless of |
| 14 |
platform. |
| 15 |
* Relative paths cannot represent a path in the filesystem without first |
| 16 |
specifying *what they are relative to* using functions such as [`to_path`] |
| 17 |
and [`to_logical_path`]. |
| 18 |
* Relative paths are always guaranteed to be valid UTF-8 strings. |
| 19 |
|
| 20 |
On top of this we support many operations that guarantee the same behavior |
| 21 |
across platforms. |
| 22 |
|
| 23 |
For more utilities to manipulate relative paths, see the |
| 24 |
[`relative-path-utils` crate]. |
| 25 |
|
| 26 |
<br> |
| 27 |
|
| 28 |
## Usage |
| 29 |
|
| 30 |
Add `relative-path` to your `Cargo.toml`: |
| 31 |
|
| 32 |
```toml |
| 33 |
relative-path = "1.9.2" |
| 34 |
``` |
| 35 |
|
| 36 |
Start using relative paths: |
| 37 |
|
| 38 |
```rust |
| 39 |
use serde::{Serialize, Deserialize}; |
| 40 |
use relative_path::RelativePath; |
| 41 |
|
| 42 |
#[derive(Serialize, Deserialize)] |
| 43 |
struct Manifest<'a> { |
| 44 |
#[serde(borrow)] |
| 45 |
source: &'a RelativePath, |
| 46 |
} |
| 47 |
|
| 48 |
``` |
| 49 |
|
| 50 |
<br> |
| 51 |
|
| 52 |
## Serde Support |
| 53 |
|
| 54 |
This library includes serde support that can be enabled with the `serde` |
| 55 |
feature. |
| 56 |
|
| 57 |
<br> |
| 58 |
|
| 59 |
## Why is `std::path` a portability hazard? |
| 60 |
|
| 61 |
Path representations differ across platforms. |
| 62 |
|
| 63 |
* Windows permits using drive volumes (multiple roots) as a prefix (e.g. |
| 64 |
`"c:\"`) and backslash (`\`) as a separator. |
| 65 |
* Unix references absolute paths from a single root and uses forward slash |
| 66 |
(`/`) as a separator. |
| 67 |
|
| 68 |
If we use `PathBuf`, Storing paths in a manifest would allow our application |
| 69 |
to build and run on one platform but potentially not others. |
| 70 |
|
| 71 |
Consider the following data model and corresponding toml for a manifest: |
| 72 |
|
| 73 |
```rust |
| 74 |
use std::path::PathBuf; |
| 75 |
|
| 76 |
use serde::{Serialize, Deserialize}; |
| 77 |
|
| 78 |
#[derive(Serialize, Deserialize)] |
| 79 |
struct Manifest { |
| 80 |
source: PathBuf, |
| 81 |
} |
| 82 |
``` |
| 83 |
|
| 84 |
```toml |
| 85 |
source = "C:\\Users\\udoprog\\repo\\data\\source" |
| 86 |
``` |
| 87 |
|
| 88 |
This will run for you (assuming `source` exists). So you go ahead and check |
| 89 |
the manifest into git. The next day your Linux colleague calls you and |
| 90 |
wonders what they have ever done to wrong you? |
| 91 |
|
| 92 |
So what went wrong? Well two things. You forgot to make the `source` |
| 93 |
relative, so anyone at the company which has a different username than you |
| 94 |
won't be able to use it. So you go ahead and fix that: |
| 95 |
|
| 96 |
```toml |
| 97 |
source = "data\\source" |
| 98 |
``` |
| 99 |
|
| 100 |
But there is still one problem! A backslash (`\`) is only a legal path |
| 101 |
separator on Windows. Luckily you learn that forward slashes are supported |
| 102 |
both on Windows *and* Linux. So you opt for: |
| 103 |
|
| 104 |
```toml |
| 105 |
source = "data/source" |
| 106 |
``` |
| 107 |
|
| 108 |
Things are working now. So all is well... Right? Sure, but we can do better. |
| 109 |
|
| 110 |
This crate provides types that work with *portable relative paths* (hence |
| 111 |
the name). So by using [`RelativePath`] we can systematically help avoid |
| 112 |
portability issues like the one above. Avoiding issues at the source is |
| 113 |
preferably over spending 5 minutes of onboarding time on a theoretical |
| 114 |
problem, hoping that your new hires will remember what to do if they ever |
| 115 |
encounter it. |
| 116 |
|
| 117 |
Using [`RelativePathBuf`] we can fix our data model like this: |
| 118 |
|
| 119 |
```rust |
| 120 |
use relative_path::RelativePathBuf; |
| 121 |
use serde::{Serialize, Deserialize}; |
| 122 |
|
| 123 |
#[derive(Serialize, Deserialize)] |
| 124 |
pub struct Manifest { |
| 125 |
source: RelativePathBuf, |
| 126 |
} |
| 127 |
``` |
| 128 |
|
| 129 |
And where it's used: |
| 130 |
|
| 131 |
```rust |
| 132 |
use std::fs; |
| 133 |
use std::env::current_dir; |
| 134 |
|
| 135 |
let manifest: Manifest = todo!(); |
| 136 |
|
| 137 |
let root = current_dir()?; |
| 138 |
let source = manifest.source.to_path(&root); |
| 139 |
let content = fs::read(&source)?; |
| 140 |
``` |
| 141 |
|
| 142 |
<br> |
| 143 |
|
| 144 |
## Overview |
| 145 |
|
| 146 |
Conversion to a platform-specific [`Path`] happens through the [`to_path`] |
| 147 |
and [`to_logical_path`] functions. Where you are required to specify the |
| 148 |
path that prefixes the relative path. This can come from a function such as |
| 149 |
[`std::env::current_dir`]. |
| 150 |
|
| 151 |
```rust |
| 152 |
use std::env::current_dir; |
| 153 |
use std::path::Path; |
| 154 |
|
| 155 |
use relative_path::RelativePath; |
| 156 |
|
| 157 |
let root = current_dir()?; |
| 158 |
|
| 159 |
// to_path unconditionally concatenates a relative path with its base: |
| 160 |
let relative_path = RelativePath::new("../foo/./bar"); |
| 161 |
let full_path = relative_path.to_path(&root); |
| 162 |
assert_eq!(full_path, root.join("..\\foo\\.\\bar")); |
| 163 |
|
| 164 |
// to_logical_path tries to apply the logical operations that the relative |
| 165 |
// path corresponds to: |
| 166 |
let relative_path = RelativePath::new("../foo/./bar"); |
| 167 |
let full_path = relative_path.to_logical_path(&root); |
| 168 |
|
| 169 |
// Replicate the operation performed by `to_logical_path`. |
| 170 |
let mut parent = root.clone(); |
| 171 |
parent.pop(); |
| 172 |
assert_eq!(full_path, parent.join("foo\\bar")); |
| 173 |
``` |
| 174 |
|
| 175 |
When two relative paths are compared to each other, their exact component |
| 176 |
makeup determines equality. |
| 177 |
|
| 178 |
```rust |
| 179 |
use relative_path::RelativePath; |
| 180 |
|
| 181 |
assert_ne!( |
| 182 |
RelativePath::new("foo/bar/../baz"), |
| 183 |
RelativePath::new("foo/baz") |
| 184 |
); |
| 185 |
``` |
| 186 |
|
| 187 |
Using platform-specific path separators to construct relative paths is not |
| 188 |
supported. |
| 189 |
|
| 190 |
Path separators from other platforms are simply treated as part of a |
| 191 |
component: |
| 192 |
|
| 193 |
```rust |
| 194 |
use relative_path::RelativePath; |
| 195 |
|
| 196 |
assert_ne!( |
| 197 |
RelativePath::new("foo/bar"), |
| 198 |
RelativePath::new("foo\\bar") |
| 199 |
); |
| 200 |
|
| 201 |
assert_eq!(1, RelativePath::new("foo\\bar").components().count()); |
| 202 |
assert_eq!(2, RelativePath::new("foo/bar").components().count()); |
| 203 |
``` |
| 204 |
|
| 205 |
To see if two relative paths are equivalent you can use [`normalize`]: |
| 206 |
|
| 207 |
```rust |
| 208 |
use relative_path::RelativePath; |
| 209 |
|
| 210 |
assert_eq!( |
| 211 |
RelativePath::new("foo/bar/../baz").normalize(), |
| 212 |
RelativePath::new("foo/baz").normalize(), |
| 213 |
); |
| 214 |
``` |
| 215 |
|
| 216 |
<br> |
| 217 |
|
| 218 |
## Additional portability notes |
| 219 |
|
| 220 |
While relative paths avoid the most egregious portability issue, that |
| 221 |
absolute paths will work equally unwell on all platforms. We cannot avoid |
| 222 |
all. This section tries to document additional portability hazards that we |
| 223 |
are aware of. |
| 224 |
|
| 225 |
[`RelativePath`], similarly to [`Path`], makes no guarantees that its |
| 226 |
constituent components make up legal file names. While components are |
| 227 |
strictly separated by slashes, we can still store things in them which may |
| 228 |
not be used as legal paths on all platforms. |
| 229 |
|
| 230 |
* A `NUL` character is not permitted on unix platforms - this is a |
| 231 |
terminator in C-based filesystem APIs. Slash (`/`) is also used as a path |
| 232 |
separator. |
| 233 |
* Windows has a number of [reserved characters and names][windows-reserved] |
| 234 |
(like `CON`, `PRN`, and `AUX`) which cannot legally be part of a |
| 235 |
filesystem component. |
| 236 |
* Windows paths are [case-insensitive by default][windows-case]. So, |
| 237 |
`Foo.txt` and `foo.txt` are the same files on windows. But they are |
| 238 |
considered different paths on most unix systems. |
| 239 |
|
| 240 |
A relative path that *accidentally* contains a platform-specific components |
| 241 |
will largely result in a nonsensical paths being generated in the hope that |
| 242 |
they will fail fast during development and testing. |
| 243 |
|
| 244 |
```rust |
| 245 |
use relative_path::{RelativePath, PathExt}; |
| 246 |
use std::path::Path; |
| 247 |
|
| 248 |
if cfg!(windows) { |
| 249 |
assert_eq!( |
| 250 |
Path::new("foo\\c:\\bar\\baz"), |
| 251 |
RelativePath::new("c:\\bar\\baz").to_path("foo") |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
if cfg!(unix) { |
| 256 |
assert_eq!( |
| 257 |
Path::new("foo/bar/baz"), |
| 258 |
RelativePath::new("/bar/baz").to_path("foo") |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
assert_eq!( |
| 263 |
Path::new("foo").relative_to("bar")?, |
| 264 |
RelativePath::new("../foo"), |
| 265 |
); |
| 266 |
``` |
| 267 |
|
| 268 |
[`None`]: https://doc.rust-lang.org/std/option/enum.Option.html |
| 269 |
[`normalize`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html#method.normalize |
| 270 |
[`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html |
| 271 |
[`RelativePath`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html |
| 272 |
[`RelativePathBuf`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePathBuf.html |
| 273 |
[`std::env::current_dir`]: https://doc.rust-lang.org/std/env/fn.current_dir.html |
| 274 |
[`std::path`]: https://doc.rust-lang.org/std/path/index.html |
| 275 |
[`to_logical_path`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html#method.to_logical_path |
| 276 |
[`to_path`]: https://docs.rs/relative-path/1/relative_path/struct.RelativePath.html#method.to_path |
| 277 |
[windows-reserved]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx |
| 278 |
[windows-case]: https://learn.microsoft.com/en-us/windows/wsl/case-sensitivity |
| 279 |
[`relative-path-utils` crate]: https://docs.rs/relative-path-utils |