Files
rust/crates/ra_db/src/mock.rs

31 lines
833 B
Rust
Raw Normal View History

2018-11-28 16:19:01 +03:00
use rustc_hash::FxHashSet;
use relative_path::{RelativePath, RelativePathBuf};
2018-12-19 12:26:51 +03:00
use crate::{FileId};
2018-11-28 16:19:01 +03:00
2018-12-09 13:49:54 +03:00
#[derive(Default, Debug, Clone)]
2018-11-28 16:19:01 +03:00
pub struct FileMap(Vec<(FileId, RelativePathBuf)>);
impl FileMap {
pub fn add(&mut self, path: RelativePathBuf) -> FileId {
let file_id = FileId((self.0.len() + 1) as u32);
self.0.push((file_id, path));
file_id
}
pub fn files(&self) -> FxHashSet<FileId> {
self.iter().map(|(id, _)| id).collect()
}
2018-12-09 13:49:54 +03:00
pub fn file_id(&self, path: &str) -> FileId {
assert!(path.starts_with('/'));
self.iter().find(|(_, p)| p == &path[1..]).unwrap().0
}
2018-11-28 16:19:01 +03:00
fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a {
self.0
.iter()
.map(|(id, path)| (*id, path.as_relative_path()))
}
}