kaiju_compiler_cli_core/
fs_module_reader.rs1use crate::compiler_core::module_reader::ModuleReader;
2use relative_path::{RelativePath, RelativePathBuf};
3use std::fs::read_to_string;
4
5fn dir_path(path: &RelativePath) -> RelativePathBuf {
6 let mut path = path.to_relative_path_buf();
7 path.pop();
8 path
9}
10
11#[derive(Default)]
12pub struct FsModuleReader {
13 path_stack: Vec<RelativePathBuf>,
14}
15
16impl ModuleReader for FsModuleReader {
17 fn load_module_source(&self, path: &str) -> Option<String> {
18 if let Ok(source) = read_to_string(path) {
19 Some(source)
20 } else {
21 None
22 }
23 }
24
25 fn push_module_path(&mut self, path: &str) {
26 self.path_stack.push(dir_path(&RelativePath::new(path)));
27 }
28
29 fn pop_module_path(&mut self) {
30 self.path_stack.pop();
31 }
32
33 fn compose_path(&self, relative_path: &str) -> String {
34 let relative_path = RelativePathBuf::from(relative_path);
35 if let Some(path) = self.path_stack.last() {
36 let mut path = path.clone();
37 path.push(relative_path);
38 path
39 } else {
40 relative_path.to_relative_path_buf()
41 }
42 .normalize()
43 .as_str()
44 .to_owned()
45 }
46}