use std::fmt;
use lsp_server::ErrorCode;
pub struct LSPError {
pub code: ErrorCode,
pub error: anyhow::Error,
}
pub type LSPResult<T> = Result<T, LSPError>;
pub trait LSPResultEx<T> {
fn with_failure_code(self, code: ErrorCode) -> Result<T, LSPError>;
}
impl<T, E: Into<anyhow::Error>> LSPResultEx<T> for Result<T, E> {
fn with_failure_code(self, code: ErrorCode) -> Result<T, LSPError> {
self.map_err(|error| LSPError::new(error.into(), code))
}
}
impl LSPError {
pub fn new(error: anyhow::Error, code: ErrorCode) -> Self {
Self { code, error }
}
}
impl fmt::Debug for LSPError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}
impl fmt::Display for LSPError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}