(
&self,
base: &Path,
path: &Path,
parent: String,
mode: Mode,
compiler: &dyn Compiler,
)
| 123 | } |
| 124 | |
| 125 | fn compile_dir( |
| 126 | &self, |
| 127 | base: &Path, |
| 128 | path: &Path, |
| 129 | parent: String, |
| 130 | mode: Mode, |
| 131 | compiler: &dyn Compiler, |
| 132 | ) -> Result<HashMap<String, CompiledModule>, Diagnostic> { |
| 133 | let mut code_map = HashMap::new(); |
| 134 | let paths = fs::read_dir(path) |
| 135 | .or_else(|e| { |
| 136 | if cfg!(windows) |
| 137 | && let Ok(real_path) = fs::read_to_string(path.canonicalize().unwrap()) |
| 138 | { |
| 139 | return fs::read_dir(real_path.trim()); |
| 140 | } |
| 141 | Err(e) |
| 142 | }) |
| 143 | .map_err(|err| { |
| 144 | Diagnostic::spans_error(self.span, format!("Error listing dir {path:?}: {err}")) |
| 145 | })?; |
| 146 | for path in paths { |
| 147 | let path = path.map_err(|err| { |
| 148 | Diagnostic::spans_error(self.span, format!("Failed to list file: {err}")) |
| 149 | })?; |
| 150 | let path = path.path(); |
| 151 | let file_name = path.file_name().unwrap().to_str().ok_or_else(|| { |
| 152 | Diagnostic::spans_error(self.span, format!("Invalid UTF-8 in file name {path:?}")) |
| 153 | })?; |
| 154 | if path.is_dir() { |
| 155 | code_map.extend(self.compile_dir( |
| 156 | base, |
| 157 | &path, |
| 158 | if parent.is_empty() { |
| 159 | file_name.to_string() |
| 160 | } else { |
| 161 | format!("{parent}.{file_name}") |
| 162 | }, |
| 163 | mode, |
| 164 | compiler, |
| 165 | )?); |
| 166 | } else if file_name.ends_with(".py") { |
| 167 | let stem = path.file_stem().unwrap().to_str().unwrap(); |
| 168 | let is_init = stem == "__init__"; |
| 169 | let module_name = if is_init { |
| 170 | parent.clone() |
| 171 | } else if parent.is_empty() { |
| 172 | stem.to_owned() |
| 173 | } else { |
| 174 | format!("{parent}.{stem}") |
| 175 | }; |
| 176 | |
| 177 | let compile_path = |src_path: &Path| { |
| 178 | let source = fs::read_to_string(src_path).map_err(|err| { |
| 179 | Diagnostic::spans_error( |
| 180 | self.span, |
| 181 | format!("Error reading file {path:?}: {err}"), |
| 182 | ) |
no test coverage detected