Skip to main content

mycelix_leptos_client/
lib.rs

1// Copyright (C) 2024-2026 Tristan Stoltz / Luminous Dynamics
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// Commercial licensing: see COMMERCIAL_LICENSE.md at repository root
4
5//! Browser-compatible Holochain client for Leptos frontends.
6//!
7//! Replaces `@holochain/client` (JavaScript) for Rust WASM frontends.
8//! Uses `web-sys::WebSocket` + `rmp-serde` (MessagePack) to communicate
9//! with a Holochain conductor over binary WebSocket frames.
10//!
11//! # Architecture
12//!
13//! The crate is built around the [`HolochainTransport`] trait, which abstracts
14//! the underlying communication mechanism. Two implementations are provided:
15//!
16//! - [`BrowserWsTransport`] — Uses `web-sys::WebSocket` for browser WASM targets
17//! - `TauriIpcTransport` — (future) Calls Tauri backend via `wasm-bindgen` `invoke()`
18//!
19//! The [`HolochainClient`] type wraps any transport and provides a typed,
20//! ergonomic API for calling zome functions with automatic MessagePack
21//! serialization/deserialization.
22//!
23//! # Example
24//!
25//! ```rust,ignore
26//! use mycelix_leptos_client::{HolochainClient, BrowserWsTransport};
27//! use serde::{Serialize, Deserialize};
28//!
29//! #[derive(Serialize)]
30//! struct CreateProposal { title: String, body: String }
31//!
32//! #[derive(Deserialize)]
33//! struct ProposalHash { hash: Vec<u8> }
34//!
35//! async fn example() {
36//!     let transport = BrowserWsTransport::new();
37//!     let client = HolochainClient::new(transport, "mycelix-unified", "governance");
38//!     // Connect with optional auth token (None = no authentication)
39//!     client.connect("ws://localhost:8888", None).await.unwrap();
40//!
41//!     let result: ProposalHash = client.call_zome(
42//!         "agora",
43//!         "create_proposal",
44//!         &CreateProposal { title: "Test".into(), body: "Body".into() },
45//!     ).await.unwrap();
46//! }
47//! ```
48
49#[cfg(feature = "browser")]
50pub mod browser;
51pub mod client;
52pub mod error;
53pub mod mock;
54#[cfg(feature = "native")]
55pub mod native;
56#[cfg(feature = "tauri")]
57pub mod tauri;
58pub mod transport;
59pub mod types;
60
61// Re-exports for convenience
62pub use client::HolochainClient;
63pub use error::ClientError;
64pub use mock::MockTransport;
65#[cfg(feature = "native")]
66pub use native::NativeWsTransport;
67pub use transport::HolochainTransport;
68pub use types::{
69    decode, encode, ConnectConfig, ConnectionStatus, ReconnectConfig, ZomeCallRequest,
70    ZomeCallResponse,
71};
72
73#[cfg(feature = "browser")]
74pub use browser::BrowserWsTransport;
75
76#[cfg(feature = "tauri")]
77pub use tauri::TauriIpcTransport;