|
1 | 1 | use glob::glob; |
2 | 2 | use serde::{Deserialize, Serialize}; |
3 | | -use std::env; |
4 | 3 | use std::error::Error; |
| 4 | +use std::path::PathBuf; |
5 | 5 | use std::process::Command; |
| 6 | +use std::{env, fs}; |
6 | 7 |
|
7 | 8 | /// Contains the structure of resulting rust-project.json file |
8 | 9 | /// and functions to build the data required to create the file |
@@ -38,27 +39,29 @@ impl RustAnalyzerProject { |
38 | 39 | } |
39 | 40 |
|
40 | 41 | /// If path contains .rs extension, add a crate to `rust-project.json` |
41 | | - fn path_to_json(&mut self, path: String) { |
42 | | - if let Some((_, ext)) = path.split_once('.') { |
| 42 | + fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box<dyn Error>> { |
| 43 | + if let Some(ext) = path.extension() { |
43 | 44 | if ext == "rs" { |
| 45 | + let abspath = fs::canonicalize(path)?; |
44 | 46 | self.crates.push(Crate { |
45 | | - root_module: path, |
| 47 | + root_module: abspath.display().to_string(), |
46 | 48 | edition: "2021".to_string(), |
47 | 49 | deps: Vec::new(), |
48 | 50 | // This allows rust_analyzer to work inside #[test] blocks |
49 | 51 | cfg: vec!["test".to_string()], |
50 | 52 | }) |
51 | 53 | } |
52 | 54 | } |
| 55 | + |
| 56 | + Ok(()) |
53 | 57 | } |
54 | 58 |
|
55 | 59 | /// Parse the exercises folder for .rs files, any matches will create |
56 | 60 | /// a new `crate` in rust-project.json which allows rust-analyzer to |
57 | 61 | /// treat it like a normal binary |
58 | 62 | pub fn exercises_to_json(&mut self) -> Result<(), Box<dyn Error>> { |
59 | | - for e in glob("./exercises/**/*")? { |
60 | | - let path = e?.to_string_lossy().to_string(); |
61 | | - self.path_to_json(path); |
| 63 | + for path in glob("./exercises/**/*")? { |
| 64 | + self.path_to_json(path?)?; |
62 | 65 | } |
63 | 66 | Ok(()) |
64 | 67 | } |
|
0 commit comments