2019-05-30 16:52:30 -07:00
|
|
|
use std::fmt::Write;
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::io;
|
2019-02-19 11:24:50 -08:00
|
|
|
use tree_sitter_highlight::PropertySheetError;
|
2019-01-07 17:57:27 -08:00
|
|
|
|
2018-12-05 12:50:12 -08:00
|
|
|
#[derive(Debug)]
|
2019-05-30 16:52:30 -07:00
|
|
|
pub struct Error(pub Vec<String>);
|
2018-12-05 12:50:12 -08:00
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
2018-12-08 13:44:11 -08:00
|
|
|
impl Error {
|
|
|
|
|
pub fn grammar(message: &str) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error(vec![format!("Grammar error: {}", message)])
|
2018-12-08 13:44:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn regex(message: &str) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error(vec![format!("Regex error: {}", message)])
|
2019-01-04 12:42:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn undefined_symbol(name: &str) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error(vec![format!("Undefined symbol `{}`", name)])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new(message: String) -> Self {
|
|
|
|
|
Error(vec![message])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn err<T>(message: String) -> Result<T> {
|
|
|
|
|
Err(Error::new(message))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn wrap<E: Into<Self>, M: ToString, F: FnOnce() -> M>(
|
|
|
|
|
message_fn: F,
|
|
|
|
|
) -> impl FnOnce(E) -> Self {
|
|
|
|
|
|e| {
|
|
|
|
|
let mut result = e.into();
|
|
|
|
|
result.0.push(message_fn().to_string());
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn message(&self) -> String {
|
|
|
|
|
let mut result = self.0.last().unwrap().clone();
|
|
|
|
|
if self.0.len() > 1 {
|
|
|
|
|
result.push_str("\nDetails:\n");
|
|
|
|
|
for msg in self.0[0..self.0.len() - 1].iter().rev() {
|
|
|
|
|
writeln!(&mut result, " {}", msg).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2018-12-08 13:44:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 12:50:12 -08:00
|
|
|
impl From<serde_json::Error> for Error {
|
|
|
|
|
fn from(error: serde_json::Error) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error::new(error.to_string())
|
2018-12-05 12:50:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
|
fn from(error: io::Error) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error::new(error.to_string())
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 18:09:55 -08:00
|
|
|
impl From<rsass::Error> for Error {
|
|
|
|
|
fn from(error: rsass::Error) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error::new(error.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<regex_syntax::ast::Error> for Error {
|
|
|
|
|
fn from(error: regex_syntax::ast::Error) -> Self {
|
|
|
|
|
Error::new(error.to_string())
|
2019-01-09 18:09:55 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
impl From<String> for Error {
|
|
|
|
|
fn from(error: String) -> Self {
|
2019-05-30 16:52:30 -07:00
|
|
|
Error::new(error)
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-19 11:24:50 -08:00
|
|
|
|
|
|
|
|
impl From<PropertySheetError> for Error {
|
|
|
|
|
fn from(error: PropertySheetError) -> Self {
|
|
|
|
|
match error {
|
|
|
|
|
PropertySheetError::InvalidFormat(e) => Self::from(e),
|
|
|
|
|
PropertySheetError::InvalidRegex(e) => Self::regex(&e.to_string()),
|
|
|
|
|
PropertySheetError::InvalidJSON(e) => Self::from(e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|