icss 0.1.1

A CSS-like theme engine, generative design system, and widget library for iced 0.14.
Documentation
//! Tailwind CSS preset generation — maps ICSS semantic tokens to Tailwind color palettes.
//!
//! Generates a `tailwind.preset.js` that overrides the default Tailwind palettes
//! (slate, blue, green, red, amber, purple) with values from the ICSS design system.
//! This allows the admin web app to use standard Tailwind classes (`bg-slate-900`,
//! `text-blue-400`, etc.) while drawing colors from the same theme as the desktop app.

use crate::engine::dims::DimTokens;
use crate::engine::semantic::SemanticColors;

/// Generate a Tailwind CSS preset (ES module) from resolved semantic colors and dimensions.
///
/// The output is a valid JavaScript file that can be imported in `tailwind.config.js`:
/// ```js
/// import preset from './tailwind.preset.js';
/// export default { presets: [preset], content: [...] };
/// ```
pub fn compose_tailwind_preset(colors: &SemanticColors, dims: &DimTokens) -> String {
    let mut out = String::with_capacity(4096);

    out.push_str("// Generated by icss — do not edit manually.\n");
    out.push_str("// Re-generate with: ./scripts/gen-tailwind.sh\n\n");
    out.push_str("/** @type {import('tailwindcss').Config} */\n");
    out.push_str("export default {\n");
    out.push_str("  theme: {\n");
    out.push_str("    extend: {\n");

    // ── Colors ──
    out.push_str("      colors: {\n");

    // slate → neutral surface family + outline + text hierarchy
    out.push_str("        slate: {\n");
    emit_color(&mut out, "300", &colors.surface.text_default); // text-slate-300
    emit_color(&mut out, "400", &colors.surface.text_soft); // text-slate-400
    emit_color(&mut out, "500", &colors.surface.text_dim); // text-slate-500
    emit_color(&mut out, "600", &colors.outline_middle); // border-slate-600, bg-slate-600
    emit_color(&mut out, "700", &colors.outline_soft); // border-slate-700, bg-slate-700
    emit_color(&mut out, "800", &colors.surface.s2); // bg-slate-800
    emit_color(&mut out, "900", &colors.surface.s1); // bg-slate-900
    emit_color(&mut out, "950", &colors.surface.s0); // bg-slate-950
    out.push_str("        },\n");

    // blue → primary family
    out.push_str("        blue: {\n");
    emit_color(&mut out, "300", &colors.on_surface_link); // text-blue-300
    emit_color(&mut out, "400", &colors.on_surface_primary); // text-blue-400
    emit_color(&mut out, "500", &colors.on_surface_primary); // border-blue-500
    emit_color(&mut out, "600", &colors.primary.s0); // bg-blue-600
    emit_color(&mut out, "700", &colors.primary.s2); // bg-blue-700 (hover)
    emit_color(&mut out, "800", &colors.primary.s3); // bg-blue-800
    out.push_str("        },\n");

    // green → success family
    out.push_str("        green: {\n");
    emit_color(&mut out, "400", &colors.on_surface_success); // text-green-400, bg-green-400
    emit_color(&mut out, "500", &colors.success.s0); // bg-green-500/10
    out.push_str("        },\n");

    // red → danger family
    out.push_str("        red: {\n");
    emit_color(&mut out, "400", &colors.on_surface_danger); // text-red-400
    emit_color(&mut out, "500", &colors.danger.s0); // bg-red-500/10, border-red-500/30
    out.push_str("        },\n");

    // amber → warning family
    out.push_str("        amber: {\n");
    emit_color(&mut out, "400", &colors.on_surface_warning); // text-amber-400
    emit_color(&mut out, "500", &colors.warning.s0); // bg-amber-500/10
    out.push_str("        },\n");

    // purple → quaternary family
    out.push_str("        purple: {\n");
    emit_color(&mut out, "400", &colors.on_surface_quaternary); // text-purple-400
    emit_color(&mut out, "500", &colors.quaternary.s0); // bg-purple-500/10
    out.push_str("        },\n");

    // white → highest contrast text on neutral surface
    out.push_str(&format!("        white: '{}',\n", colors.surface.text));

    out.push_str("      },\n");

    // ── Border radius ──
    out.push_str("      borderRadius: {\n");
    emit_px(&mut out, "lg", dims.radius_100); // rounded-lg
    emit_px(&mut out, "xl", dims.radius_150); // rounded-xl
    emit_px(&mut out, "'full'", dims.radius_infinite); // rounded-full
    out.push_str("      },\n");

    out.push_str("    },\n"); // close extend
    out.push_str("  },\n"); // close theme
    out.push_str("};\n");

    out
}

fn emit_color(out: &mut String, key: &str, hex: &str) {
    // Handle rgba() values — Tailwind needs hex or rgb, so pass through as-is
    out.push_str(&format!("          {key}: '{hex}',\n"));
}

fn emit_px(out: &mut String, key: &str, val: f32) {
    let v = val as i32;
    out.push_str(&format!("        {key}: '{v}px',\n"));
}