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(crate) mod engine_core;
34pub mod error;
35pub mod ir;
36pub mod lifecycle;
37pub mod reactive;
38pub mod reconcile;
39pub mod serialize;
40pub mod state;
41
42/// Internal rendering logic shared between Engine and WasmEngine.
43///
44/// This module is public for integration testing but is not part of the
45/// stable API — use [`Engine`] or `WasmEngine` instead.
46#[doc(hidden)]
47pub mod render;
48
49/// Internal logging utilities.
50#[doc(hidden)]
51pub mod logger;
52
53// WASM bindings module
54// The FFI types (wasm::ffi) are always available for testing and cross-platform use.
55// The actual WASM bindings are conditionally compiled:
56// - `js` feature: JavaScript bindings via wasm-bindgen (Node.js, Bun, browsers)
57// - `wasi` feature: WASI-compatible C FFI (Go, Python, Rust, etc.)
58pub mod wasm;
59
60// UniFFI bindings for native platforms (Kotlin, Swift, Python, Ruby)
61#[cfg(feature = "uniffi")]
62pub mod uniffi;
63
64// UniFFI scaffolding must be in crate root
65#[cfg(feature = "uniffi")]
66::uniffi::setup_scaffolding!();
67
68// ── Public API ─────────────────────────────────────────────────────────
69
70pub use engine::Engine;
71pub use error::EngineError;
72
73pub use ir::{ast_to_ir_node, Element, IRNode, Value};
74pub use ir::{parse_svg, resolve_icons_in_ir, IconData, IconPath, ResourceRegistry};
75pub use lifecycle::{Module, ModuleInstance};
76pub use reconcile::Patch;
77pub use state::StateChange;
78
79#[cfg(test)]
80mod tailwind_tests {
81    use hypen_tailwind_parse::parse_classes;
82
83    #[test]
84    fn test_tailwind_parse_basic() {
85        let output = parse_classes("p-4 text-blue-500 bg-white");
86        assert_eq!(output.base.len(), 3);
87
88        let props = output.to_props();
89        assert_eq!(props.get("padding"), Some(&"1rem".to_string()));
90        assert_eq!(props.get("color"), Some(&"#3b82f6".to_string()));
91        assert_eq!(props.get("background-color"), Some(&"#ffffff".to_string()));
92    }
93
94    #[test]
95    fn test_tailwind_parse_with_breakpoints() {
96        let output = parse_classes("p-4 md:p-8 lg:p-12");
97
98        let props = output.to_props();
99        assert_eq!(props.get("padding"), Some(&"1rem".to_string()));
100        assert_eq!(props.get("padding@md"), Some(&"2rem".to_string()));
101        assert_eq!(props.get("padding@lg"), Some(&"3rem".to_string()));
102    }
103
104    #[test]
105    fn test_tailwind_parse_with_hover() {
106        let output = parse_classes("bg-white hover:bg-blue-500");
107
108        let props = output.to_props();
109        assert_eq!(props.get("background-color"), Some(&"#ffffff".to_string()));
110        assert_eq!(
111            props.get("background-color:hover"),
112            Some(&"#3b82f6".to_string())
113        );
114    }
115
116    #[test]
117    fn test_tailwind_parse_layout() {
118        let output = parse_classes("flex justify-center items-center gap-4");
119
120        let props = output.to_props();
121        assert_eq!(props.get("display"), Some(&"flex".to_string()));
122        assert_eq!(props.get("justify-content"), Some(&"center".to_string()));
123        assert_eq!(props.get("align-items"), Some(&"center".to_string()));
124        assert_eq!(props.get("gap"), Some(&"1rem".to_string()));
125    }
126
127    #[test]
128    fn test_tailwind_parse_sizing() {
129        let output = parse_classes("w-full h-screen max-w-lg");
130
131        let props = output.to_props();
132        assert_eq!(props.get("width"), Some(&"100%".to_string()));
133        assert_eq!(props.get("height"), Some(&"100vh".to_string()));
134        assert_eq!(props.get("max-width"), Some(&"32rem".to_string()));
135    }
136}