1use super::*;
2
3#[derive(Debug)]
4pub struct Source<'src> {
5 pub file_depth: u32,
6 pub file_path: Vec<PathBuf>,
7 pub import_offsets: Vec<usize>,
8 pub namepath: Namepath<'src>,
9 pub path: PathBuf,
10 pub working_directory: PathBuf,
11}
12
13impl<'src> Source<'src> {
14 pub fn root(path: &Path) -> Self {
15 Self {
16 file_depth: 0,
17 file_path: vec![path.into()],
18 import_offsets: Vec::new(),
19 namepath: Namepath::default(),
20 path: path.into(),
21 working_directory: path.parent().unwrap().into(),
22 }
23 }
24
25 pub fn import(&self, path: PathBuf, import_offset: usize) -> Self {
26 Self {
27 file_depth: self.file_depth + 1,
28 file_path: self
29 .file_path
30 .clone()
31 .into_iter()
32 .chain(iter::once(path.clone()))
33 .collect(),
34 import_offsets: self
35 .import_offsets
36 .iter()
37 .copied()
38 .chain(iter::once(import_offset))
39 .collect(),
40 namepath: self.namepath.clone(),
41 path,
42 working_directory: self.working_directory.clone(),
43 }
44 }
45
46 pub fn module(&self, name: Name<'src>, path: PathBuf) -> Self {
47 Self {
48 file_depth: self.file_depth + 1,
49 file_path: self
50 .file_path
51 .clone()
52 .into_iter()
53 .chain(iter::once(path.clone()))
54 .collect(),
55 import_offsets: Vec::new(),
56 namepath: self.namepath.join(name),
57 path: path.clone(),
58 working_directory: path.parent().unwrap().into(),
59 }
60 }
61}