use std::collections::HashMap;
use std::io;
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use rustc_span::source_map::{FileLoader, RealFileLoader};
pub(crate) const ENV: &str = "RIDX_BUFFERS";
pub(crate) struct BufferLoader {
buffers: HashMap<PathBuf, PathBuf>,
}
impl BufferLoader {
pub(crate) fn from_list(list: &Path) -> Option<Self> {
let map: HashMap<String, String> = serde_json::from_slice(&std::fs::read(list).ok()?).ok()?;
Some(Self::new(map.into_iter().map(|(src, text)| (PathBuf::from(src), PathBuf::from(text)))))
}
pub(crate) fn new(buffers: impl IntoIterator<Item = (PathBuf, PathBuf)>) -> Self {
Self { buffers: buffers.into_iter().map(|(src, text)| (canonical(&src), text)).collect() }
}
fn buffer_of(&self, path: &Path) -> Option<&Path> {
if self.buffers.is_empty() {
return None;
}
self.buffers.get(&canonical(path)).map(PathBuf::as_path)
}
}
fn canonical(path: &Path) -> PathBuf {
let absolute = if path.is_absolute() { path.to_path_buf() } else { std::env::current_dir().map(|d| d.join(path)).unwrap_or_else(|_| path.to_path_buf()) };
if let Ok(c) = absolute.canonicalize() {
return c;
}
let mut out = PathBuf::new();
let mut missing = 0usize;
for component in absolute.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
out.pop();
missing = missing.saturating_sub(1);
}
other => {
out.push(other);
if missing > 0 {
missing += 1;
} else {
match out.canonicalize() {
Ok(c) => out = c,
Err(_) => missing = 1,
}
}
}
}
}
out
}
impl FileLoader for BufferLoader {
fn file_exists(&self, path: &Path) -> bool {
self.buffer_of(path).is_some() || RealFileLoader.file_exists(path)
}
fn read_file(&self, path: &Path) -> io::Result<String> {
match self.buffer_of(path) {
Some(text) => std::fs::read_to_string(text),
None => RealFileLoader.read_file(path),
}
}
fn read_binary_file(&self, path: &Path) -> io::Result<Arc<[u8]>> {
match self.buffer_of(path) {
Some(text) => std::fs::read(text).map(Into::into),
None => RealFileLoader.read_binary_file(path),
}
}
fn current_directory(&self) -> io::Result<PathBuf> {
RealFileLoader.current_directory()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_a_buffer_not_on_disk_is_found_by_a_relative_path() {
let dir = std::env::current_dir().unwrap().join(format!("ridx_buffers_test_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let text = dir.join("text.rs");
std::fs::write(&text, "pub fn new() {}\n").unwrap();
let loader = BufferLoader::new([(dir.join("src/new.rs"), text)]);
let relative = PathBuf::from(dir.file_name().unwrap()).join("src/./new.rs");
assert!(loader.file_exists(&relative), "a typed, unsaved file asked for relative to the working directory");
assert_eq!(loader.read_file(&relative).unwrap(), "pub fn new() {}\n");
assert!(!loader.file_exists(&dir.join("src/other.rs")));
assert!(loader.file_exists(&dir.join("missing/../src/new.rs")), "a `..` after a directory that does not exist");
let _ = std::fs::remove_dir_all(&dir);
}
#[cfg(unix)]
#[test]
fn test_a_link_after_a_missing_directory_is_followed() {
let dir = std::env::temp_dir().join(format!("ridx_buffers_link_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("workspace")).unwrap();
std::fs::create_dir_all(dir.join("external/nested")).unwrap();
std::os::unix::fs::symlink(dir.join("external/nested"), dir.join("workspace/shortcut")).unwrap();
let (local, external) = (dir.join("local.txt"), dir.join("external.txt"));
std::fs::write(&local, "LOCAL").unwrap();
std::fs::write(&external, "EXTERNAL").unwrap();
let loader = BufferLoader::new([(dir.join("workspace/new.rs"), local), (dir.join("external/new.rs"), external)]);
assert_eq!(loader.read_file(&dir.join("workspace/missing/../shortcut/../new.rs")).unwrap(), "EXTERNAL");
let _ = std::fs::remove_dir_all(&dir);
}
}