Skip to content

Commit 51712cc

Browse files
committed
Merge get_sysroot_src into the constructor
1 parent efa9f57 commit 51712cc

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ fn main() -> Result<()> {
204204
}
205205

206206
Subcommands::Lsp => {
207-
let mut project = RustAnalyzerProject::new();
208-
project
209-
.get_sysroot_src()
210-
.expect("Couldn't find toolchain path, do you have `rustc` installed?");
207+
let mut project = RustAnalyzerProject::build()?;
211208
project
212209
.exercises_to_json()
213210
.expect("Couldn't parse rustlings exercises files");

src/project.rs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::{bail, Context, Result};
12
use glob::glob;
23
use serde::{Deserialize, Serialize};
34
use std::env;
@@ -22,11 +23,44 @@ pub struct Crate {
2223
}
2324

2425
impl RustAnalyzerProject {
25-
pub fn new() -> RustAnalyzerProject {
26-
RustAnalyzerProject {
27-
sysroot_src: String::new(),
28-
crates: Vec::new(),
26+
pub fn build() -> Result<Self> {
27+
// check if RUST_SRC_PATH is set
28+
if let Ok(sysroot_src) = env::var("RUST_SRC_PATH") {
29+
return Ok(Self {
30+
sysroot_src,
31+
crates: Vec::new(),
32+
});
2933
}
34+
35+
let toolchain = Command::new("rustc")
36+
.arg("--print")
37+
.arg("sysroot")
38+
.output()
39+
.context("Failed to get the sysroot from `rustc`. Do you have `rustc` installed?")?
40+
.stdout;
41+
42+
let toolchain =
43+
String::from_utf8(toolchain).context("The toolchain path is invalid UTF8")?;
44+
let toolchain = toolchain.trim_end();
45+
46+
println!("Determined toolchain: {toolchain}\n");
47+
48+
let Ok(sysroot_src) = Path::new(toolchain)
49+
.join("lib")
50+
.join("rustlib")
51+
.join("src")
52+
.join("rust")
53+
.join("library")
54+
.into_os_string()
55+
.into_string()
56+
else {
57+
bail!("The sysroot path is invalid UTF8");
58+
};
59+
60+
Ok(Self {
61+
sysroot_src,
62+
crates: Vec::new(),
63+
})
3064
}
3165

3266
/// Write rust-project.json to disk
@@ -66,39 +100,4 @@ impl RustAnalyzerProject {
66100
}
67101
Ok(())
68102
}
69-
70-
/// Use `rustc` to determine the default toolchain
71-
pub fn get_sysroot_src(&mut self) -> Result<(), Box<dyn Error>> {
72-
// check if RUST_SRC_PATH is set
73-
if let Ok(path) = env::var("RUST_SRC_PATH") {
74-
self.sysroot_src = path;
75-
return Ok(());
76-
}
77-
78-
let toolchain = Command::new("rustc")
79-
.arg("--print")
80-
.arg("sysroot")
81-
.output()?
82-
.stdout;
83-
84-
let toolchain = String::from_utf8(toolchain)?;
85-
let toolchain = toolchain.trim_end();
86-
87-
println!("Determined toolchain: {toolchain}\n");
88-
89-
let Ok(path) = Path::new(toolchain)
90-
.join("lib")
91-
.join("rustlib")
92-
.join("src")
93-
.join("rust")
94-
.join("library")
95-
.into_os_string()
96-
.into_string()
97-
else {
98-
return Err("The sysroot path is invalid UTF8".into());
99-
};
100-
self.sysroot_src = path;
101-
102-
Ok(())
103-
}
104103
}

0 commit comments

Comments
 (0)