From 87e55ccffde51b08be7d90ab53f1bb2462efa85a Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 22:20:00 +0100 Subject: [PATCH] Use the parsed exercises instead of glob --- Cargo.toml | 1 - src/main.rs | 2 +- src/project.rs | 35 +++++++++++++---------------------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7b5a096..ef499473 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ edition = "2021" anyhow = "1.0.81" clap = { version = "4.5.2", features = ["derive"] } console = "0.15.8" -glob = "0.3.0" home = "0.5.9" indicatif = "0.17.8" notify-debouncer-mini = "0.4.1" diff --git a/src/main.rs b/src/main.rs index 4ce0b30d..803e2f8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -206,7 +206,7 @@ fn main() -> Result<()> { Subcommands::Lsp => { let mut project = RustAnalyzerProject::build()?; project - .exercises_to_json() + .exercises_to_json(exercises) .expect("Couldn't parse rustlings exercises files"); if project.crates.is_empty() { diff --git a/src/project.rs b/src/project.rs index 1f42d4eb..534aab09 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> { - 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> { - for path in glob("./exercises/**/*")? { - self.path_to_json(path?)?; - } + pub fn exercises_to_json(&mut self, exercises: Vec) -> Result<(), Box> { + 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(()) } }