use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};
use crate::hash::generate_hash;
use crate::style_parser::parse_and_scope;
use crate::scss_compiler::{compile_scss_to_css, is_scss_file};
pub fn scoped_style_impl(input: TokenStream) -> TokenStream {
let input_str = parse_macro_input!(input as LitStr);
let css_content = input_str.value();
if is_likely_file_path(&css_content) {
let file_path = css_content.clone();
let possible_paths = [
file_path.clone(), format!("../{}", file_path), format!("../../{}", file_path), format!("src/{}", file_path), ];
let (actual_path, raw_file_content) = match possible_paths.iter().find_map(|path| {
std::fs::read_to_string(path)
.ok()
.map(|content| (path.clone(), content))
}) {
Some(result) => result,
None => {
let error = format!(
"Failed to find CSS/SCSS file '{}'. Tried paths:\n{}",
file_path,
possible_paths
.iter()
.map(|p| format!(" - {}", p))
.collect::<Vec<_>>()
.join("\n")
);
return syn::Error::new(input_str.span(), error)
.to_compile_error()
.into();
}
};
eprintln!("✅ Found file at: {}", actual_path);
let css_content = if is_scss_file(&file_path) {
eprintln!("🎨 Compiling SCSS: {}", file_path);
let minify = cfg!(not(debug_assertions));
match compile_scss_to_css(&raw_file_content, Some(&file_path), minify) {
Ok(css) => {
eprintln!("✅ SCSS compiled successfully ({} bytes)", css.len());
css
}
Err(e) => {
return syn::Error::new(input_str.span(), e)
.to_compile_error()
.into();
}
}
} else {
eprintln!("📄 Using plain CSS: {}", file_path);
raw_file_content
};
let scope_lit = generate_hash(&css_content, Some(&file_path));
let minify = cfg!(not(debug_assertions));
let scoped = parse_and_scope(&css_content, &scope_lit, minify);
let scoped_css = scoped.scoped;
let include_path = &file_path;
let file_type = if is_scss_file(&file_path) { "SCSS" } else { "CSS" };
let expanded = quote! {
{
::dioxus_style::lazy_static! {
static ref STYLE_INSTANCE: ::dioxus_style::ScopedStyle = {
let _css_tracker = include_str!(#include_path);
let scope = #scope_lit.to_string();
let css = #scoped_css.to_string();
eprintln!("🚀 [STATIC INIT] Loaded {} from file: {}", #file_type, #file_path);
eprintln!("🎯 [STATIC INIT] Scope: {}, CSS length: {}", scope, css.len());
::dioxus_style::ScopedStyle::new(scope, css)
};
}
STYLE_INSTANCE.clone()
}
};
TokenStream::from(expanded)
} else {
eprintln!("📝 Using inline CSS (not a file path)");
let css_content = if looks_like_scss(&css_content) {
eprintln!("🎨 Compiling inline SCSS");
let minify = cfg!(not(debug_assertions));
match compile_scss_to_css(&css_content, None, minify) {
Ok(css) => {
eprintln!("✅ Inline SCSS compiled successfully");
css
}
Err(e) => {
return syn::Error::new(input_str.span(), e)
.to_compile_error()
.into();
}
}
} else {
css_content
};
let scope = generate_hash(&css_content, None);
let minify = cfg!(not(debug_assertions));
let scoped = parse_and_scope(&css_content, &scope, minify);
let scoped_css = scoped.scoped;
let expanded = quote! {
{
::dioxus_style::lazy_static! {
static ref STYLE_INSTANCE: ::dioxus_style::ScopedStyle = {
let scope = #scope.to_string();
let css = #scoped_css.to_string();
::dioxus_style::ScopedStyle::new(scope, css)
};
}
STYLE_INSTANCE.clone()
}
};
TokenStream::from(expanded)
}
}
pub fn css_impl(input: TokenStream) -> TokenStream {
let input_str = parse_macro_input!(input as LitStr);
let css_content = input_str.value();
let scope = generate_hash(&css_content, None);
let wrapped_css = format!(".{} {{ {} }}", scope, css_content);
let minify = cfg!(not(debug_assertions));
let final_css = if minify {
crate::style_parser::parse_and_scope(&wrapped_css, &scope, true).scoped
} else {
wrapped_css
};
let expanded = quote! {
{
::dioxus_style::lazy_static! {
static ref STYLE_INSTANCE: ::dioxus_style::ScopedStyle = {
let scope = #scope.to_string();
let css = #final_css.to_string();
::dioxus_style::ScopedStyle::new(scope, css)
};
}
STYLE_INSTANCE.clone()
}
};
TokenStream::from(expanded)
}
fn is_likely_file_path(s: &str) -> bool {
s.ends_with(".css") || s.ends_with(".scss") || s.ends_with(".sass")
|| s.contains('/') || s.contains('\\')
}
fn looks_like_scss(content: &str) -> bool {
content.contains('$') || content.contains("@mixin") || content.contains("@include") || content.contains("@import") || content.contains("@use") || content.contains("@extend") || has_nesting(content) }
fn has_nesting(content: &str) -> bool {
let mut brace_depth = 0;
let mut in_selector = false;
for line in content.lines() {
let trimmed = line.trim();
if trimmed.contains('{') {
brace_depth += 1;
in_selector = true;
}
if trimmed.contains('}') {
brace_depth -= 1;
}
if brace_depth > 0 && in_selector && !trimmed.is_empty()
&& !trimmed.contains(':') && !trimmed.contains('}')
&& !trimmed.starts_with("//") && !trimmed.starts_with("/*") {
if trimmed.starts_with('.') || trimmed.starts_with('#')
|| trimmed.starts_with('&') || trimmed.chars().next().map_or(false, |c| c.is_alphabetic()) {
return true;
}
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_likely_file_path() {
assert!(is_likely_file_path("button.css"));
assert!(is_likely_file_path("button.scss"));
assert!(is_likely_file_path("button.sass"));
assert!(is_likely_file_path("styles/button.css"));
assert!(is_likely_file_path("./button.scss"));
assert!(!is_likely_file_path(".button { color: red; }"));
assert!(!is_likely_file_path("color: red; font-size: 16px;"));
}
#[test]
fn test_looks_like_scss() {
assert!(looks_like_scss("$primary: blue;"));
assert!(looks_like_scss("@mixin flex { display: flex; }"));
assert!(looks_like_scss("@include flex;"));
assert!(looks_like_scss(".parent { .child { } }"));
assert!(looks_like_scss("&:hover { }"));
assert!(!looks_like_scss(".button { color: red; }"));
assert!(!looks_like_scss("div { margin: 0; }"));
}
#[test]
fn test_has_nesting() {
let nested = r#"
.parent {
color: red;
.child {
color: blue;
}
}
"#;
assert!(has_nesting(nested));
let not_nested = r#"
.button {
color: red;
padding: 10px;
}
"#;
assert!(!has_nesting(not_nested));
}
}