use crate::common::Pos; use std::error::Error; use std::fmt::{self, Display}; use std::io; use std::string::FromUtf8Error; pub type SassResult = Result; #[derive(Debug)] pub struct SassError { message: String, pos: Pos, } impl SassError { pub fn new>(message: S, pos: Pos) -> Self { SassError { message: message.into(), pos, } } } impl Display for SassError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "error: {} at {}", self.message, self.pos) } } impl From for SassError { fn from(error: io::Error) -> Self { SassError { pos: Pos::new(), message: format!("{}", error), } } } impl From for SassError { fn from(error: std::fmt::Error) -> Self { SassError { pos: Pos::new(), message: format!("{}", error), } } } impl From 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 for String { #[inline] fn from(error: SassError) -> String { error.message } } impl From<&str> for SassError { #[inline] fn from(error: &str) -> SassError { SassError { pos: Pos::new(), message: error.to_string(), } } } impl From for SassError { #[inline] fn from(error: String) -> SassError { SassError { pos: Pos::new(), message: error, } } } impl Error for SassError { fn description(&self) -> &'static str { "SASS parsing error" } }