Skip to content

Commit 8ddbf96

Browse files
committed
Add write_project_json
1 parent 8d3ec24 commit 8ddbf96

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::exercise::{Exercise, ExerciseList};
2-
use crate::project::RustAnalyzerProject;
2+
use crate::project::write_project_json;
33
use crate::run::{reset, run};
44
use crate::verify::verify;
55
use anyhow::Result;
@@ -204,13 +204,8 @@ fn main() -> Result<()> {
204204
}
205205

206206
Subcommands::Lsp => {
207-
let mut project = RustAnalyzerProject::build()?;
208-
project
209-
.exercises_to_json(exercises)
210-
.expect("Couldn't parse rustlings exercises files");
211-
212-
if project.write_to_disk().is_err() {
213-
println!("Failed to write rust-project.json to disk for rust-analyzer");
207+
if let Err(e) = write_project_json(exercises) {
208+
println!("Failed to write rust-project.json to disk for rust-analyzer: {e}");
214209
} else {
215210
println!("Successfully generated rust-project.json");
216211
println!("rust-analyzer will now parse exercises, restart your language server or editor")

src/project.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::{Context, Result};
22
use serde::Serialize;
33
use std::env;
4-
use std::error::Error;
54
use std::path::PathBuf;
65
use std::process::{Command, Stdio};
76

@@ -10,7 +9,7 @@ use crate::exercise::Exercise;
109
/// Contains the structure of resulting rust-project.json file
1110
/// and functions to build the data required to create the file
1211
#[derive(Serialize)]
13-
pub struct RustAnalyzerProject {
12+
struct RustAnalyzerProject {
1413
sysroot_src: PathBuf,
1514
crates: Vec<Crate>,
1615
}
@@ -25,12 +24,22 @@ struct Crate {
2524
}
2625

2726
impl RustAnalyzerProject {
28-
pub fn build() -> Result<Self> {
29-
// check if RUST_SRC_PATH is set
27+
fn build(exercises: Vec<Exercise>) -> Result<Self> {
28+
let crates = exercises
29+
.into_iter()
30+
.map(|exercise| Crate {
31+
root_module: exercise.path,
32+
edition: "2021",
33+
deps: Vec::new(),
34+
// This allows rust_analyzer to work inside #[test] blocks
35+
cfg: ["test"],
36+
})
37+
.collect();
38+
3039
if let Some(path) = env::var_os("RUST_SRC_PATH") {
3140
return Ok(Self {
3241
sysroot_src: PathBuf::from(path),
33-
crates: Vec::new(),
42+
crates,
3443
});
3544
}
3645

@@ -53,35 +62,21 @@ impl RustAnalyzerProject {
5362

5463
Ok(Self {
5564
sysroot_src,
56-
crates: Vec::new(),
65+
crates,
5766
})
5867
}
68+
}
5969

60-
/// Write rust-project.json to disk
61-
pub fn write_to_disk(&self) -> Result<(), std::io::Error> {
62-
// Using the capacity 2^14 = 16384 since the file length in bytes is higher than 2^13.
63-
// The final length is not known exactly because it depends on the user's sysroot path,
64-
// the current number of exercises etc.
65-
let mut buf = Vec::with_capacity(16384);
66-
serde_json::to_writer(&mut buf, &self).expect("Failed to serialize to JSON");
67-
std::fs::write("rust-project.json", buf)?;
68-
Ok(())
69-
}
70+
/// Write `rust-project.json` to disk.
71+
pub fn write_project_json(exercises: Vec<Exercise>) -> Result<()> {
72+
let content = RustAnalyzerProject::build(exercises)?;
7073

71-
/// Parse the exercises folder for .rs files, any matches will create
72-
/// a new `crate` in rust-project.json which allows rust-analyzer to
73-
/// treat it like a normal binary
74-
pub fn exercises_to_json(&mut self, exercises: Vec<Exercise>) -> Result<(), Box<dyn Error>> {
75-
self.crates = exercises
76-
.into_iter()
77-
.map(|exercise| Crate {
78-
root_module: exercise.path,
79-
edition: "2021",
80-
deps: Vec::new(),
81-
// This allows rust_analyzer to work inside #[test] blocks
82-
cfg: ["test"],
83-
})
84-
.collect();
85-
Ok(())
86-
}
74+
// Using the capacity 2^14 since the file length in bytes is higher than 2^13.
75+
// The final length is not known exactly because it depends on the user's sysroot path,
76+
// the current number of exercises etc.
77+
let mut buf = Vec::with_capacity(1 << 14);
78+
serde_json::to_writer(&mut buf, &content)?;
79+
std::fs::write("rust-project.json", buf)?;
80+
81+
Ok(())
8782
}

0 commit comments

Comments
 (0)