aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/mock.rs
blob: 5e185062b450e53d03c9969bb374958cd97ed7ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use rustc_hash::FxHashSet;
use relative_path::{RelativePath, RelativePathBuf};

use crate::{FileId};

#[derive(Default, Debug, Clone)]
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()
    }

    pub fn file_id(&self, path: &str) -> FileId {
        assert!(path.starts_with('/'));
        self.iter().find(|(_, p)| p == &path[1..]).unwrap().0
    }

    fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a {
        self.0
            .iter()
            .map(|(id, path)| (*id, path.as_relative_path()))
    }
}