generate_theme_code

Function generate_theme_code 

Source
pub fn generate_theme_code(
    document: &ThemeDocument,
    style_classes: &HashMap<String, StyleClass>,
    module_name: &str,
) -> Result<GeneratedCode, String>
Expand description

Generate Rust code for a theme document

This function generates a Rust module containing functions to access themes at runtime without any parsing overhead. It also generates style class functions for widget styling.

§Arguments

  • document - The parsed theme document
  • style_classes - Style class definitions from the Dampen document
  • module_name - Name for the generated module (e.g., “app” → app_theme module)

§Returns

Ok(GeneratedCode) with the generated Rust code, or an error if validation fails

§Example Output

// Generated theme code
pub fn app_theme() -> iced::Theme {
    app_default_theme()
}

pub fn app_themes() -> HashMap<&'static str, iced::Theme> {
    let mut themes = HashMap::new();
    themes.insert("light", app_theme_light());
    themes.insert("dark", app_theme_dark());
    themes
}

fn app_theme_light() -> iced::Theme {
    iced::Theme::custom(
        "light".to_string(),
        iced::theme::Palette {
            background: iced::Color::from_rgb8(0xEC, 0xF0, 0xF1),
            text: iced::Color::from_rgb8(0x2C, 0x3E, 0x50),
            primary: iced::Color::from_rgb8(0x34, 0x98, 0xDB),
            success: iced::Color::from_rgb8(0x27, 0xAE, 0x60),
            warning: iced::Color::from_rgb8(0xF3, 0x9C, 0x12),
            danger: iced::Color::from_rgb8(0xE7, 0x4C, 0x3C),
        }
    )
}

// Style class functions
pub fn style_primary_button(_theme: &iced::Theme, status: iced::widget::button::Status) -> iced::widget::button::Style {
    match status {
        iced::widget::button::Status::Active => { /* ... */ }
        iced::widget::button::Status::Hovered => { /* ... */ }
        _ => iced::widget::button::Style::default()
    }
}