mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
internal: use types to remove some unwraps
This commit is contained in:
parent
1dc337645a
commit
493ed2c17b
7 changed files with 118 additions and 87 deletions
|
@ -10,7 +10,7 @@ use anyhow::{format_err, Result};
|
|||
use la_arena::{Arena, Idx};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
|
||||
use crate::utf8_stdout;
|
||||
use crate::{utf8_stdout, ManifestPath};
|
||||
|
||||
#[derive(Default, Debug, Clone, Eq, PartialEq)]
|
||||
pub struct Sysroot {
|
||||
|
@ -22,7 +22,7 @@ pub(crate) type SysrootCrate = Idx<SysrootCrateData>;
|
|||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct SysrootCrateData {
|
||||
pub name: String,
|
||||
pub root: AbsPathBuf,
|
||||
pub root: ManifestPath,
|
||||
pub deps: Vec<SysrootCrate>,
|
||||
}
|
||||
|
||||
|
@ -48,20 +48,17 @@ impl Sysroot {
|
|||
self.crates.iter().map(|(id, _data)| id)
|
||||
}
|
||||
|
||||
pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> {
|
||||
log::debug!("Discovering sysroot for {}", cargo_toml.display());
|
||||
let current_dir = cargo_toml.parent().ok_or_else(|| {
|
||||
format_err!("Failed to find the parent directory for {}", cargo_toml.display())
|
||||
})?;
|
||||
let sysroot_dir = discover_sysroot_dir(current_dir)?;
|
||||
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir, current_dir)?;
|
||||
pub fn discover(dir: &AbsPath) -> Result<Sysroot> {
|
||||
log::debug!("Discovering sysroot for {}", dir.display());
|
||||
let sysroot_dir = discover_sysroot_dir(dir)?;
|
||||
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir, dir)?;
|
||||
let res = Sysroot::load(&sysroot_src_dir)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn discover_rustc(cargo_toml: &AbsPath) -> Option<AbsPathBuf> {
|
||||
pub fn discover_rustc(cargo_toml: &ManifestPath) -> Option<ManifestPath> {
|
||||
log::debug!("Discovering rustc source for {}", cargo_toml.display());
|
||||
let current_dir = cargo_toml.parent().unwrap();
|
||||
let current_dir = cargo_toml.parent();
|
||||
discover_sysroot_dir(current_dir).ok().and_then(|sysroot_dir| get_rustc_src(&sysroot_dir))
|
||||
}
|
||||
|
||||
|
@ -73,6 +70,7 @@ impl Sysroot {
|
|||
let root = [format!("{}/src/lib.rs", path), format!("lib{}/lib.rs", path)]
|
||||
.iter()
|
||||
.map(|it| sysroot_src_dir.join(it))
|
||||
.filter_map(|it| ManifestPath::try_from(it).ok())
|
||||
.find(|it| fs::metadata(it).is_ok());
|
||||
|
||||
if let Some(root) = root {
|
||||
|
@ -168,8 +166,9 @@ try installing the Rust source the same way you installed rustc",
|
|||
})
|
||||
}
|
||||
|
||||
fn get_rustc_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
|
||||
fn get_rustc_src(sysroot_path: &AbsPath) -> Option<ManifestPath> {
|
||||
let rustc_src = sysroot_path.join("lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml");
|
||||
let rustc_src = ManifestPath::try_from(rustc_src).ok()?;
|
||||
log::debug!("Checking for rustc source code: {}", rustc_src.display());
|
||||
if fs::metadata(&rustc_src).is_ok() {
|
||||
Some(rustc_src)
|
||||
|
@ -185,12 +184,6 @@ fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
|
|||
["library", "src"].iter().map(|it| rust_src.join(it)).find(|it| fs::metadata(it).is_ok())
|
||||
}
|
||||
|
||||
impl SysrootCrateData {
|
||||
pub fn root_dir(&self) -> &AbsPath {
|
||||
self.root.parent().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
const SYSROOT_CRATES: &str = "
|
||||
alloc
|
||||
core
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue