-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
53 lines (41 loc) · 1.43 KB
/
error.rs
File metadata and controls
53 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::io;
use std::{path::PathBuf, result};
use thiserror::Error;
use crate::util::request_failed;
#[derive(Error, Debug)]
pub enum EarthlylsError {
#[error("{glob}: {source}")]
GlobPattern { source: glob::PatternError, glob: String },
#[error(transparent)]
Glob(#[from] glob::GlobError),
// #[error("IO error: {0}")]
// Io(#[from] std::io::Error),
#[error("{path}: {source}")]
PathIo { path: PathBuf, source: io::Error },
#[error("Can't convert path {path} to URL")]
PathToUrl { path: PathBuf },
}
impl From<EarthlylsError> for tower_lsp::jsonrpc::Error {
fn from(val: EarthlylsError) -> Self {
request_failed(&val.to_string())
}
}
/// Alias for a `Result` with the error type `AppError`.
pub type Result<T> = result::Result<T, EarthlylsError>;
pub trait IOResultExt<T> {
fn path_ctx<P: Into<PathBuf>>(self, path: P) -> Result<T>;
}
impl<T> IOResultExt<T> for io::Result<T> {
fn path_ctx<P: Into<PathBuf>>(self, path: P) -> Result<T> {
self.map_err(|source| EarthlylsError::PathIo { source, path: path.into() })
}
}
/// Extension trait for glob Result.
pub trait GlobResultExt<T> {
fn glob_ctx<S: Into<String>>(self, glob: S) -> Result<T>;
}
impl<T> GlobResultExt<T> for result::Result<T, glob::PatternError> {
fn glob_ctx<S: Into<String>>(self, glob: S) -> Result<T> {
self.map_err(|source| EarthlylsError::GlobPattern { source, glob: glob.into() })
}
}