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
3 changed files with 102 additions and 92 deletions

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(