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

            
11
use alloc::{string::String, vec::Vec};
12

            
13
use crate::css::Css;
14

            
15
pub mod cpp;
16
pub mod format;
17
pub mod python;
18
pub mod rust;
19

            
20
/// One emitted source artifact (e.g. `src/main.rs`, `Cargo.toml`).
21
pub struct GeneratedFile {
22
    pub path: String,
23
    pub contents: String,
24
}
25

            
26
/// Pluggable code-generation strategy. Each backend turns a parsed [`Css`] into
27
/// a list of files that, taken together, form a buildable standalone project.
28
pub trait CodegenBackend {
29
    /// Stable identifier (e.g. `"rust"`) used by the HTTP layer to pick a backend.
30
    fn lang(&self) -> &'static str;
31

            
32
    /// Render a single CSS expression. Useful for tests / quick previews where
33
    /// the caller doesn't want a full project layout.
34
    fn emit_css(&self, css: &Css) -> String;
35

            
36
    /// Emit a complete standalone project. The returned files are root-relative.
37
    fn emit_project(&self, css: &Css) -> Vec<GeneratedFile>;
38
}
39

            
40
/// Look up a backend by its [`CodegenBackend::lang`] identifier.
41
pub fn backend_for(lang: &str) -> Option<alloc::boxed::Box<dyn CodegenBackend>> {
42
    match lang {
43
        "rust" => Some(alloc::boxed::Box::new(rust::RustBackend)),
44
        "cpp" | "c++" => Some(alloc::boxed::Box::new(cpp::CppBackend)),
45
        "python" | "py" => Some(alloc::boxed::Box::new(python::PythonBackend)),
46
        _ => None,
47
    }
48
}