-
Notifications
You must be signed in to change notification settings - Fork 499
/
source.rs
61 lines (57 loc) · 1.52 KB
/
source.rs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use super::*;
#[derive(Debug)]
pub(crate) struct Source<'src> {
pub(crate) file_depth: u32,
pub(crate) file_path: Vec<PathBuf>,
pub(crate) import_offsets: Vec<usize>,
pub(crate) namepath: Namepath<'src>,
pub(crate) path: PathBuf,
pub(crate) working_directory: PathBuf,
}
impl<'src> Source<'src> {
pub(crate) fn root(path: &Path) -> Self {
Self {
file_depth: 0,
file_path: vec![path.into()],
import_offsets: Vec::new(),
namepath: Namepath::default(),
path: path.into(),
working_directory: path.parent().unwrap().into(),
}
}
pub(crate) fn import(&self, path: PathBuf, import_offset: usize) -> Self {
Self {
file_depth: self.file_depth + 1,
file_path: self
.file_path
.clone()
.into_iter()
.chain(iter::once(path.clone()))
.collect(),
import_offsets: self
.import_offsets
.iter()
.copied()
.chain(iter::once(import_offset))
.collect(),
namepath: self.namepath.clone(),
path,
working_directory: self.working_directory.clone(),
}
}
pub(crate) fn module(&self, name: Name<'src>, path: PathBuf) -> Self {
Self {
file_depth: self.file_depth + 1,
file_path: self
.file_path
.clone()
.into_iter()
.chain(iter::once(path.clone()))
.collect(),
import_offsets: Vec::new(),
namepath: self.namepath.join(name),
path: path.clone(),
working_directory: path.parent().unwrap().into(),
}
}
}