Skip to content

Commit 6c321d7

Browse files
committed
Move error to new file
1 parent 2202891 commit 6c321d7

File tree

2 files changed

+61
-54
lines changed

2 files changed

+61
-54
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,28 @@
33
mod cargo_workspace;
44
mod json_project;
55
mod sysroot;
6+
mod workspace_error;
67

78
use std::{
8-
fmt,
99
fs::File,
1010
io::BufReader,
1111
path::{Path, PathBuf},
1212
process::Command,
1313
};
1414

1515
use ra_cfg::CfgOptions;
16-
use ra_db::{CrateGraph, CrateId, Edition, Env, FileId, ParseEditionError};
16+
use ra_db::{CrateGraph, CrateId, Edition, Env, FileId};
1717
use rustc_hash::FxHashMap;
1818
use serde_json::from_reader;
1919

20+
use crate::workspace_error::WorkspaceError;
21+
2022
pub use crate::{
2123
cargo_workspace::{CargoFeatures, CargoWorkspace, Package, Target, TargetKind},
2224
json_project::JsonProject,
2325
sysroot::Sysroot,
2426
};
2527

26-
#[derive(Debug)]
27-
pub enum WorkspaceError {
28-
CargoMetadataFailed(cargo_metadata::Error),
29-
CargoTomlNotFound(PathBuf),
30-
NoStdLib(PathBuf),
31-
OpenWorkspaceError(std::io::Error),
32-
ParseEditionError(ParseEditionError),
33-
ReadWorkspaceError(serde_json::Error),
34-
RustcCfgError,
35-
RustcError(std::io::Error),
36-
RustcOutputError(std::string::FromUtf8Error),
37-
SysrootNotFound,
38-
}
39-
40-
impl fmt::Display for WorkspaceError {
41-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42-
match self {
43-
Self::OpenWorkspaceError(err) | Self::RustcError(err) => write!(f, "{}", err),
44-
Self::ParseEditionError(err) => write!(f, "{}", err),
45-
Self::ReadWorkspaceError(err) => write!(f, "{}", err),
46-
Self::RustcOutputError(err) => write!(f, "{}", err),
47-
Self::CargoMetadataFailed(err) => write!(f, "cargo metadata failed: {}", err),
48-
Self::RustcCfgError => write!(f, "failed to get rustc cfgs"),
49-
Self::SysrootNotFound => write!(f, "failed to locate sysroot"),
50-
Self::CargoTomlNotFound(path) => {
51-
write!(f, "can't find Cargo.toml at {}", path.display())
52-
}
53-
Self::NoStdLib(sysroot) => write!(
54-
f,
55-
"can't load standard library from sysroot\n\
56-
{:?}\n\
57-
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
58-
sysroot,
59-
),
60-
}
61-
}
62-
}
63-
64-
impl std::error::Error for WorkspaceError {}
65-
66-
impl From<ParseEditionError> for WorkspaceError {
67-
fn from(err: ParseEditionError) -> Self {
68-
Self::ParseEditionError(err.into())
69-
}
70-
}
71-
72-
impl From<cargo_metadata::Error> for WorkspaceError {
73-
fn from(err: cargo_metadata::Error) -> Self {
74-
Self::CargoMetadataFailed(err.into())
75-
}
76-
}
77-
7828
#[derive(Debug, Clone)]
7929
pub enum ProjectWorkspace {
8030
/// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! Workspace-related errors
2+
3+
use std::{error::Error, fmt, io, path::PathBuf, string::FromUtf8Error};
4+
5+
use ra_db::ParseEditionError;
6+
7+
#[derive(Debug)]
8+
pub enum WorkspaceError {
9+
CargoMetadataFailed(cargo_metadata::Error),
10+
CargoTomlNotFound(PathBuf),
11+
NoStdLib(PathBuf),
12+
OpenWorkspaceError(io::Error),
13+
ParseEditionError(ParseEditionError),
14+
ReadWorkspaceError(serde_json::Error),
15+
RustcCfgError,
16+
RustcError(io::Error),
17+
RustcOutputError(FromUtf8Error),
18+
SysrootNotFound,
19+
}
20+
21+
impl fmt::Display for WorkspaceError {
22+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23+
match self {
24+
Self::OpenWorkspaceError(err) | Self::RustcError(err) => write!(f, "{}", err),
25+
Self::ParseEditionError(err) => write!(f, "{}", err),
26+
Self::ReadWorkspaceError(err) => write!(f, "{}", err),
27+
Self::RustcOutputError(err) => write!(f, "{}", err),
28+
Self::CargoMetadataFailed(err) => write!(f, "cargo metadata failed: {}", err),
29+
Self::RustcCfgError => write!(f, "failed to get rustc cfgs"),
30+
Self::SysrootNotFound => write!(f, "failed to locate sysroot"),
31+
Self::CargoTomlNotFound(path) => {
32+
write!(f, "can't find Cargo.toml at {}", path.display())
33+
}
34+
Self::NoStdLib(sysroot) => write!(
35+
f,
36+
"can't load standard library from sysroot\n\
37+
{:?}\n\
38+
try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
39+
sysroot,
40+
),
41+
}
42+
}
43+
}
44+
45+
impl From<ParseEditionError> for WorkspaceError {
46+
fn from(err: ParseEditionError) -> Self {
47+
Self::ParseEditionError(err.into())
48+
}
49+
}
50+
51+
impl From<cargo_metadata::Error> for WorkspaceError {
52+
fn from(err: cargo_metadata::Error) -> Self {
53+
Self::CargoMetadataFailed(err.into())
54+
}
55+
}
56+
57+
impl Error for WorkspaceError {}

0 commit comments

Comments
 (0)