grass/src/error.rs

67 lines
1.4 KiB
Rust
Raw Normal View History

2020-01-06 18:26:07 -05:00
use crate::common::Pos;
use std::error::Error;
use std::fmt::{self, Display};
use std::io;
use std::string::FromUtf8Error;
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 {
write!(f, "error: {} at {}", self.message, self.pos)
}
}
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
}
}
impl From<std::fmt::Error> for SassError {
fn from(error: std::fmt::Error) -> Self {
SassError {
pos: Pos::new(),
message: format!("{}", error),
}
}
}
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]
fn from(error: SassError) -> String {
error.message
}
}
2020-01-06 18:26:07 -05:00
impl Error for SassError {
fn description(&self) -> &'static str {
"SASS parsing error"
}
}