mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 05:52:23 -05:00
Merge get_sysroot_src into the constructor
This commit is contained in:
parent
efa9f57048
commit
51712cc19f
2 changed files with 39 additions and 43 deletions
|
@ -204,10 +204,7 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Subcommands::Lsp => {
|
Subcommands::Lsp => {
|
||||||
let mut project = RustAnalyzerProject::new();
|
let mut project = RustAnalyzerProject::build()?;
|
||||||
project
|
|
||||||
.get_sysroot_src()
|
|
||||||
.expect("Couldn't find toolchain path, do you have `rustc` installed?");
|
|
||||||
project
|
project
|
||||||
.exercises_to_json()
|
.exercises_to_json()
|
||||||
.expect("Couldn't parse rustlings exercises files");
|
.expect("Couldn't parse rustlings exercises files");
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use anyhow::{bail, Context, Result};
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
@ -22,11 +23,44 @@ pub struct Crate {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RustAnalyzerProject {
|
impl RustAnalyzerProject {
|
||||||
pub fn new() -> RustAnalyzerProject {
|
pub fn build() -> Result<Self> {
|
||||||
RustAnalyzerProject {
|
// check if RUST_SRC_PATH is set
|
||||||
sysroot_src: String::new(),
|
if let Ok(sysroot_src) = env::var("RUST_SRC_PATH") {
|
||||||
crates: Vec::new(),
|
return Ok(Self {
|
||||||
|
sysroot_src,
|
||||||
|
crates: Vec::new(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let toolchain = Command::new("rustc")
|
||||||
|
.arg("--print")
|
||||||
|
.arg("sysroot")
|
||||||
|
.output()
|
||||||
|
.context("Failed to get the sysroot from `rustc`. Do you have `rustc` installed?")?
|
||||||
|
.stdout;
|
||||||
|
|
||||||
|
let toolchain =
|
||||||
|
String::from_utf8(toolchain).context("The toolchain path is invalid UTF8")?;
|
||||||
|
let toolchain = toolchain.trim_end();
|
||||||
|
|
||||||
|
println!("Determined toolchain: {toolchain}\n");
|
||||||
|
|
||||||
|
let Ok(sysroot_src) = Path::new(toolchain)
|
||||||
|
.join("lib")
|
||||||
|
.join("rustlib")
|
||||||
|
.join("src")
|
||||||
|
.join("rust")
|
||||||
|
.join("library")
|
||||||
|
.into_os_string()
|
||||||
|
.into_string()
|
||||||
|
else {
|
||||||
|
bail!("The sysroot path is invalid UTF8");
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
sysroot_src,
|
||||||
|
crates: Vec::new(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write rust-project.json to disk
|
/// Write rust-project.json to disk
|
||||||
|
@ -66,39 +100,4 @@ impl RustAnalyzerProject {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use `rustc` to determine the default toolchain
|
|
||||||
pub fn get_sysroot_src(&mut self) -> Result<(), Box<dyn Error>> {
|
|
||||||
// check if RUST_SRC_PATH is set
|
|
||||||
if let Ok(path) = env::var("RUST_SRC_PATH") {
|
|
||||||
self.sysroot_src = path;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let toolchain = Command::new("rustc")
|
|
||||||
.arg("--print")
|
|
||||||
.arg("sysroot")
|
|
||||||
.output()?
|
|
||||||
.stdout;
|
|
||||||
|
|
||||||
let toolchain = String::from_utf8(toolchain)?;
|
|
||||||
let toolchain = toolchain.trim_end();
|
|
||||||
|
|
||||||
println!("Determined toolchain: {toolchain}\n");
|
|
||||||
|
|
||||||
let Ok(path) = Path::new(toolchain)
|
|
||||||
.join("lib")
|
|
||||||
.join("rustlib")
|
|
||||||
.join("src")
|
|
||||||
.join("rust")
|
|
||||||
.join("library")
|
|
||||||
.into_os_string()
|
|
||||||
.into_string()
|
|
||||||
else {
|
|
||||||
return Err("The sysroot path is invalid UTF8".into());
|
|
||||||
};
|
|
||||||
self.sysroot_src = path;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue