Use the parsed exercises instead of glob

This commit is contained in:
mo8it 2024-03-25 22:20:00 +01:00
parent b932ed1f67
commit 87e55ccffd
3 changed files with 14 additions and 24 deletions

View file

@ -12,7 +12,6 @@ edition = "2021"
anyhow = "1.0.81" anyhow = "1.0.81"
clap = { version = "4.5.2", features = ["derive"] } clap = { version = "4.5.2", features = ["derive"] }
console = "0.15.8" console = "0.15.8"
glob = "0.3.0"
home = "0.5.9" home = "0.5.9"
indicatif = "0.17.8" indicatif = "0.17.8"
notify-debouncer-mini = "0.4.1" notify-debouncer-mini = "0.4.1"

View file

@ -206,7 +206,7 @@ fn main() -> Result<()> {
Subcommands::Lsp => { Subcommands::Lsp => {
let mut project = RustAnalyzerProject::build()?; let mut project = RustAnalyzerProject::build()?;
project project
.exercises_to_json() .exercises_to_json(exercises)
.expect("Couldn't parse rustlings exercises files"); .expect("Couldn't parse rustlings exercises files");
if project.crates.is_empty() { if project.crates.is_empty() {

View file

@ -1,11 +1,12 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use glob::glob;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env; use std::env;
use std::error::Error; use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use crate::exercise::Exercise;
/// Contains the structure of resulting rust-project.json file /// Contains the structure of resulting rust-project.json file
/// and functions to build the data required to create the file /// and functions to build the data required to create the file
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -69,30 +70,20 @@ impl RustAnalyzerProject {
Ok(()) Ok(())
} }
/// If path contains .rs extension, add a crate to `rust-project.json` /// Parse the exercises folder for .rs files, any matches will create
fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box<dyn Error>> { /// a new `crate` in rust-project.json which allows rust-analyzer to
if let Some(ext) = path.extension() { /// treat it like a normal binary
if ext == "rs" { pub fn exercises_to_json(&mut self, exercises: Vec<Exercise>) -> Result<(), Box<dyn Error>> {
self.crates.push(Crate { self.crates = exercises
root_module: path.display().to_string(), .into_iter()
.map(|exercise| Crate {
root_module: exercise.path.display().to_string(),
edition: "2021".to_string(), edition: "2021".to_string(),
deps: Vec::new(), deps: Vec::new(),
// This allows rust_analyzer to work inside #[test] blocks // This allows rust_analyzer to work inside #[test] blocks
cfg: vec!["test".to_string()], cfg: vec!["test".to_string()],
}) })
} .collect();
}
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?)?;
}
Ok(()) Ok(())
} }
} }