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
31
32
33
34
35
36
37
38
39
40
41
42
use std::{
collections::HashMap,
path::PathBuf,
sync::{
atomic::{AtomicU8, Ordering},
RwLock,
},
};
use lazy_static::lazy_static;
lazy_static! {
static ref SOURCE_IDS_TO_FILES: RwLock<HashMap<SourceId, (PathBuf, String)>> =
RwLock::new(HashMap::new());
}
static SOURCE_ID_COUNTER: AtomicU8 = AtomicU8::new(1);
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct SourceId(pub u8);
impl SourceId {
pub fn new(path: PathBuf, content: String) -> Self {
let source_id = Self(SOURCE_ID_COUNTER.fetch_add(1, Ordering::SeqCst));
SOURCE_IDS_TO_FILES
.write()
.unwrap()
.insert(source_id, (path, content));
source_id
}
pub const fn null() -> Self {
Self(0)
}
pub fn get_file(&self) -> Option<(PathBuf, String)> {
SOURCE_IDS_TO_FILES.read().unwrap().get(self).cloned()
}
}