Skip to main content

azul_css/codegen/
mod.rs

1//! Multi-language code generation backends for parsed CSS.
2//!
3//! The kernel of a future "compile UI to standalone project" feature exposed by
4//! the `azul-dll` debug HTTP server: a parsed [`Css`](crate::css::Css) is fed to
5//! a [`CodegenBackend`] implementation, which returns a complete source string
6//! plus auxiliary files (e.g. `Cargo.toml`) for a target language.
7//!
8//! Today only [`rust::RustBackend`] is implemented; [`cpp`] and [`python`] hold
9//! stub backends so the public API stays stable as new languages come online.
10
11use alloc::{string::String, vec::Vec};
12
13use crate::css::Css;
14
15pub mod cpp;
16pub mod python;
17pub mod rust;
18
19/// One emitted source artifact (e.g. `src/main.rs`, `Cargo.toml`).
20pub struct GeneratedFile {
21    pub path: String,
22    pub contents: String,
23}
24
25/// Pluggable code-generation strategy. Each backend turns a parsed [`Css`] into
26/// a list of files that, taken together, form a buildable standalone project.
27pub trait CodegenBackend {
28    /// Stable identifier (e.g. `"rust"`) used by the HTTP layer to pick a backend.
29    fn lang(&self) -> &'static str;
30
31    /// Render a single CSS expression. Useful for tests / quick previews where
32    /// the caller doesn't want a full project layout.
33    fn emit_css(&self, css: &Css) -> String;
34
35    /// Emit a complete standalone project. The returned files are root-relative.
36    fn emit_project(&self, css: &Css) -> Vec<GeneratedFile>;
37}
38
39/// Look up a backend by its [`CodegenBackend::lang`] identifier.
40pub fn backend_for(lang: &str) -> Option<alloc::boxed::Box<dyn CodegenBackend>> {
41    match lang {
42        "rust" => Some(alloc::boxed::Box::new(rust::RustBackend)),
43        "cpp" | "c++" => Some(alloc::boxed::Box::new(cpp::CppBackend)),
44        "python" | "py" => Some(alloc::boxed::Box::new(python::PythonBackend)),
45        _ => None,
46    }
47}