| 231 | ); |
| 232 | |
| 233 | fn path_to_module(root: &Path, path: &Path) -> Result<String> { |
| 234 | let abs_root = std::fs::canonicalize(root)?; |
| 235 | let abs_path = std::fs::canonicalize(path)?; |
| 236 | |
| 237 | if abs_path.starts_with(&abs_root) && abs_path != abs_root { |
| 238 | let relative = abs_path.strip_prefix(&abs_root)?; |
| 239 | let relative = relative.to_str().unwrap().to_owned(); |
| 240 | if relative.ends_with(".py") { |
| 241 | Ok(relative[..relative.len() - 3].replace("/", ".")) |
| 242 | } else if relative.ends_with('/') { |
| 243 | Ok(relative[..relative.len() - 1].replace("/", ".")) |
| 244 | } else { |
| 245 | Ok(relative.replace("/", ".")) |
| 246 | } |
| 247 | } else { |
| 248 | Err(anyhow::anyhow!("module not found")) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #[cfg(test)] |
| 253 | mod test { |