2020-01-06 18:26:07 -05:00
|
|
|
use crate::common::Pos;
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::{self, Display};
|
|
|
|
use std::io;
|
2020-01-20 12:13:52 -05:00
|
|
|
use std::string::FromUtf8Error;
|
2020-01-04 22:55:04 -05:00
|
|
|
|
2020-02-16 10:08:45 -05:00
|
|
|
pub type SassResult<T> = Result<T, SassError>;
|
|
|
|
|
2020-01-06 18:26:07 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SassError {
|
|
|
|
message: String,
|
2020-01-06 19:23:52 -05:00
|
|
|
pos: Pos,
|
2020-01-06 18:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SassError {
|
|
|
|
pub fn new<S: Into<String>>(message: S, pos: Pos) -> Self {
|
2020-01-06 19:23:52 -05:00
|
|
|
SassError {
|
|
|
|
message: message.into(),
|
|
|
|
pos,
|
|
|
|
}
|
2020-01-06 18:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for SassError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-02-16 11:10:03 -05:00
|
|
|
write!(f, "Error: {}", self.message)
|
2020-01-06 18:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for SassError {
|
|
|
|
fn from(error: io::Error) -> Self {
|
2020-01-06 19:23:52 -05:00
|
|
|
SassError {
|
|
|
|
pos: Pos::new(),
|
|
|
|
message: format!("{}", error),
|
|
|
|
}
|
2020-01-06 18:26:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:55:21 -05:00
|
|
|
impl From<std::fmt::Error> for SassError {
|
|
|
|
fn from(error: std::fmt::Error) -> Self {
|
|
|
|
SassError {
|
|
|
|
pos: Pos::new(),
|
|
|
|
message: format!("{}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:13:52 -05:00
|
|
|
impl From<FromUtf8Error> for SassError {
|
|
|
|
fn from(error: FromUtf8Error) -> Self {
|
|
|
|
SassError {
|
|
|
|
pos: Pos::new(),
|
|
|
|
message: format!("Invalid UTF-8 character \"\\x{:X?}\"", error.as_bytes()[0]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SassError> for String {
|
2020-01-20 12:17:07 -05:00
|
|
|
#[inline]
|
2020-01-20 12:13:52 -05:00
|
|
|
fn from(error: SassError) -> String {
|
|
|
|
error.message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 10:54:25 -05:00
|
|
|
impl From<&str> for SassError {
|
|
|
|
#[inline]
|
|
|
|
fn from(error: &str) -> SassError {
|
|
|
|
SassError {
|
|
|
|
pos: Pos::new(),
|
|
|
|
message: error.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for SassError {
|
|
|
|
#[inline]
|
|
|
|
fn from(error: String) -> SassError {
|
|
|
|
SassError {
|
|
|
|
pos: Pos::new(),
|
|
|
|
message: error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:26:07 -05:00
|
|
|
impl Error for SassError {
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
"SASS parsing error"
|
|
|
|
}
|
|
|
|
}
|