From a823ae781133b523cd05994fc7a459289d5efc75 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sat, 4 Jul 2020 11:27:57 -0400 Subject: [PATCH] remove fallible trait impls from `SassError` --- src/args.rs | 2 +- src/error.rs | 58 +++++----------------------------------- src/parse/value/eval.rs | 2 +- src/parse/value/parse.rs | 4 +-- 4 files changed, 11 insertions(+), 55 deletions(-) diff --git a/src/args.rs b/src/args.rs index db6df85..6f32a98 100644 --- a/src/args.rs +++ b/src/args.rs @@ -35,7 +35,7 @@ impl FuncArgs { } } -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] pub(crate) struct CallArgs(pub HashMap>>, pub Span); #[derive(Debug, Clone, Hash, Eq, PartialEq)] diff --git a/src/error.rs b/src/error.rs index 93a3dcf..108de6b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,7 @@ use std::{ error::Error, fmt::{self, Display}, io, + rc::Rc, string::FromUtf8Error, }; @@ -9,58 +10,11 @@ use codemap::{Span, SpanLoc}; pub type SassResult = Result>; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SassError { 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 { pub(crate) fn raw(self) -> (String, Span) { match self.kind { @@ -76,7 +30,7 @@ impl SassError { } } -#[derive(Debug)] +#[derive(Debug, Clone)] enum SassErrorKind { /// A raw error with no additional metadata /// It contains only a `String` message and @@ -86,7 +40,9 @@ enum SassErrorKind { message: String, loc: SpanLoc, }, - IoError(io::Error), + // we put IoErrors in an `Rc` to allow it to be + // cloneable + IoError(Rc), FromUtf8Error(String), } @@ -129,7 +85,7 @@ impl From for Box { #[inline] fn from(error: io::Error) -> Box { Box::new(SassError { - kind: SassErrorKind::IoError(error), + kind: SassErrorKind::IoError(Rc::new(error)), }) } } diff --git a/src/parse/value/eval.rs b/src/parse/value/eval.rs index fae3f5c..acfba39 100644 --- a/src/parse/value/eval.rs +++ b/src/parse/value/eval.rs @@ -14,7 +14,7 @@ use crate::{ use super::super::Parser; -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug)] pub(crate) enum HigherIntermediateValue { Literal(Value), /// A function that hasn't yet been evaluated diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index 4432f4b..ef7441f 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -26,7 +26,7 @@ use super::eval::{HigherIntermediateValue, ValueVisitor}; use super::super::Parser; -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug)] enum IntermediateValue { Value(HigherIntermediateValue), Op(Op), @@ -44,7 +44,7 @@ impl IntermediateValue { impl IsWhitespace for IntermediateValue { fn is_whitespace(&self) -> bool { - if self == &IntermediateValue::Whitespace { + if let IntermediateValue::Whitespace = self { return true; } false