diff options
| author | mo8it <mo8it@proton.me> | 2024-03-25 22:20:00 +0100 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-03-25 22:20:00 +0100 |
| commit | 87e55ccffde51b08be7d90ab53f1bb2462efa85a (patch) | |
| tree | 41db225b29ae6088f30a219372f7ccd6c368ddff /src/project.rs | |
| parent | b932ed1f672532e7dccf6cd23f6b9895c24a4de7 (diff) | |
Use the parsed exercises instead of glob
Diffstat (limited to 'src/project.rs')
| -rw-r--r-- | src/project.rs | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/src/project.rs b/src/project.rs index 1f42d4e..534aab0 100644 --- a/src/project.rs +++ b/src/project.rs @@ -1,11 +1,12 @@ use anyhow::{bail, Context, Result}; -use glob::glob; use serde::{Deserialize, Serialize}; use std::env; use std::error::Error; use std::path::PathBuf; use std::process::{Command, Stdio}; +use crate::exercise::Exercise; + /// Contains the structure of resulting rust-project.json file /// and functions to build the data required to create the file #[derive(Serialize, Deserialize)] @@ -69,30 +70,20 @@ impl RustAnalyzerProject { Ok(()) } - /// If path contains .rs extension, add a crate to `rust-project.json` - fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box<dyn Error>> { - if let Some(ext) = path.extension() { - if ext == "rs" { - self.crates.push(Crate { - root_module: path.display().to_string(), - edition: "2021".to_string(), - deps: Vec::new(), - // This allows rust_analyzer to work inside #[test] blocks - cfg: vec!["test".to_string()], - }) - } - } - - Ok(()) - } - /// Parse the exercises folder for .rs files, any matches will create /// a new `crate` in rust-project.json which allows rust-analyzer to /// treat it like a normal binary - pub fn exercises_to_json(&mut self) -> Result<(), Box<dyn Error>> { - for path in glob("./exercises/**/*")? { - self.path_to_json(path?)?; - } + pub fn exercises_to_json(&mut self, exercises: Vec<Exercise>) -> Result<(), Box<dyn Error>> { + self.crates = exercises + .into_iter() + .map(|exercise| Crate { + root_module: exercise.path.display().to_string(), + edition: "2021".to_string(), + deps: Vec::new(), + // This allows rust_analyzer to work inside #[test] blocks + cfg: vec!["test".to_string()], + }) + .collect(); Ok(()) } } |
