Skip to content

Commit 87e55cc

Browse files
committed
Use the parsed exercises instead of glob
1 parent b932ed1 commit 87e55cc

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ edition = "2021"
1212
anyhow = "1.0.81"
1313
clap = { version = "4.5.2", features = ["derive"] }
1414
console = "0.15.8"
15-
glob = "0.3.0"
1615
home = "0.5.9"
1716
indicatif = "0.17.8"
1817
notify-debouncer-mini = "0.4.1"

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ fn main() -> Result<()> {
206206
Subcommands::Lsp => {
207207
let mut project = RustAnalyzerProject::build()?;
208208
project
209-
.exercises_to_json()
209+
.exercises_to_json(exercises)
210210
.expect("Couldn't parse rustlings exercises files");
211211

212212
if project.crates.is_empty() {

src/project.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use anyhow::{bail, Context, Result};
2-
use glob::glob;
32
use serde::{Deserialize, Serialize};
43
use std::env;
54
use std::error::Error;
65
use std::path::PathBuf;
76
use std::process::{Command, Stdio};
87

8+
use crate::exercise::Exercise;
9+
910
/// Contains the structure of resulting rust-project.json file
1011
/// and functions to build the data required to create the file
1112
#[derive(Serialize, Deserialize)]
@@ -69,30 +70,20 @@ impl RustAnalyzerProject {
6970
Ok(())
7071
}
7172

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-
8973
/// Parse the exercises folder for .rs files, any matches will create
9074
/// a new `crate` in rust-project.json which allows rust-analyzer to
9175
/// 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();
9687
Ok(())
9788
}
9889
}

0 commit comments

Comments
 (0)