remove fallible trait impls from SassError

This commit is contained in:
Connor Skees 2020-07-04 11:27:57 -04:00
parent 71d3faa042
commit a823ae7811
4 changed files with 11 additions and 55 deletions

View File

@ -35,7 +35,7 @@ impl FuncArgs {
} }
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone)]
pub(crate) struct CallArgs(pub HashMap<CallArg, SassResult<Spanned<Value>>>, pub Span); pub(crate) struct CallArgs(pub HashMap<CallArg, SassResult<Spanned<Value>>>, pub Span);
#[derive(Debug, Clone, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Hash, Eq, PartialEq)]

View File

@ -2,6 +2,7 @@ use std::{
error::Error, error::Error,
fmt::{self, Display}, fmt::{self, Display},
io, io,
rc::Rc,
string::FromUtf8Error, string::FromUtf8Error,
}; };
@ -9,58 +10,11 @@ use codemap::{Span, SpanLoc};
pub type SassResult<T> = Result<T, Box<SassError>>; pub type SassResult<T> = Result<T, Box<SassError>>;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct SassError { pub struct SassError {
kind: SassErrorKind, kind: SassErrorKind,
} }
// todo: we should split the unclonable errors (io, potentially others) into
// a separate enum to allow these methods to be infallible
#[allow(clippy::unimplemented)]
impl Clone for SassError {
#[inline]
fn clone(&self) -> Self {
match &self.kind {
SassErrorKind::Raw(a, b) => SassError {
kind: SassErrorKind::Raw(a.clone(), *b),
},
SassErrorKind::ParseError { message, loc } => SassError {
kind: SassErrorKind::ParseError {
message: message.clone(),
loc: loc.clone(),
},
},
_ => unimplemented!(),
}
}
}
#[allow(clippy::unimplemented)]
impl PartialEq for SassError {
#[inline]
fn eq(&self, other: &Self) -> bool {
match &self.kind {
SassErrorKind::Raw(a, b) => match &other.kind {
SassErrorKind::Raw(c, d) => a == c && b == d,
_ => false,
},
SassErrorKind::ParseError {
message: message1,
loc: loc1,
} => match &other.kind {
SassErrorKind::ParseError {
message: message2,
loc: loc2,
} => message1 == message2 && loc1 == loc2,
_ => false,
},
_ => unimplemented!(),
}
}
}
impl Eq for SassError {}
impl SassError { impl SassError {
pub(crate) fn raw(self) -> (String, Span) { pub(crate) fn raw(self) -> (String, Span) {
match self.kind { match self.kind {
@ -76,7 +30,7 @@ impl SassError {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
enum SassErrorKind { enum SassErrorKind {
/// A raw error with no additional metadata /// A raw error with no additional metadata
/// It contains only a `String` message and /// It contains only a `String` message and
@ -86,7 +40,9 @@ enum SassErrorKind {
message: String, message: String,
loc: SpanLoc, loc: SpanLoc,
}, },
IoError(io::Error), // we put IoErrors in an `Rc` to allow it to be
// cloneable
IoError(Rc<io::Error>),
FromUtf8Error(String), FromUtf8Error(String),
} }
@ -129,7 +85,7 @@ impl From<io::Error> for Box<SassError> {
#[inline] #[inline]
fn from(error: io::Error) -> Box<SassError> { fn from(error: io::Error) -> Box<SassError> {
Box::new(SassError { Box::new(SassError {
kind: SassErrorKind::IoError(error), kind: SassErrorKind::IoError(Rc::new(error)),
}) })
} }
} }

View File

@ -14,7 +14,7 @@ use crate::{
use super::super::Parser; use super::super::Parser;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug)]
pub(crate) enum HigherIntermediateValue { pub(crate) enum HigherIntermediateValue {
Literal(Value), Literal(Value),
/// A function that hasn't yet been evaluated /// A function that hasn't yet been evaluated

View File

@ -26,7 +26,7 @@ use super::eval::{HigherIntermediateValue, ValueVisitor};
use super::super::Parser; use super::super::Parser;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug)]
enum IntermediateValue { enum IntermediateValue {
Value(HigherIntermediateValue), Value(HigherIntermediateValue),
Op(Op), Op(Op),
@ -44,7 +44,7 @@ impl IntermediateValue {
impl IsWhitespace for IntermediateValue { impl IsWhitespace for IntermediateValue {
fn is_whitespace(&self) -> bool { fn is_whitespace(&self) -> bool {
if self == &IntermediateValue::Whitespace { if let IntermediateValue::Whitespace = self {
return true; return true;
} }
false false