hypen-server 0.4.37

Rust server SDK for building Hypen applications
Documentation

hypen-server

Rust server SDK for building Hypen applications.

Hypen is a declarative UI language and reactive runtime for building cross-platform applications. This SDK provides a type-safe, idiomatic Rust API for defining stateful modules, handling actions, managing routing, and discovering components.

Quick Start

use hypen_server::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Serialize, Deserialize)]
struct Counter { count: i32 }

#[derive(Deserialize)]
struct AddPayload { amount: i32 }

let module = ModuleBuilder::new("Counter")
    .state(Counter { count: 0 })
    .ui(r#"
        Column {
            Text("Count: ${state.count}")
            Button("@actions.increment") { Text("+") }
        }
    "#)
    .on_action::<()>("increment", |state, _, _ctx| {
        state.count += 1;
    })
    .on_action::<AddPayload>("add", |state, payload, _ctx| {
        state.count += payload.amount;
    })
    .build();

Action Handling

Actions always take a type parameter for the payload and a string name. Use () for actions with no payload.

// No payload
.on_action::<()>("increment", |state, _, _ctx| {
    state.count += 1;
})

// Typed payload (just needs #[derive(Deserialize)])
#[derive(Deserialize)]
struct SetValue { value: i32 }

.on_action::<SetValue>("set_value", |state, payload, _ctx| {
    state.count = payload.value;
})

// Raw JSON access
.on_action::<serde_json::Value>("raw", |state, raw, _ctx| {
    if let Some(n) = raw.as_i64() { state.count = n as i32; }
})

Routing

let app = HypenApp::builder()
    .route("/", home_module)
    .route("/counter", counter_module)
    .components_dir("./components")
    .build();

app.navigate("/counter");

Features

  • Typed state with automatic JSON diffing and path-based change detection
  • Typed action payloads via serde::Deserialize (no custom traits needed)
  • Lifecycle hooks: on_created, on_destroyed
  • URL router with pattern matching and parameter extraction
  • Component discovery from .hypen files on the filesystem
  • Cross-module communication via GlobalContext and EventEmitter
  • Patch-based rendering compatible with all Hypen renderers (DOM, Canvas, iOS, Android)

License

MIT