refactor: use single unified syntax shim type instead of many
This commit is contained in:
parent
dc3c3bac11
commit
c514a38d35
9 changed files with 102 additions and 92 deletions
|
|
@ -186,7 +186,7 @@ impl SabryBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
let css = self.css_compiler.compile_module(
|
let css = self.css_compiler.compile_module(
|
||||||
scope.original_scope.adapter().syntax.into(),
|
scope.original_scope.adapter().syntax,
|
||||||
&scope.hashed_code,
|
&scope.hashed_code,
|
||||||
)?;
|
)?;
|
||||||
self.state
|
self.state
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use lightningcss::{
|
||||||
targets::Targets,
|
targets::Targets,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::config::SabryConfig;
|
use crate::{config::SabryConfig, syntax::ostrta::OneSyntaxToRuleThemAll};
|
||||||
|
|
||||||
/// Convenience wrapper on Grass and Lightningcss
|
/// Convenience wrapper on Grass and Lightningcss
|
||||||
pub struct CompilerAdapter {
|
pub struct CompilerAdapter {
|
||||||
|
|
@ -21,7 +21,7 @@ impl CompilerAdapter {
|
||||||
|
|
||||||
pub fn compile_module(
|
pub fn compile_module(
|
||||||
&self,
|
&self,
|
||||||
syntax: CompilerSyntax,
|
syntax: OneSyntaxToRuleThemAll,
|
||||||
code: &str,
|
code: &str,
|
||||||
) -> Result<String, SabryCompilerError> {
|
) -> Result<String, SabryCompilerError> {
|
||||||
let options = grass::Options::from(&self.config).input_syntax(syntax.into());
|
let options = grass::Options::from(&self.config).input_syntax(syntax.into());
|
||||||
|
|
@ -131,30 +131,3 @@ impl Debug for SabryCompilerError {
|
||||||
write!(f, "{explain}")
|
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ impl ScopeHash {
|
||||||
mod test {
|
mod test {
|
||||||
use syn::Ident;
|
use syn::Ident;
|
||||||
|
|
||||||
use crate::{config::SabryHashConfig, scoper::ArbitraryScope};
|
use crate::{config::SabryHashConfig, scoper::ArbitraryScope, syntax::ostrta::OneSyntaxToRuleThemAll};
|
||||||
|
|
||||||
use super::ScopeHash;
|
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 source = ".cls1{color:red; &-dark{color: black} #id1 {color:green;} div {color:blue;}} .cls3#id2{color: black;}";
|
||||||
|
|
||||||
let scope1 = ArbitraryScope::from_source(
|
let scope1 = ArbitraryScope::from_source(
|
||||||
raffia::Syntax::Scss,
|
OneSyntaxToRuleThemAll::Scss,
|
||||||
syn::parse_str::<Ident>("lasifudm").unwrap(),
|
syn::parse_str::<Ident>("lasifudm").unwrap(),
|
||||||
source,
|
source,
|
||||||
)
|
)
|
||||||
|
|
@ -86,7 +86,7 @@ mod test {
|
||||||
let hash1 = ScopeHash::new(&scope1, &SabryHashConfig::default());
|
let hash1 = ScopeHash::new(&scope1, &SabryHashConfig::default());
|
||||||
|
|
||||||
let scope2 = ArbitraryScope::from_source(
|
let scope2 = ArbitraryScope::from_source(
|
||||||
raffia::Syntax::Scss,
|
OneSyntaxToRuleThemAll::Scss,
|
||||||
syn::parse_str::<Ident>("lasifudm").unwrap(),
|
syn::parse_str::<Ident>("lasifudm").unwrap(),
|
||||||
source,
|
source,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use hash::ScopeHash;
|
||||||
use raffia::Spanned;
|
use raffia::Spanned;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use crate::{config::SabryHashConfig, syntax::StylesheetAdapter};
|
use crate::{config::SabryHashConfig, syntax::{ostrta::OneSyntaxToRuleThemAll, StylesheetAdapter}};
|
||||||
|
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
|
|
||||||
|
|
@ -35,7 +35,7 @@ pub struct ArbitraryScope<'s> {
|
||||||
impl<'s> ArbitraryScope<'s> {
|
impl<'s> ArbitraryScope<'s> {
|
||||||
/// Parse given source code with given syntax and assign given name
|
/// Parse given source code with given syntax and assign given name
|
||||||
pub fn from_source(
|
pub fn from_source(
|
||||||
syntax: raffia::Syntax,
|
syntax: OneSyntaxToRuleThemAll,
|
||||||
name: syn::Ident,
|
name: syn::Ident,
|
||||||
source: &'s str,
|
source: &'s str,
|
||||||
) -> Result<Self, ScopeError> {
|
) -> Result<Self, ScopeError> {
|
||||||
|
|
@ -375,7 +375,7 @@ mod test {
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::SabryHashConfig,
|
config::SabryHashConfig,
|
||||||
scoper::{hash::ScopeHash, HashedScope},
|
scoper::{hash::ScopeHash, HashedScope}, syntax::ostrta::OneSyntaxToRuleThemAll,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::ArbitraryScope;
|
use super::ArbitraryScope;
|
||||||
|
|
@ -415,7 +415,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
let scope = ArbitraryScope::from_source(
|
let scope = ArbitraryScope::from_source(
|
||||||
raffia::Syntax::Scss,
|
OneSyntaxToRuleThemAll::Scss,
|
||||||
syn::parse_str("scope1").unwrap(),
|
syn::parse_str("scope1").unwrap(),
|
||||||
code,
|
code,
|
||||||
)
|
)
|
||||||
|
|
@ -463,7 +463,7 @@ mod test {
|
||||||
|
|
||||||
let hash_cfg = SabryHashConfig::default();
|
let hash_cfg = SabryHashConfig::default();
|
||||||
let scope = ArbitraryScope::from_source(
|
let scope = ArbitraryScope::from_source(
|
||||||
raffia::Syntax::Scss,
|
OneSyntaxToRuleThemAll::Scss,
|
||||||
syn::parse_str::<Ident>("scope2oiej").unwrap(),
|
syn::parse_str::<Ident>("scope2oiej").unwrap(),
|
||||||
code,
|
code,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,30 @@
|
||||||
|
use ostrta::OneSyntaxToRuleThemAll;
|
||||||
use raffia::{
|
use raffia::{
|
||||||
ast::{
|
ast::{
|
||||||
ClassSelector, CompoundSelector, IdSelector, NestingSelector, SimpleSelector, Statement,
|
ClassSelector, CompoundSelector, IdSelector, NestingSelector, SimpleSelector, Statement,
|
||||||
Stylesheet, TypeSelector,
|
Stylesheet, TypeSelector,
|
||||||
},
|
},
|
||||||
ParserBuilder, Syntax,
|
ParserBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub mod ostrta;
|
||||||
|
|
||||||
/// Convenience wrapper for [Stylesheet]
|
/// Convenience wrapper for [Stylesheet]
|
||||||
pub struct StylesheetAdapter<'s> {
|
pub struct StylesheetAdapter<'s> {
|
||||||
pub syntax: Syntax,
|
pub syntax: OneSyntaxToRuleThemAll,
|
||||||
source: &'s str,
|
source: &'s str,
|
||||||
stylesheet: Stylesheet<'s>,
|
stylesheet: Stylesheet<'s>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> StylesheetAdapter<'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 {
|
Ok(Self {
|
||||||
stylesheet: ParserBuilder::new(source)
|
stylesheet: ParserBuilder::new(source)
|
||||||
.ignore_comments()
|
.ignore_comments()
|
||||||
.syntax(syntax)
|
.syntax(syntax.into())
|
||||||
.build()
|
.build()
|
||||||
.parse()?,
|
.parse()?,
|
||||||
syntax,
|
syntax,
|
||||||
|
|
@ -103,6 +109,8 @@ mod test {
|
||||||
|
|
||||||
use raffia::Spanned;
|
use raffia::Spanned;
|
||||||
|
|
||||||
|
use crate::syntax::ostrta::OneSyntaxToRuleThemAll;
|
||||||
|
|
||||||
use super::StylesheetAdapter;
|
use super::StylesheetAdapter;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -157,7 +165,7 @@ div#id2
|
||||||
let expect_tags = HashSet::from(["ul", "div", "span"]);
|
let expect_tags = HashSet::from(["ul", "div", "span"]);
|
||||||
|
|
||||||
//scss
|
//scss
|
||||||
let adp = StylesheetAdapter::new(source_scss, raffia::Syntax::Scss).unwrap();
|
let adp = StylesheetAdapter::new(source_scss, OneSyntaxToRuleThemAll::Scss).unwrap();
|
||||||
|
|
||||||
let classes = adp
|
let classes = adp
|
||||||
.class_selectors()
|
.class_selectors()
|
||||||
|
|
@ -183,7 +191,7 @@ div#id2
|
||||||
assert_eq!(expect_tags, tags);
|
assert_eq!(expect_tags, tags);
|
||||||
|
|
||||||
//sass
|
//sass
|
||||||
let adp = StylesheetAdapter::new(source_sass, raffia::Syntax::Sass).unwrap();
|
let adp = StylesheetAdapter::new(source_sass, OneSyntaxToRuleThemAll::Sass).unwrap();
|
||||||
|
|
||||||
let classes = adp
|
let classes = adp
|
||||||
.class_selectors()
|
.class_selectors()
|
||||||
|
|
@ -215,7 +223,7 @@ div#id2
|
||||||
let newname = "cls203-jg7ihgjhftyfhjh";
|
let newname = "cls203-jg7ihgjhftyfhjh";
|
||||||
let expect = ".cls1{color:red; .cls203-jg7ihgjhftyfhjh{color:green;}}";
|
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 classes = adp.class_selectors();
|
||||||
let target_class = classes[0].clone();
|
let target_class = classes[0].clone();
|
||||||
|
|
||||||
|
|
|
||||||
64
sabry_intrnl/src/syntax/ostrta.rs
Normal file
64
sabry_intrnl/src/syntax/ostrta.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,7 @@ use std::{fmt::Debug, fs, path::PathBuf, str::FromStr};
|
||||||
use proc_macro2::{Span, TokenStream};
|
use proc_macro2::{Span, TokenStream};
|
||||||
use quote::{quote, ToTokens, TokenStreamExt};
|
use quote::{quote, ToTokens, TokenStreamExt};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use sabry_intrnl::compiler::CompilerSyntax;
|
use sabry_intrnl::syntax::ostrta::OneSyntaxToRuleThemAll;
|
||||||
use syn::{braced, parse::Parse, Ident, LitStr, Token};
|
use syn::{braced, parse::Parse, Ident, LitStr, Token};
|
||||||
|
|
||||||
pub mod sassy;
|
pub mod sassy;
|
||||||
|
|
@ -93,26 +93,20 @@ impl ToString for ArbitraryStyleBlock {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Default, Debug, Clone, Copy)]
|
||||||
pub enum ArbitraryStyleSyntax {
|
pub struct ArbitraryStyleSyntax(OneSyntaxToRuleThemAll);
|
||||||
Scss,
|
|
||||||
Sass,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ArbitraryStyleSyntax> for CompilerSyntax {
|
impl From<ArbitraryStyleSyntax> for OneSyntaxToRuleThemAll {
|
||||||
fn from(value: ArbitraryStyleSyntax) -> Self {
|
fn from(value: ArbitraryStyleSyntax) -> Self {
|
||||||
match value {
|
value.0
|
||||||
ArbitraryStyleSyntax::Sass => Self::Sass,
|
|
||||||
ArbitraryStyleSyntax::Scss => Self::Scss,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToTokens for ArbitraryStyleSyntax {
|
impl ToTokens for ArbitraryStyleSyntax {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
match self {
|
match self.0 {
|
||||||
Self::Sass => tokens.append_all(quote! {"sass"}),
|
OneSyntaxToRuleThemAll::Sass => tokens.append_all(quote! {"sass"}),
|
||||||
Self::Scss => tokens.append_all(quote! {"scss"}),
|
OneSyntaxToRuleThemAll::Scss => tokens.append_all(quote! {"scss"}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -120,11 +114,7 @@ impl ToTokens for ArbitraryStyleSyntax {
|
||||||
impl TryFrom<&str> for ArbitraryStyleSyntax {
|
impl TryFrom<&str> for ArbitraryStyleSyntax {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||||
match value {
|
Ok(Self(OneSyntaxToRuleThemAll::try_from(value)?))
|
||||||
"sass" => Ok(Self::Sass),
|
|
||||||
"scss" => Ok(Self::Scss),
|
|
||||||
_ => Err(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ pub fn sassy_macro_impl(input: TokenStream) -> TokenStream {
|
||||||
};
|
};
|
||||||
|
|
||||||
// quick raffia syntax check
|
// 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(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => return syn::Error::new(code.span, format!("{e:?}")).into_compile_error(),
|
Err(e) => return syn::Error::new(code.span, format!("{e:?}")).into_compile_error(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
|
||||||
Err(e) => return e.to_compile_error(),
|
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(s) => match s.hashed(&config.hash) {
|
||||||
Ok(hs) => Ok(hs),
|
Ok(hs) => Ok(hs),
|
||||||
|
|
@ -134,7 +134,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
if constant {
|
if constant {
|
||||||
let compiler = CompilerAdapter::new(config.clone());
|
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,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return syn::Error::new(
|
return syn::Error::new(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue