|
1 | 1 | use anyhow::{bail, Context, Result};
|
2 |
| -use glob::glob; |
3 | 2 | use serde::{Deserialize, Serialize};
|
4 | 3 | use std::env;
|
5 | 4 | use std::error::Error;
|
6 | 5 | use std::path::PathBuf;
|
7 | 6 | use std::process::{Command, Stdio};
|
8 | 7 |
|
| 8 | +use crate::exercise::Exercise; |
| 9 | + |
9 | 10 | /// Contains the structure of resulting rust-project.json file
|
10 | 11 | /// and functions to build the data required to create the file
|
11 | 12 | #[derive(Serialize, Deserialize)]
|
@@ -69,30 +70,20 @@ impl RustAnalyzerProject {
|
69 | 70 | Ok(())
|
70 | 71 | }
|
71 | 72 |
|
72 |
| - /// If path contains .rs extension, add a crate to `rust-project.json` |
73 |
| - fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box<dyn Error>> { |
74 |
| - if let Some(ext) = path.extension() { |
75 |
| - if ext == "rs" { |
76 |
| - self.crates.push(Crate { |
77 |
| - root_module: path.display().to_string(), |
78 |
| - edition: "2021".to_string(), |
79 |
| - deps: Vec::new(), |
80 |
| - // This allows rust_analyzer to work inside #[test] blocks |
81 |
| - cfg: vec!["test".to_string()], |
82 |
| - }) |
83 |
| - } |
84 |
| - } |
85 |
| - |
86 |
| - Ok(()) |
87 |
| - } |
88 |
| - |
89 | 73 | /// Parse the exercises folder for .rs files, any matches will create
|
90 | 74 | /// a new `crate` in rust-project.json which allows rust-analyzer to
|
91 | 75 | /// treat it like a normal binary
|
92 |
| - pub fn exercises_to_json(&mut self) -> Result<(), Box<dyn Error>> { |
93 |
| - for path in glob("./exercises/**/*")? { |
94 |
| - self.path_to_json(path?)?; |
95 |
| - } |
| 76 | + pub fn exercises_to_json(&mut self, exercises: Vec<Exercise>) -> Result<(), Box<dyn Error>> { |
| 77 | + self.crates = exercises |
| 78 | + .into_iter() |
| 79 | + .map(|exercise| Crate { |
| 80 | + root_module: exercise.path.display().to_string(), |
| 81 | + edition: "2021".to_string(), |
| 82 | + deps: Vec::new(), |
| 83 | + // This allows rust_analyzer to work inside #[test] blocks |
| 84 | + cfg: vec!["test".to_string()], |
| 85 | + }) |
| 86 | + .collect(); |
96 | 87 | Ok(())
|
97 | 88 | }
|
98 | 89 | }
|
0 commit comments