dioxus_style_macro 0.3.0

Procedural macros for dioxus_style with SCSS/SASS compilation support - internal use only
Documentation
//! Procedural macro implementations for scoped styling with SCSS support.

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

/// Implementation of the `scoped_style!` macro with SCSS support.
pub fn scoped_style_impl(input: TokenStream) -> TokenStream {
    let input_str = parse_macro_input!(input as LitStr);
    let css_content = input_str.value();

    // Determine if this is a file path or inline CSS
    if is_likely_file_path(&css_content) {
        let file_path = css_content.clone();

        // Try to find the CSS/SCSS file in common locations
        let possible_paths = [
            file_path.clone(),              // As specified
            format!("../{}", file_path),    // Parent directory
            format!("../../{}", file_path), // Two levels up
            format!("src/{}", file_path),   // In src
        ];

        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);

        // **NEW: Check if it's SCSS and compile if needed**
        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
        };

        // Generate hash with both file path and content
        let scope_lit = generate_hash(&css_content, Some(&file_path));
        
        // Scope the CSS (now already compiled from SCSS if needed)
        let minify = cfg!(not(debug_assertions));
        let scoped = parse_and_scope(&css_content, &scope_lit, minify);
        let scoped_css = scoped.scoped;

        // Use the original file_path for include_str! (not actual_path)
        let include_path = &file_path;

        // Generate code that uses include_str! at compile time
        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 = {
                        // include_str! runs at compile time and tracks the file for rebuilds
                        let _css_tracker = include_str!(#include_path);

                        // Use pre-processed and scoped CSS
                        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 {
        // Inline CSS/SCSS - process at compile time
        eprintln!("📝 Using inline CSS (not a file path)");

        // **NEW: Check if inline content looks like SCSS**
        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)
    }
}

/// Implementation of the `css!` macro for inline styles.
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)
}

/// Checks if a string looks like a file path.
fn is_likely_file_path(s: &str) -> bool {
    s.ends_with(".css") || s.ends_with(".scss") || s.ends_with(".sass") 
        || s.contains('/') || s.contains('\\')
}

/// Checks if inline content looks like SCSS (has SCSS-specific syntax).
fn looks_like_scss(content: &str) -> bool {
    // Check for common SCSS features
    content.contains('$')           // Variables
        || content.contains("@mixin")   // Mixins
        || content.contains("@include") // Includes
        || content.contains("@import")  // Imports
        || content.contains("@use")     // Use statements
        || content.contains("@extend")  // Extends
        || has_nesting(content)         // Nesting
}

/// Detects nested selectors (basic heuristic).
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 we see another selector declaration while inside braces, it's nested
        if brace_depth > 0 && in_selector && !trimmed.is_empty() 
            && !trimmed.contains(':') && !trimmed.contains('}') 
            && !trimmed.starts_with("//") && !trimmed.starts_with("/*") {
            
            // Check if it looks like a selector (starts with . # & or letter)
            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() {
        // Should detect 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 { }"));

        // Should NOT detect SCSS
        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));
    }
}