nushell/src/errors.rs

140 lines
3.8 KiB
Rust
Raw Normal View History

use crate::parser::lexer::{Span, SpannedToken};
2019-05-16 00:58:44 +02:00
#[allow(unused)]
use crate::prelude::*;
2019-05-10 18:59:12 +02:00
use derive_new::new;
use language_reporting::Diagnostic;
use serde::{Serialize, Serializer};
use serde_derive::Serialize;
2019-05-10 18:59:12 +02:00
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Serialize)]
pub enum ShellError {
String(StringError),
Diagnostic(ShellDiagnostic, String),
}
impl ShellError {
crate fn parse_error(
error: lalrpop_util::ParseError<usize, SpannedToken, ShellError>,
source: String,
) -> ShellError {
use lalrpop_util::ParseError;
use language_reporting::*;
match error {
ParseError::UnrecognizedToken {
token: (start, SpannedToken { token, .. }, end),
expected,
} => {
let diagnostic = Diagnostic::new(
Severity::Error,
format!("Unexpected {:?}, expected {:?}", token, expected),
)
.with_label(Label::new_primary(Span::from((start, end))));
ShellError::diagnostic(diagnostic, source)
}
other => ShellError::string(format!("{:?}", other)),
}
}
crate fn diagnostic(diagnostic: Diagnostic<Span>, source: String) -> ShellError {
ShellError::Diagnostic(ShellDiagnostic { diagnostic }, source)
}
crate fn string(title: impl Into<String>) -> ShellError {
ShellError::String(StringError::new(title.into(), Value::nothing()))
}
crate fn copy_error(&self) -> ShellError {
self.clone()
}
}
2019-05-16 23:43:36 +02:00
#[derive(Debug, Clone)]
pub struct ShellDiagnostic {
crate diagnostic: Diagnostic<Span>,
}
impl PartialEq for ShellDiagnostic {
fn eq(&self, _other: &ShellDiagnostic) -> bool {
false
2019-05-16 23:43:36 +02:00
}
2019-05-10 18:59:12 +02:00
}
impl Eq for ShellDiagnostic {}
impl std::cmp::PartialOrd for ShellDiagnostic {
fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
Some(std::cmp::Ordering::Less)
}
}
impl std::cmp::Ord for ShellDiagnostic {
fn cmp(&self, _other: &Self) -> std::cmp::Ordering {
std::cmp::Ordering::Less
}
}
impl Serialize for ShellDiagnostic {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
"<diagnostic>".serialize(serializer)
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, new, Clone, Serialize)]
pub struct StringError {
title: String,
error: Value,
}
2019-05-10 18:59:12 +02:00
impl std::fmt::Display for ShellError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ShellError::String(s) => write!(f, "{}", &s.title),
ShellError::Diagnostic(_, _) => write!(f, "<diagnostic>"),
}
2019-05-10 18:59:12 +02:00
}
}
impl std::error::Error for ShellError {}
impl std::convert::From<std::io::Error> for ShellError {
fn from(input: std::io::Error) -> ShellError {
ShellError::String(StringError {
2019-05-10 18:59:12 +02:00
title: format!("{}", input),
error: Value::nothing(),
})
2019-05-10 18:59:12 +02:00
}
}
2019-05-24 06:34:43 +02:00
impl std::convert::From<futures_sink::VecSinkError> for ShellError {
fn from(_input: futures_sink::VecSinkError) -> ShellError {
ShellError::String(StringError {
2019-05-24 06:34:43 +02:00
title: format!("Unexpected Vec Sink Error"),
error: Value::nothing(),
})
2019-05-24 06:34:43 +02:00
}
}
2019-05-24 09:29:16 +02:00
impl std::convert::From<subprocess::PopenError> for ShellError {
fn from(input: subprocess::PopenError) -> ShellError {
ShellError::String(StringError {
2019-05-24 09:29:16 +02:00
title: format!("{}", input),
error: Value::nothing(),
})
2019-05-24 09:29:16 +02:00
}
}
2019-05-26 08:54:41 +02:00
impl std::convert::From<nom::Err<(&str, nom::error::ErrorKind)>> for ShellError {
fn from(input: nom::Err<(&str, nom::error::ErrorKind)>) -> ShellError {
ShellError::String(StringError {
2019-05-26 08:54:41 +02:00
title: format!("{:?}", input),
error: Value::nothing(),
})
2019-05-26 08:54:41 +02:00
}
}