use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr, ItemFn, Ident, Token, parse::{Parse, ParseStream}};
use std::path::{Path, PathBuf};
use std::env;
use crate::hash::generate_hash;
use crate::style_parser::parse_and_scope;
use crate::scss_compiler::{compile_scss_to_css, is_scss_file};
use crate::class_extractor::extract_class_names;
use crate::css_constants::looks_like_scss;
use crate::codegen_utils::{generate_class_constant, generate_namespace_module, generate_include_quote};
pub struct ProcessedStyle {
pub scope_hash: String,
pub scoped_css: String,
pub class_constants: Vec<proc_macro2::TokenStream>,
pub include_path: Option<String>,
}
pub fn resolve_and_process_style(input_str: &str, span: proc_macro2::Span) -> syn::Result<ProcessedStyle> {
let is_likely_path = is_likely_file_path(input_str);
let (css_content, absolute_path_opt) = if is_likely_path {
match find_style_file(input_str) {
Ok((path, content)) => (content, Some(path)),
Err(e) => return Err(syn::Error::new(span, e)),
}
} else {
(input_str.to_string(), None)
};
let is_scss = if let Some(_) = absolute_path_opt {
is_scss_file(input_str)
} else {
looks_like_scss(&css_content)
};
let compiled_css = if is_scss {
let minify = cfg!(not(debug_assertions));
let path_arg = if absolute_path_opt.is_some() { Some(input_str) } else { None };
match compile_scss_to_css(&css_content, path_arg, minify) {
Ok(css) => css,
Err(e) => return Err(syn::Error::new(span, e)),
}
} else {
css_content
};
let path_arg_for_hash = if absolute_path_opt.is_some() { Some(input_str) } else { None };
let scope_hash = generate_hash(&compiled_css, path_arg_for_hash);
let minify = cfg!(not(debug_assertions));
let scoped_css = parse_and_scope(&compiled_css, &scope_hash, minify);
let selector_infos = extract_class_names(&compiled_css);
let class_constants: Vec<_> = selector_infos
.iter()
.map(|selector_info| generate_class_constant(selector_info, &scope_hash))
.collect();
let include_path = if let Some(path) = absolute_path_opt {
match path_to_include_str_format(&path) {
Ok(p) => Some(p),
Err(e) => return Err(syn::Error::new(span, e)),
}
} else {
None
};
Ok(ProcessedStyle {
scope_hash,
scoped_css,
class_constants,
include_path,
})
}
struct FunctionMetadata<'a> {
name: &'a Ident,
inputs: &'a syn::punctuated::Punctuated<syn::FnArg, syn::token::Comma>,
output: &'a syn::ReturnType,
vis: &'a syn::Visibility,
body: &'a syn::Block,
component_args: Option<proc_macro2::TokenStream>,
other_attrs: Vec<&'a syn::Attribute>,
}
impl<'a> FunctionMetadata<'a> {
fn from_item_fn(func: &'a ItemFn) -> Self {
let mut component_args = None;
let mut other_attrs = Vec::new();
for attr in &func.attrs {
if attr.path().is_ident("component") {
component_args = Some(if let syn::Meta::List(meta) = &attr.meta {
meta.tokens.clone()
} else {
proc_macro2::TokenStream::new()
});
} else {
other_attrs.push(attr);
}
}
Self {
name: &func.sig.ident,
inputs: &func.sig.inputs,
output: &func.sig.output,
vis: &func.vis,
body: &func.block,
component_args,
other_attrs,
}
}
fn validate_element_return(&self) -> Result<(), proc_macro2::TokenStream> {
let has_element_return = match self.output {
syn::ReturnType::Type(_, ty) => {
if let syn::Type::Path(type_path) = ty.as_ref() {
type_path.path.segments.last()
.map(|seg| seg.ident == "Element")
.unwrap_or(false)
} else {
false
}
}
_ => false,
};
if !has_element_return {
return Err(syn::Error::new_spanned(
self.output,
"Functions with CSS must return Element"
).to_compile_error());
}
Ok(())
}
fn component_attribute(&self) -> proc_macro2::TokenStream {
if let Some(ref args) = self.component_args {
if args.is_empty() {
quote! { #[::dioxus::prelude::component] }
} else {
quote! { #[::dioxus::prelude::component(#args)] }
}
} else {
quote! { #[::dioxus::prelude::component] }
}
}
}
pub struct WithCssArgs {
pub namespace: Ident,
pub _comma: Token![,],
pub css_file: LitStr,
}
impl Parse for WithCssArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(WithCssArgs {
namespace: input.parse()?,
_comma: input.parse()?,
css_file: input.parse()?,
})
}
}
pub fn with_css_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
let args = parse_macro_input!(attr as WithCssArgs);
let namespace_name = args.namespace;
let css_file_lit = args.css_file;
let func = parse_macro_input!(item as ItemFn);
let meta = FunctionMetadata::from_item_fn(&func);
if let Err(e) = meta.validate_element_return() {
return e.into();
}
let processed = match resolve_and_process_style(&css_file_lit.value(), css_file_lit.span()) {
Ok(p) => p,
Err(e) => return e.to_compile_error().into()
};
let scope_hash = processed.scope_hash;
let scoped_css = processed.scoped_css;
let class_constants = processed.class_constants;
let include_quote = generate_include_quote(processed.include_path);
let namespace_module = generate_namespace_module(
&namespace_name,
&scope_hash,
&scoped_css,
&class_constants,
&include_quote,
);
let component_attr = meta.component_attribute();
let other_attrs = &meta.other_attrs;
let fn_name = meta.name;
let fn_inputs = meta.inputs;
let fn_output = meta.output;
let fn_vis = meta.vis;
let fn_body = meta.body;
let expanded = quote! {
#component_attr
#(#other_attrs)*
#fn_vis fn #fn_name(#fn_inputs) #fn_output {
use ::dioxus::prelude::*;
#namespace_module
let _ = #namespace_name::get_scope();
let user_element = { #fn_body };
rsx! {
style { dangerous_inner_html: "{::dioxus_style::inject_styles()}" }
{user_element}
}
}
};
TokenStream::from(expanded)
}
pub struct ComponentInput {
pub style_files: Vec<(Ident, LitStr)>, pub func: ItemFn,
}
impl Parse for ComponentInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut style_files = Vec::new();
while !input.peek(Token![fn]) {
let namespace: Ident = input.parse()?;
let _: Token![:] = input.parse()?;
let css_path: LitStr = input.parse()?;
style_files.push((namespace, css_path));
if input.peek(Token![,]) {
let _: Token![,] = input.parse()?;
} else {
break; }
}
let func: ItemFn = input.parse()?;
if style_files.is_empty() {
return Err(syn::Error::new(input.span(), "Expected at least one namespace:path pair before function"));
}
Ok(ComponentInput { style_files, func })
}
}
pub fn component_with_css_impl(input: TokenStream) -> TokenStream {
let args = parse_macro_input!(input as ComponentInput);
let style_files = args.style_files;
let func = args.func;
let meta = FunctionMetadata::from_item_fn(&func);
if let Err(e) = meta.validate_element_return() {
return e.into();
}
let mut namespace_modules = Vec::new();
let mut namespace_inits = Vec::new();
for (namespace_name, css_file_lit) in style_files {
let processed = match resolve_and_process_style(&css_file_lit.value(), css_file_lit.span()) {
Ok(p) => p,
Err(e) => return e.to_compile_error().into()
};
let include_quote = generate_include_quote(processed.include_path);
let namespace_module = generate_namespace_module(
&namespace_name,
&processed.scope_hash,
&processed.scoped_css,
&processed.class_constants,
&include_quote,
);
namespace_modules.push(namespace_module);
namespace_inits.push(quote! {
let _ = #namespace_name::get_scope();
});
}
let component_attr = meta.component_attribute();
let other_attrs = &meta.other_attrs;
let fn_name = meta.name;
let fn_inputs = meta.inputs;
let fn_output = meta.output;
let fn_vis = meta.vis;
let fn_body = meta.body;
let expanded = quote! {
#component_attr
#(#other_attrs)*
#fn_vis fn #fn_name(#fn_inputs) #fn_output {
use ::dioxus::prelude::*;
#(#namespace_modules)*
#(#namespace_inits)*
let user_element = { #fn_body };
rsx! {
style { dangerous_inner_html: "{::dioxus_style::inject_styles()}" }
{user_element}
}
}
};
TokenStream::from(expanded)
}
pub fn scoped_style_impl(input: TokenStream) -> TokenStream {
enum StyleInput {
Anonymous(LitStr),
Named(Ident, LitStr),
}
impl Parse for StyleInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(Ident) && input.peek2(Token![,]) {
let name: Ident = input.parse()?;
let _: Token![,] = input.parse()?;
let path: LitStr = input.parse()?;
Ok(StyleInput::Named(name, path))
} else {
Ok(StyleInput::Anonymous(input.parse()?))
}
}
}
let parsed_input = parse_macro_input!(input as StyleInput);
let (namespace, css_lit) = match parsed_input {
StyleInput::Anonymous(lit) => (None, lit),
StyleInput::Named(ident, lit) => (Some(ident), lit),
};
let processed = match resolve_and_process_style(&css_lit.value(), css_lit.span()) {
Ok(p) => p,
Err(e) => return e.to_compile_error().into()
};
let scope_hash = processed.scope_hash;
let scoped_css = processed.scoped_css;
let class_constants = processed.class_constants;
let include_quote = generate_include_quote(processed.include_path);
if let Some(mod_name) = namespace {
let namespace_module = generate_namespace_module(
&mod_name,
&scope_hash,
&scoped_css,
&class_constants,
&include_quote,
);
let expanded = quote! {
#namespace_module
let _ = #mod_name::get_scope();
};
TokenStream::from(expanded)
} else {
let expanded = quote! {
{
use ::std::sync::OnceLock;
static STYLE_INSTANCE: OnceLock<::dioxus_style::ScopedStyle> = OnceLock::new();
*STYLE_INSTANCE.get_or_init(|| {
#include_quote
::dioxus_style::ScopedStyle::new(#scope_hash, #scoped_css)
})
}
};
TokenStream::from(expanded)
}
}
pub fn path_to_include_str_format(path: &Path) -> Result<String, String> {
let canonical = path.canonicalize()
.map_err(|e| format!("Failed to canonicalize path '{}': {}", path.display(), e))?;
let path_str = canonical.to_str()
.ok_or_else(|| format!("Path contains invalid UTF-8: {}", canonical.display()))?;
let normalized = if cfg!(windows) {
path_str.replace('\\', "/")
} else {
path_str.to_string()
};
Ok(normalized)
}
pub fn find_style_file(file_path: &str) -> Result<(PathBuf, String), String> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR")
.map_err(|_| "CARGO_MANIFEST_DIR not set (not running in cargo context)".to_string())?;
let base = PathBuf::from(&manifest_dir);
let candidates = vec![
base.join(file_path),
base.join("src").join(file_path),
base.join("styles").join(file_path),
base.join("assets").join(file_path),
base.join("src/styles").join(file_path),
base.join("src/assets").join(file_path),
];
for path in &candidates {
if path.exists() && path.is_file() {
let content = std::fs::read_to_string(path)
.map_err(|e| format!("Failed to read '{}': {}", path.display(), e))?;
return Ok((path.clone(), content));
}
}
let tried_paths = candidates.iter()
.map(|p| format!(" ✗ {}", p.display()))
.collect::<Vec<_>>()
.join("\n");
Err(format!(
"Style file '{}' not found.\nSearched in:\n{}\n",
file_path, tried_paths
))
}
fn is_likely_file_path(s: &str) -> bool {
s.ends_with(".css") || s.ends_with(".scss") || s.ends_with(".sass")
|| s.contains('/') || s.contains('\\')
}