Skip to main content

hypen_engine/
lib.rs

1//! # Hypen Engine
2//!
3//! Core reactive rendering engine for the Hypen UI framework.
4//!
5//! This crate provides the platform-agnostic runtime that powers Hypen's
6//! declarative UI model. It parses Hypen DSL, maintains a virtual tree,
7//! tracks reactive dependencies, and emits minimal [`Patch`] operations
8//! when state changes.
9//!
10//! ## Public API
11//!
12//! The primary types for SDK authors and application developers:
13//!
14//! - [`Engine`] — Native Rust engine (for embedding in Rust applications)
15//! - [`EngineError`] — Structured error type for all engine operations
16//! - [`Patch`] — UI mutation operations emitted by the engine
17//! - [`Element`] / [`Value`] — IR building blocks for custom components
18//! - [`Module`] / [`ModuleInstance`] — Stateful module management
19//! - [`StateChange`] — Path-based state change notifications
20//!
21//! For WASM/JavaScript usage, see the [`wasm`] module (enabled via the `js` feature).
22//!
23//! ## Internal Modules
24//!
25//! The following modules are exported for advanced use and testing but are
26//! **not part of the stable API**. Their signatures may change between
27//! minor versions:
28//!
29//! `ir`, `reactive`, `reconcile`, `dispatch`, `render`, `serialize`
30
31pub mod dispatch;
32pub mod engine;
33pub mod error;
34pub mod ir;
35pub mod lifecycle;
36pub mod reactive;
37pub mod reconcile;
38pub mod serialize;
39pub mod state;
40
41/// Internal rendering logic shared between Engine and WasmEngine.
42///
43/// This module is public for integration testing but is not part of the
44/// stable API — use [`Engine`] or `WasmEngine` instead.
45#[doc(hidden)]
46pub mod render;
47
48/// Internal logging utilities.
49#[doc(hidden)]
50pub mod logger;
51
52// WASM bindings module
53// The FFI types (wasm::ffi) are always available for testing and cross-platform use.
54// The actual WASM bindings are conditionally compiled:
55// - `js` feature: JavaScript bindings via wasm-bindgen (Node.js, Bun, browsers)
56// - `wasi` feature: WASI-compatible C FFI (Go, Python, Rust, etc.)
57pub mod wasm;
58
59// UniFFI bindings for native platforms (Kotlin, Swift, Python, Ruby)
60#[cfg(feature = "uniffi")]
61pub mod uniffi;
62
63// UniFFI scaffolding must be in crate root
64#[cfg(feature = "uniffi")]
65::uniffi::setup_scaffolding!();
66
67// ── Public API ─────────────────────────────────────────────────────────
68
69pub use engine::Engine;
70pub use error::EngineError;
71
72pub use ir::{ast_to_ir_node, Element, IRNode, Value};
73pub use lifecycle::{Module, ModuleInstance};
74pub use reconcile::Patch;
75pub use state::StateChange;
76
77#[cfg(test)]
78mod tailwind_tests {
79    use hypen_tailwind_parse::parse_classes;
80
81    #[test]
82    fn test_tailwind_parse_basic() {
83        let output = parse_classes("p-4 text-blue-500 bg-white");
84        assert_eq!(output.base.len(), 3);
85
86        let props = output.to_props();
87        assert_eq!(props.get("padding"), Some(&"1rem".to_string()));
88        assert_eq!(props.get("color"), Some(&"#3b82f6".to_string()));
89        assert_eq!(props.get("background-color"), Some(&"#ffffff".to_string()));
90    }
91
92    #[test]
93    fn test_tailwind_parse_with_breakpoints() {
94        let output = parse_classes("p-4 md:p-8 lg:p-12");
95
96        let props = output.to_props();
97        assert_eq!(props.get("padding"), Some(&"1rem".to_string()));
98        assert_eq!(props.get("padding@md"), Some(&"2rem".to_string()));
99        assert_eq!(props.get("padding@lg"), Some(&"3rem".to_string()));
100    }
101
102    #[test]
103    fn test_tailwind_parse_with_hover() {
104        let output = parse_classes("bg-white hover:bg-blue-500");
105
106        let props = output.to_props();
107        assert_eq!(props.get("background-color"), Some(&"#ffffff".to_string()));
108        assert_eq!(
109            props.get("background-color:hover"),
110            Some(&"#3b82f6".to_string())
111        );
112    }
113
114    #[test]
115    fn test_tailwind_parse_layout() {
116        let output = parse_classes("flex justify-center items-center gap-4");
117
118        let props = output.to_props();
119        assert_eq!(props.get("display"), Some(&"flex".to_string()));
120        assert_eq!(props.get("justify-content"), Some(&"center".to_string()));
121        assert_eq!(props.get("align-items"), Some(&"center".to_string()));
122        assert_eq!(props.get("gap"), Some(&"1rem".to_string()));
123    }
124
125    #[test]
126    fn test_tailwind_parse_sizing() {
127        let output = parse_classes("w-full h-screen max-w-lg");
128
129        let props = output.to_props();
130        assert_eq!(props.get("width"), Some(&"100%".to_string()));
131        assert_eq!(props.get("height"), Some(&"100vh".to_string()));
132        assert_eq!(props.get("max-width"), Some(&"32rem".to_string()));
133    }
134}