refactor: use single unified syntax shim type instead of many

This commit is contained in:
Yiffy Dev 2024-11-02 01:52:22 +05:00 • committed by spushk
commit 7702862071
Signed by: spushk
GPG key ID: 7797B761E105E8AA
9 changed files with 102 additions and 92 deletions

View file

@ -186,7 +186,7 @@ impl SabryBuilder {
}
let css = self.css_compiler.compile_module(
scope.original_scope.adapter().syntax.into(),
scope.original_scope.adapter().syntax,
&scope.hashed_code,
)?;
self.state

View file

@ -7,7 +7,7 @@ use lightningcss::{
targets::Targets,
};
use crate::config::SabryConfig;
use crate::{config::SabryConfig, syntax::ostrta::OneSyntaxToRuleThemAll};
/// Convenience wrapper on Grass and Lightningcss
pub struct CompilerAdapter {
@ -21,7 +21,7 @@ impl CompilerAdapter {
pub fn compile_module(
&self,
syntax: CompilerSyntax,
syntax: OneSyntaxToRuleThemAll,
code: &str,
) -> Result<String, SabryCompilerError> {
let options = grass::Options::from(&self.config).input_syntax(syntax.into());
@ -131,30 +131,3 @@ impl Debug for SabryCompilerError {
write!(f, "{explain}")
}
}
/// Unification wrapper over all the Syntax enums
#[derive(Clone, Copy)]
pub enum CompilerSyntax {
Sass,
Scss,
}
impl From<raffia::Syntax> for CompilerSyntax {
fn from(value: raffia::Syntax) -> Self {
match value {
raffia::Syntax::Sass => Self::Sass,
raffia::Syntax::Scss => Self::Scss,
// TODO: fallbacky error-prone
_ => Self::Scss,
}
}
}
impl From<CompilerSyntax> for grass::InputSyntax {
fn from(value: CompilerSyntax) -> Self {
match value {
CompilerSyntax::Scss => Self::Scss,
CompilerSyntax::Sass => Self::Sass,
}
}
}

View file

@ -69,7 +69,7 @@ impl ScopeHash {
mod test {
use syn::Ident;
use crate::{config::SabryHashConfig, scoper::ArbitraryScope};
use crate::{config::SabryHashConfig, scoper::ArbitraryScope, syntax::ostrta::OneSyntaxToRuleThemAll};
use super::ScopeHash;
@ -78,7 +78,7 @@ mod test {
let source = ".cls1{color:red; &-dark{color: black} #id1 {color:green;} div {color:blue;}} .cls3#id2{color: black;}";
let scope1 = ArbitraryScope::from_source(
raffia::Syntax::Scss,
OneSyntaxToRuleThemAll::Scss,
syn::parse_str::<Ident>("lasifudm").unwrap(),
source,
)
@ -86,7 +86,7 @@ mod test {
let hash1 = ScopeHash::new(&scope1, &SabryHashConfig::default());
let scope2 = ArbitraryScope::from_source(
raffia::Syntax::Scss,
OneSyntaxToRuleThemAll::Scss,
syn::parse_str::<Ident>("lasifudm").unwrap(),
source,
)

View file

@ -5,7 +5,7 @@ use hash::ScopeHash;
use raffia::Spanned;
use regex::Regex;
use crate::{config::SabryHashConfig, syntax::StylesheetAdapter};
use crate::{config::SabryHashConfig, syntax::{ostrta::OneSyntaxToRuleThemAll, StylesheetAdapter}};
pub mod hash;
@ -35,7 +35,7 @@ pub struct ArbitraryScope<'s> {
impl<'s> ArbitraryScope<'s> {
/// Parse given source code with given syntax and assign given name
pub fn from_source(
syntax: raffia::Syntax,
syntax: OneSyntaxToRuleThemAll,
name: syn::Ident,
source: &'s str,
) -> Result<Self, ScopeError> {
@ -375,7 +375,7 @@ mod test {
use crate::{
config::SabryHashConfig,
scoper::{hash::ScopeHash, HashedScope},
scoper::{hash::ScopeHash, HashedScope}, syntax::ostrta::OneSyntaxToRuleThemAll,
};
use super::ArbitraryScope;
@ -415,7 +415,7 @@ mod test {
}
let scope = ArbitraryScope::from_source(
raffia::Syntax::Scss,
OneSyntaxToRuleThemAll::Scss,
syn::parse_str("scope1").unwrap(),
code,
)
@ -463,7 +463,7 @@ mod test {
let hash_cfg = SabryHashConfig::default();
let scope = ArbitraryScope::from_source(
raffia::Syntax::Scss,
OneSyntaxToRuleThemAll::Scss,
syn::parse_str::<Ident>("scope2oiej").unwrap(),
code,
)

View file

@ -1,24 +1,30 @@
use ostrta::OneSyntaxToRuleThemAll;
use raffia::{
ast::{
ClassSelector, CompoundSelector, IdSelector, NestingSelector, SimpleSelector, Statement,
Stylesheet, TypeSelector,
},
ParserBuilder, Syntax,
ParserBuilder,
};
pub mod ostrta;
/// Convenience wrapper for [Stylesheet]
pub struct StylesheetAdapter<'s> {
pub syntax: Syntax,
pub syntax: OneSyntaxToRuleThemAll,
source: &'s str,
stylesheet: Stylesheet<'s>,
}
impl<'s> StylesheetAdapter<'s> {
pub fn new(source: &'s str, syntax: Syntax) -> Result<Self, raffia::error::Error> {
pub fn new(
source: &'s str,
syntax: OneSyntaxToRuleThemAll,
) -> Result<Self, raffia::error::Error> {
Ok(Self {
stylesheet: ParserBuilder::new(source)
.ignore_comments()
.syntax(syntax)
.syntax(syntax.into())
.build()
.parse()?,
syntax,
@ -103,6 +109,8 @@ mod test {
use raffia::Spanned;
use crate::syntax::ostrta::OneSyntaxToRuleThemAll;
use super::StylesheetAdapter;
#[test]
@ -157,7 +165,7 @@ div#id2
let expect_tags = HashSet::from(["ul", "div", "span"]);
//scss
let adp = StylesheetAdapter::new(source_scss, raffia::Syntax::Scss).unwrap();
let adp = StylesheetAdapter::new(source_scss, OneSyntaxToRuleThemAll::Scss).unwrap();
let classes = adp
.class_selectors()
@ -183,7 +191,7 @@ div#id2
assert_eq!(expect_tags, tags);
//sass
let adp = StylesheetAdapter::new(source_sass, raffia::Syntax::Sass).unwrap();
let adp = StylesheetAdapter::new(source_sass, OneSyntaxToRuleThemAll::Sass).unwrap();
let classes = adp
.class_selectors()
@ -215,7 +223,7 @@ div#id2
let newname = "cls203-jg7ihgjhftyfhjh";
let expect = ".cls1{color:red; .cls203-jg7ihgjhftyfhjh{color:green;}}";
let adp = StylesheetAdapter::new(scss, raffia::Syntax::Scss).unwrap();
let adp = StylesheetAdapter::new(scss, OneSyntaxToRuleThemAll::Scss).unwrap();
let classes = adp.class_selectors();
let target_class = classes[0].clone();

View file

@ -0,0 +1,64 @@
/// Convenience unification entrypoint
/// to make friends with all the different syntax-consumers
#[derive(Debug, Clone, Copy)]
pub enum OneSyntaxToRuleThemAll {
Sass,
Scss
}
impl Default for OneSyntaxToRuleThemAll {
fn default() -> Self {
Self::Scss
}
}
impl TryFrom<&str> for OneSyntaxToRuleThemAll {
type Error = ();
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"sass" => Ok(Self::Sass),
"scss" => Ok(Self::Scss),
_ => Err(())
}
}
}
impl TryFrom<raffia::Syntax> for OneSyntaxToRuleThemAll {
type Error = ();
fn try_from(value: raffia::Syntax) -> Result<Self, Self::Error> {
match value {
raffia::Syntax::Sass => Ok(Self::Sass),
raffia::Syntax::Scss => Ok(Self::Scss),
_ => Err(())
}
}
}
impl TryFrom<grass::InputSyntax> for OneSyntaxToRuleThemAll {
type Error = ();
fn try_from(value: grass::InputSyntax) -> Result<Self, Self::Error> {
match value {
grass::InputSyntax::Sass => Ok(Self::Sass),
grass::InputSyntax::Scss => Ok(Self::Scss),
_ => Err(())
}
}
}
impl From<OneSyntaxToRuleThemAll> for raffia::Syntax {
fn from(value: OneSyntaxToRuleThemAll) -> Self {
match value {
OneSyntaxToRuleThemAll::Sass => Self::Sass,
OneSyntaxToRuleThemAll::Scss => Self::Scss
}
}
}
impl From<OneSyntaxToRuleThemAll> for grass::InputSyntax {
fn from(value: OneSyntaxToRuleThemAll) -> Self {
match value {
OneSyntaxToRuleThemAll::Scss => Self::Scss,
OneSyntaxToRuleThemAll::Sass => Self::Sass
}
}
}

View file

@ -3,7 +3,7 @@ use std::{fmt::Debug, fs, path::PathBuf, str::FromStr};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens, TokenStreamExt};
use regex::Regex;
use sabry_intrnl::compiler::CompilerSyntax;
use sabry_intrnl::syntax::ostrta::OneSyntaxToRuleThemAll;
use syn::{braced, parse::Parse, Ident, LitStr, Token};
pub mod sassy;
@ -93,26 +93,20 @@ impl ToString for ArbitraryStyleBlock {
}
}
#[derive(Clone, Copy)]
pub enum ArbitraryStyleSyntax {
Scss,
Sass,
}
#[derive(Default, Debug, Clone, Copy)]
pub struct ArbitraryStyleSyntax(OneSyntaxToRuleThemAll);
impl From<ArbitraryStyleSyntax> for CompilerSyntax {
impl From<ArbitraryStyleSyntax> for OneSyntaxToRuleThemAll {
fn from(value: ArbitraryStyleSyntax) -> Self {
match value {
ArbitraryStyleSyntax::Sass => Self::Sass,
ArbitraryStyleSyntax::Scss => Self::Scss,
}
value.0
}
}
impl ToTokens for ArbitraryStyleSyntax {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Sass => tokens.append_all(quote! {"sass"}),
Self::Scss => tokens.append_all(quote! {"scss"}),
match self.0 {
OneSyntaxToRuleThemAll::Sass => tokens.append_all(quote! {"sass"}),
OneSyntaxToRuleThemAll::Scss => tokens.append_all(quote! {"scss"}),
}
}
}
@ -120,11 +114,7 @@ impl ToTokens for ArbitraryStyleSyntax {
impl TryFrom<&str> for ArbitraryStyleSyntax {
type Error = ();
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"sass" => Ok(Self::Sass),
"scss" => Ok(Self::Scss),
_ => Err(())
}
Ok(Self(OneSyntaxToRuleThemAll::try_from(value)?))
}
}
@ -152,31 +142,6 @@ impl Parse for ArbitraryStyleSyntax {
}
}
impl Debug for ArbitraryStyleSyntax {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
Self::Sass => "sass",
Self::Scss => "scss",
};
write!(f, "{name}")
}
}
impl Default for ArbitraryStyleSyntax {
fn default() -> Self {
Self::Scss
}
}
impl From<ArbitraryStyleSyntax> for raffia::Syntax {
fn from(value: ArbitraryStyleSyntax) -> Self {
match value {
ArbitraryStyleSyntax::Sass => Self::Sass,
ArbitraryStyleSyntax::Scss => Self::Scss,
}
}
}
#[cfg(test)]
mod test {
#[test]

View file

@ -22,7 +22,7 @@ pub fn sassy_macro_impl(input: TokenStream) -> TokenStream {
};
// quick raffia syntax check
match ArbitraryScope::from_source(mode.into(), ident.clone(), &code.code) {
match ArbitraryScope::from_source(mode.0, ident.clone(), &code.code) {
Ok(_) => {}
Err(e) => return syn::Error::new(code.span, format!("{e:?}")).into_compile_error(),
}

View file

@ -45,7 +45,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
Err(e) => return e.to_compile_error(),
};
let scope = match ArbitraryScope::from_source(ms.syntax.into(), ms.scope.clone(), &ms.code.code)
let scope = match ArbitraryScope::from_source(ms.syntax.0, ms.scope.clone(), &ms.code.code)
{
Ok(s) => match s.hashed(&config.hash) {
Ok(hs) => Ok(hs),
@ -134,7 +134,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
if constant {
let compiler = CompilerAdapter::new(config.clone());
let css = match compiler.compile_module(ms.syntax.into(), &ms.code.code) {
let css = match compiler.compile_module(ms.syntax.0, &ms.code.code) {
Ok(c) => c,
Err(e) => {
return syn::Error::new(