rust/crates/vfs/src/anchored_path.rs

50 lines
1.8 KiB
Rust
Raw Normal View History

2020-12-09 09:41:35 -06:00
//! Analysis-level representation of file-system paths.
//!
//! The primary goal of this is to losslessly represent paths like
//!
//! ```
//! #[path = "./bar.rs"]
//! mod foo;
//! ```
//!
//! The first approach one might reach for is to use `PathBuf`. The problem here
//! is that `PathBuf` depends on host target (windows or linux), but
//! rust-analyzer should be capable to process `#[path = r"C:\bar.rs"]` on Unix.
//!
//! The second try is to use a `String`. This also fails, however. Consider a
//! hypothetical scenario, where rust-analyzer operates in a
//! networked/distributed mode. There's one global instance of rust-analyzer,
//! which processes requests from different machines. Now, the semantics of
//! `#[path = "/abs/path.rs"]` actually depends on which file-system we are at!
//! That is, even absolute paths exist relative to a file system!
//!
//! A more realistic scenario here is virtual VFS paths we use for testing. More
//! generally, there can be separate "universes" of VFS paths.
//!
//! That's why we use anchored representation -- each path carries an info about
//! a file this path originates from. We can fetch fs/"universe" information
//! from the anchor than.
use crate::FileId;
2021-01-12 10:22:57 -06:00
/// Path relative to a file.
///
/// Owned version of [`AnchoredPath`].
2020-12-09 09:41:35 -06:00
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AnchoredPathBuf {
2021-01-12 10:22:57 -06:00
/// File that this path is relative to.
2020-12-09 09:41:35 -06:00
pub anchor: FileId,
2021-01-12 10:22:57 -06:00
/// Path relative to `anchor`'s containing directory.
2020-12-09 09:41:35 -06:00
pub path: String,
}
2021-01-12 10:22:57 -06:00
/// Path relative to a file.
///
/// Borrowed version of [`AnchoredPathBuf`].
2020-12-09 09:41:35 -06:00
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AnchoredPath<'a> {
2021-01-12 10:22:57 -06:00
/// File that this path is relative to.
2020-12-09 09:41:35 -06:00
pub anchor: FileId,
2021-01-12 10:22:57 -06:00
/// Path relative to `anchor`'s containing directory.
2020-12-09 09:41:35 -06:00
pub path: &'a str,
}