vfs/anchored_path.rs
1//! Analysis-level representation of file-system paths.
2//!
3//! The primary goal of this is to losslessly represent paths like
4//!
5//! ```ignore
6//! #[path = "./bar.rs"]
7//! mod foo;
8//! ```
9//!
10//! The first approach one might reach for is to use `PathBuf`. The problem here
11//! is that `PathBuf` depends on host target (windows or linux), but
12//! rust-analyzer should be capable to process `#[path = r"C:\bar.rs"]` on Unix.
13//!
14//! The second try is to use a `String`. This also fails, however. Consider a
15//! hypothetical scenario, where rust-analyzer operates in a
16//! networked/distributed mode. There's one global instance of rust-analyzer,
17//! which processes requests from different machines. Now, the semantics of
18//! `#[path = "/abs/path.rs"]` actually depends on which file-system we are at!
19//! That is, even absolute paths exist relative to a file system!
20//!
21//! A more realistic scenario here is virtual VFS paths we use for testing. More
22//! generally, there can be separate "universes" of VFS paths.
23//!
24//! That's why we use anchored representation -- each path carries an info about
25//! a file this path originates from. We can fetch fs/"universe" information
26//! from the anchor than.
27use crate::FileId;
28
29/// Path relative to a file.
30///
31/// Owned version of [`AnchoredPath`].
32#[derive(Clone, PartialEq, Eq, Debug)]
33pub struct AnchoredPathBuf {
34 /// File that this path is relative to.
35 pub anchor: FileId,
36 /// Path relative to `anchor`'s containing directory.
37 pub path: String,
38}
39
40/// Path relative to a file.
41///
42/// Borrowed version of [`AnchoredPathBuf`].
43#[derive(Clone, Copy, PartialEq, Eq, Debug)]
44pub struct AnchoredPath<'a> {
45 /// File that this path is relative to.
46 pub anchor: FileId,
47 /// Path relative to `anchor`'s containing directory.
48 pub path: &'a str,
49}