From 26ae7f38b45925e5c99798990382453ed77c9b36 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 6 Jan 2020 18:26:07 -0500 Subject: [PATCH] Implement basic error handling --- src/error.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/error.rs b/src/error.rs index 8b13789..69ae29b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1 +1,38 @@ +use crate::common::Pos; +use std::error::Error; +use std::fmt::{self, Display}; +use std::io; +#[derive(Debug)] +pub struct SassError { + message: String, + pos: Pos +} + +impl SassError { + pub fn new>(message: S, pos: Pos) -> Self { + SassError { message: message.into(), pos } + } + + pub fn unexpected_eof(pos: Pos) -> Self { + SassError { message: String::from("unexpected eof"), 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 Error for SassError { + fn description(&self) -> &'static str { + "SASS parsing error" + } +}