From 95230c0df99cb3395806c56ef4c04729f2ef26c2 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sat, 3 Sep 2022 19:23:07 -0400 Subject: [PATCH] expose error kind --- src/args.rs | 5 ----- src/error.rs | 41 +++++++++++++++++++++++++++++++++++------ src/lib.rs | 5 +++-- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/args.rs b/src/args.rs index b44b0a1..04641d5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -207,11 +207,6 @@ impl CallArgs { self.get_positional(position) } - #[allow(dead_code, clippy::unused_self)] - fn named_arg(&mut self, name: &'static str) -> Option>> { - self.get_named(name) - } - pub fn default_named_arg(&mut self, name: &'static str, default: Value) -> SassResult { Ok(match self.get_named(name) { Some(val) => val?.node, diff --git a/src/error.rs b/src/error.rs index 478d287..17a947b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,8 +2,8 @@ use std::{ error::Error, fmt::{self, Display}, io, - rc::Rc, string::FromUtf8Error, + sync::Arc, }; use codemap::{Span, SpanLoc}; @@ -37,6 +37,24 @@ pub struct SassError { } impl SassError { + #[must_use] + pub fn kind(self) -> PublicSassErrorKind { + match self.kind { + SassErrorKind::ParseError { + message, + loc, + unicode, + } => PublicSassErrorKind::ParseError { + message, + loc, + unicode, + }, + SassErrorKind::FromUtf8Error(s) => PublicSassErrorKind::FromUtf8Error(s), + SassErrorKind::IoError(io) => PublicSassErrorKind::IoError(io), + SassErrorKind::Raw(..) => unreachable!("raw errors should not be accessible by users"), + } + } + pub(crate) fn raw(self) -> (String, Span) { match self.kind { SassErrorKind::Raw(string, span) => (string, span), @@ -55,6 +73,18 @@ impl SassError { } } +#[non_exhaustive] +#[derive(Debug, Clone)] +pub enum PublicSassErrorKind { + ParseError { + message: String, + loc: SpanLoc, + unicode: bool, + }, + IoError(Arc), + FromUtf8Error(String), +} + #[derive(Debug, Clone)] enum SassErrorKind { /// A raw error with no additional metadata @@ -66,9 +96,8 @@ enum SassErrorKind { loc: SpanLoc, unicode: bool, }, - // we put IoErrors in an `Rc` to allow it to be - // cloneable - IoError(Rc), + // we put `IoError`s in an `Arc` to allow them to be cloneable + IoError(Arc), FromUtf8Error(String), } @@ -86,7 +115,7 @@ impl Display for SassError { } => (message, loc, *unicode), SassErrorKind::FromUtf8Error(s) => return writeln!(f, "Error: {}", s), SassErrorKind::IoError(s) => return writeln!(f, "Error: {}", s), - SassErrorKind::Raw(..) => todo!(), + SassErrorKind::Raw(..) => unreachable!(), }; let first_bar = if unicode { '╷' } else { '|' }; @@ -128,7 +157,7 @@ impl From for Box { #[inline] fn from(error: io::Error) -> Box { Box::new(SassError { - kind: SassErrorKind::IoError(Rc::new(error)), + kind: SassErrorKind::IoError(Arc::new(error)), }) } } diff --git a/src/lib.rs b/src/lib.rs index df3709f..ec4314a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,6 @@ grass input.scss // this is only available on nightly clippy::unnested_or_patterns, )] -#![cfg_attr(feature = "nightly", feature(track_caller))] #![cfg_attr(feature = "profiling", inline(never))] use std::path::Path; @@ -70,7 +69,9 @@ pub(crate) use beef::lean::Cow; use codemap::CodeMap; -pub use crate::error::{SassError as Error, SassResult as Result}; +pub use crate::error::{ + PublicSassErrorKind as ErrorKind, SassError as Error, SassResult as Result, +}; pub use crate::fs::{Fs, NullFs, StdFs}; pub(crate) use crate::token::Token; use crate::{