Add write_project_json

This commit is contained in:
mo8it 2024-03-25 23:01:56 +01:00
parent 8d3ec24c11
commit 8ddbf9635d
2 changed files with 32 additions and 42 deletions

View file

@ -1,5 +1,5 @@
use crate::exercise::{Exercise, ExerciseList}; use crate::exercise::{Exercise, ExerciseList};
use crate::project::RustAnalyzerProject; use crate::project::write_project_json;
use crate::run::{reset, run}; use crate::run::{reset, run};
use crate::verify::verify; use crate::verify::verify;
use anyhow::Result; use anyhow::Result;
@ -204,13 +204,8 @@ fn main() -> Result<()> {
} }
Subcommands::Lsp => { Subcommands::Lsp => {
let mut project = RustAnalyzerProject::build()?; if let Err(e) = write_project_json(exercises) {
project println!("Failed to write rust-project.json to disk for rust-analyzer: {e}");
.exercises_to_json(exercises)
.expect("Couldn't parse rustlings exercises files");
if project.write_to_disk().is_err() {
println!("Failed to write rust-project.json to disk for rust-analyzer");
} else { } else {
println!("Successfully generated rust-project.json"); println!("Successfully generated rust-project.json");
println!("rust-analyzer will now parse exercises, restart your language server or editor") println!("rust-analyzer will now parse exercises, restart your language server or editor")

View file

@ -1,7 +1,6 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use serde::Serialize; use serde::Serialize;
use std::env; use std::env;
use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
@ -10,7 +9,7 @@ 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)] #[derive(Serialize)]
pub struct RustAnalyzerProject { struct RustAnalyzerProject {
sysroot_src: PathBuf, sysroot_src: PathBuf,
crates: Vec<Crate>, crates: Vec<Crate>,
} }
@ -25,12 +24,22 @@ struct Crate {
} }
impl RustAnalyzerProject { impl RustAnalyzerProject {
pub fn build() -> Result<Self> { fn build(exercises: Vec<Exercise>) -> Result<Self> {
// check if RUST_SRC_PATH is set let crates = exercises
.into_iter()
.map(|exercise| Crate {
root_module: exercise.path,
edition: "2021",
deps: Vec::new(),
// This allows rust_analyzer to work inside #[test] blocks
cfg: ["test"],
})
.collect();
if let Some(path) = env::var_os("RUST_SRC_PATH") { if let Some(path) = env::var_os("RUST_SRC_PATH") {
return Ok(Self { return Ok(Self {
sysroot_src: PathBuf::from(path), sysroot_src: PathBuf::from(path),
crates: Vec::new(), crates,
}); });
} }
@ -53,35 +62,21 @@ impl RustAnalyzerProject {
Ok(Self { Ok(Self {
sysroot_src, sysroot_src,
crates: Vec::new(), crates,
}) })
} }
}
/// Write rust-project.json to disk /// Write `rust-project.json` to disk.
pub fn write_to_disk(&self) -> Result<(), std::io::Error> { pub fn write_project_json(exercises: Vec<Exercise>) -> Result<()> {
// Using the capacity 2^14 = 16384 since the file length in bytes is higher than 2^13. let content = RustAnalyzerProject::build(exercises)?;
// Using the capacity 2^14 since the file length in bytes is higher than 2^13.
// The final length is not known exactly because it depends on the user's sysroot path, // The final length is not known exactly because it depends on the user's sysroot path,
// the current number of exercises etc. // the current number of exercises etc.
let mut buf = Vec::with_capacity(16384); let mut buf = Vec::with_capacity(1 << 14);
serde_json::to_writer(&mut buf, &self).expect("Failed to serialize to JSON"); serde_json::to_writer(&mut buf, &content)?;
std::fs::write("rust-project.json", buf)?; std::fs::write("rust-project.json", buf)?;
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, exercises: Vec<Exercise>) -> Result<(), Box<dyn Error>> {
self.crates = exercises
.into_iter()
.map(|exercise| Crate {
root_module: exercise.path,
edition: "2021",
deps: Vec::new(),
// This allows rust_analyzer to work inside #[test] blocks
cfg: ["test"],
})
.collect();
Ok(()) Ok(())
} }
}