From 6eb97255835ecb6dc3a53663c6f0a91a7e602063 Mon Sep 17 00:00:00 2001 From: Yiffy Dev Date: Sat, 2 Nov 2024 00:20:10 +0500 Subject: [PATCH] fix: more-or-less readable errors + syntax check on sassy macro --- examples/define-styles/Cargo.lock | 8 ++-- examples/use-styles/Cargo.lock | 10 ++-- sabry_build/src/buildmagic.rs | 2 +- sabry_intrnl/src/compiler/mod.rs | 61 +++++++++++++++++++++---- sabry_intrnl/src/scoper/mod.rs | 26 ++++++++--- sabry_procmacro_impl/src/impls/sassy.rs | 10 +++- 6 files changed, 91 insertions(+), 26 deletions(-) diff --git a/examples/define-styles/Cargo.lock b/examples/define-styles/Cargo.lock index 5c7cbf1..1b2178a 100644 --- a/examples/define-styles/Cargo.lock +++ b/examples/define-styles/Cargo.lock @@ -994,7 +994,7 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "sabry" -version = "0.0.1-alpha.2" +version = "0.0.1-beta.2" dependencies = [ "sabry_intrnl", "sabry_procmacro", @@ -1002,7 +1002,7 @@ dependencies = [ [[package]] name = "sabry_intrnl" -version = "0.0.1-alpha.2" +version = "0.0.1-beta.2" dependencies = [ "base64", "blake3", @@ -1022,14 +1022,14 @@ dependencies = [ [[package]] name = "sabry_procmacro" -version = "0.0.1-alpha.2" +version = "0.0.1-beta.2" dependencies = [ "sabry_procmacro_impl", ] [[package]] name = "sabry_procmacro_impl" -version = "0.0.1-alpha.2" +version = "0.0.1-beta.2" dependencies = [ "proc-macro2", "quote", diff --git a/examples/use-styles/Cargo.lock b/examples/use-styles/Cargo.lock index b5c9a3f..a75ee98 100644 --- a/examples/use-styles/Cargo.lock +++ b/examples/use-styles/Cargo.lock @@ -994,7 +994,7 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "sabry" -version = "0.0.1-beta.1" +version = "0.0.1-beta.2" dependencies = [ "sabry_build", "sabry_intrnl", @@ -1003,7 +1003,7 @@ dependencies = [ [[package]] name = "sabry_build" -version = "0.0.1-beta.1" +version = "0.0.1-beta.2" dependencies = [ "proc-macro2", "sabry_intrnl", @@ -1015,7 +1015,7 @@ dependencies = [ [[package]] name = "sabry_intrnl" -version = "0.0.1-beta.1" +version = "0.0.1-beta.2" dependencies = [ "base64", "blake3", @@ -1035,14 +1035,14 @@ dependencies = [ [[package]] name = "sabry_procmacro" -version = "0.0.1-beta.1" +version = "0.0.1-beta.2" dependencies = [ "sabry_procmacro_impl", ] [[package]] name = "sabry_procmacro_impl" -version = "0.0.1-beta.1" +version = "0.0.1-beta.2" dependencies = [ "proc-macro2", "quote", diff --git a/sabry_build/src/buildmagic.rs b/sabry_build/src/buildmagic.rs index bcff0ef..fe3043c 100644 --- a/sabry_build/src/buildmagic.rs +++ b/sabry_build/src/buildmagic.rs @@ -331,7 +331,7 @@ pub enum SabryBuildError { #[error("Sabry cant understand the file")] FileVisit(#[from] FileVisitError), #[error("Syntax of style can not be parsed")] - SyntaxParse(#[from] ScopeError), + SyntaxError(#[from] ScopeError), #[error("Another scope has the same hash, and sabry is configured to raise an error. Try to adjust config, increase hash size, or change the style code")] HashCollision { scope: ModuleName }, #[error("Something's wrong with file path")] diff --git a/sabry_intrnl/src/compiler/mod.rs b/sabry_intrnl/src/compiler/mod.rs index 7b20556..7376497 100644 --- a/sabry_intrnl/src/compiler/mod.rs +++ b/sabry_intrnl/src/compiler/mod.rs @@ -1,4 +1,11 @@ -use lightningcss::{printer::PrinterOptions, stylesheet::MinifyOptions, targets::Targets}; +use std::fmt::Debug; + +use lightningcss::{ + error::{MinifyErrorKind, PrinterErrorKind}, + printer::PrinterOptions, + stylesheet::MinifyOptions, + targets::Targets, +}; use crate::config::SabryConfig; @@ -28,7 +35,12 @@ impl CompilerAdapter { lightningcss::stylesheet::ParserOptions::default(), ) { Ok(s) => s, - Err(_) => return Err(SabryCompilerError::Lightningcss()), + Err(e) => { + return Err(SabryCompilerError::LightParse { + kind: e.kind.to_string(), + loc: e.loc, + }) + } }; let targets = Targets { @@ -47,16 +59,14 @@ impl CompilerAdapter { ..Default::default() }; - if lightsheet.minify(minify_options).is_err() { - return Err(SabryCompilerError::Lightningcss()); - } + lightsheet.minify(minify_options)?; css = match lightsheet.to_css(printer_options) { Ok(c) => { drop(lightsheet); c.code } - Err(_) => return Err(SabryCompilerError::Lightningcss()), + Err(e) => return Err(SabryCompilerError::LightPrint(e)), }; } @@ -78,12 +88,45 @@ impl From<&SabryConfig> for grass::Options<'_> { } } -#[derive(Debug, thiserror::Error)] +#[derive(thiserror::Error)] pub enum SabryCompilerError { #[error("Could not compile into CSS with grass")] GrassCompile(#[from] Box), - #[error("Could not parse compiled CSS with lightningcss")] - Lightningcss(), + #[error("Could not print compiled CSS with lightningcss")] + LightPrint(#[from] lightningcss::error::Error), + #[error("Could not minify CSS with lightningcss")] + LightMinify(#[from] lightningcss::error::Error), + #[error("Could not parse CSS with lightningcss")] + LightParse { + kind: String, + loc: Option, + }, +} + +impl Debug for SabryCompilerError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let explain = match self { + Self::LightPrint(e) => format!( + "{} at {:?}", + e.kind, + e.loc.clone() + .map(|l| format!("file {}, line {}, col {}", l.filename, l.line, l.column)) + ), + Self::GrassCompile(err) => format!("{err}"), + Self::LightMinify(e) => format!( + "{} at {:?}", + e.kind, + e.loc.clone() + .map(|l| format!("file {}, line {}, col {}", l.filename, l.line, l.column)) + ), + Self::LightParse { kind, loc } => format!( + "{kind} at {:?}", + loc.clone().map(|l| format!("file {}, line {}, col {}", l.filename, l.line, l.column)) + ), + }; + + write!(f, "{explain}") + } } /// Unification wrapper over all the Syntax enums diff --git a/sabry_intrnl/src/scoper/mod.rs b/sabry_intrnl/src/scoper/mod.rs index b519cff..a42949c 100644 --- a/sabry_intrnl/src/scoper/mod.rs +++ b/sabry_intrnl/src/scoper/mod.rs @@ -1,3 +1,5 @@ +use std::fmt::Debug; + use cfg_if::cfg_if; use hash::ScopeHash; use raffia::Spanned; @@ -7,15 +9,19 @@ use crate::{config::SabryHashConfig, syntax::StylesheetAdapter}; pub mod hash; -#[derive(Debug, thiserror::Error)] +#[derive(thiserror::Error)] pub enum ScopeError { #[error("Raffia reports parse error")] - Raffia(raffia::error::ErrorKind), + Raffia(raffia::error::ErrorKind, String), } -impl From for ScopeError { - fn from(value: raffia::error::Error) -> Self { - Self::Raffia(value.kind) +impl Debug for ScopeError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let a = match self { + Self::Raffia(kind, source) => format!("{kind}: at {source}") + }; + + write!(f, "{a}") } } @@ -33,8 +39,16 @@ impl<'s> ArbitraryScope<'s> { name: syn::Ident, source: &'s str, ) -> Result { + let adapter = match StylesheetAdapter::new(source, syntax) { + Ok(a) => a, + Err(e) => { + let source = &source[e.span.start..e.span.end]; + return Err(ScopeError::Raffia(e.kind, source.to_string())) + } + }; + Ok(Self { - adapter: StylesheetAdapter::new(source, syntax)?, + adapter, name, }) } diff --git a/sabry_procmacro_impl/src/impls/sassy.rs b/sabry_procmacro_impl/src/impls/sassy.rs index 42d1725..b05d03f 100644 --- a/sabry_procmacro_impl/src/impls/sassy.rs +++ b/sabry_procmacro_impl/src/impls/sassy.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; use quote::quote; -use regex::Regex; +use sabry_intrnl::scoper::ArbitraryScope; use syn::{parse::Parse, Ident}; use super::{ArbitraryStyleBlock, ArbitraryStyleSyntax}; @@ -21,6 +21,14 @@ pub fn sassy_macro_impl(input: TokenStream) -> TokenStream { Err(e) => return e.to_compile_error(), }; + // quick raffia syntax check + match ArbitraryScope::from_source(mode.into(), ident.clone(), &code.code) { + Ok(_) => {}, + Err(e) => { + return syn::Error::new(code.span, format!("{e:?}")).into_compile_error() + } + } + let sourcesass = code.to_string(); let identname = ident.to_string(); let macro_doc = format!("Arbitrary {:?} code declared with `sassy!`. Pretty usable in tandem with `usey!` and `magic()` at build time", mode);