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 c514a38d35
Signed by: spushk
GPG key ID: 7797B761E105E8AA
5 changed files with 102 additions and 92 deletions

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
}
}
}