mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 14:57:32 -05:00
Add write_project_json
This commit is contained in:
parent
8d3ec24c11
commit
8ddbf9635d
2 changed files with 32 additions and 42 deletions
11
src/main.rs
11
src/main.rs
|
@ -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")
|
||||||
|
|
|
@ -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
|
|
||||||
pub fn write_to_disk(&self) -> Result<(), std::io::Error> {
|
/// Write `rust-project.json` to disk.
|
||||||
// Using the capacity 2^14 = 16384 since the file length in bytes is higher than 2^13.
|
pub fn write_project_json(exercises: Vec<Exercise>) -> Result<()> {
|
||||||
// The final length is not known exactly because it depends on the user's sysroot path,
|
let content = RustAnalyzerProject::build(exercises)?;
|
||||||
// the current number of exercises etc.
|
|
||||||
let mut buf = Vec::with_capacity(16384);
|
// Using the capacity 2^14 since the file length in bytes is higher than 2^13.
|
||||||
serde_json::to_writer(&mut buf, &self).expect("Failed to serialize to JSON");
|
// The final length is not known exactly because it depends on the user's sysroot path,
|
||||||
std::fs::write("rust-project.json", buf)?;
|
// the current number of exercises etc.
|
||||||
Ok(())
|
let mut buf = Vec::with_capacity(1 << 14);
|
||||||
}
|
serde_json::to_writer(&mut buf, &content)?;
|
||||||
|
std::fs::write("rust-project.json", buf)?;
|
||||||
/// Parse the exercises folder for .rs files, any matches will create
|
|
||||||
/// a new `crate` in rust-project.json which allows rust-analyzer to
|
Ok(())
|
||||||
/// 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(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue