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 format;
17pub mod python;
18pub mod rust;
19
20/// One emitted source artifact (e.g. `src/main.rs`, `Cargo.toml`).
21#[derive(Debug)]
22pub struct GeneratedFile {
23 pub path: String,
24 pub contents: String,
25}
26
27/// Pluggable code-generation strategy. Each backend turns a parsed [`Css`] into
28/// a list of files that, taken together, form a buildable standalone project.
29pub trait CodegenBackend {
30 /// Stable identifier (e.g. `"rust"`) used by the HTTP layer to pick a backend.
31 fn lang(&self) -> &'static str;
32
33 /// Render a single CSS expression. Useful for tests / quick previews where
34 /// the caller doesn't want a full project layout.
35 fn emit_css(&self, css: &Css) -> String;
36
37 /// Emit a complete standalone project. The returned files are root-relative.
38 fn emit_project(&self, css: &Css) -> Vec<GeneratedFile>;
39}
40
41/// Look up a backend by its [`CodegenBackend::lang`] identifier.
42#[must_use] pub fn backend_for(lang: &str) -> Option<Box<dyn CodegenBackend>> {
43 match lang {
44 "rust" => Some(Box::new(rust::RustBackend)),
45 "cpp" | "c++" => Some(Box::new(cpp::CppBackend)),
46 "python" | "py" => Some(Box::new(python::PythonBackend)),
47 _ => None,
48 }
49}