MCPcopy Index your code
hub / github.com/andogq/qubit

github.com/andogq/qubit @qubit-macros-v1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release qubit-macros-v1.0 ↗ · + Follow
341 symbols 634 edges 99 files 79 documented · 23%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README


Qubit: Seamless RPC For Rust & TypeScript

crates.io docs.rs npm checks

Tired of wrestling with RPC boilerplate? Qubit simplifies communication between your Rust services and TypeScript clients, offering a type-safe and feature-rich development experience, so you can focus on building amazing applications.

Features:

  • Generated Type-Safe Clients: Say goodbye to manual type definitions, Qubit automatically generates TypeScript clients based on your Rust API, ensuring a smooth development experience.

  • Subscriptions: Build real-time, data-driven applications with subscriptions, allowing for your Rust server to push data directly to connected TypeScript clients.

  • Build Modular APIs: Organise your API handlers into nested routers, ensuring simplicity and maintainability as your service grows.

  • Serde Compatibility: Leverage Serde for seamless data serialisation and deserialisation between Rust and TypeScript.

  • Built on JSONRPC 2.0: Need a non-TypeScript client? Use any JSONRPC client in any language over WebSockets or HTTP.

  • Proven Base: Built on established libraries like ts-rs for type generation and jsonrpsee as the JSONRPC implementation.

Getting Started

  1. Add the required dependencies
# Cargo.toml
[dependencies]
qubit = "1.0.0-beta.0"

serde = { version = "1.0", features = ["derive"] } # Required for serialisable types
futures = "0.3" # Required for streaming functionality

tokio = { version = "1.44", features = ["full"] }
axum = "0.8"
hyper = { version = "1.6", features = ["server"] }
pnpm i @qubit-rs/client@latest
  1. Setup a Qubit router, and save the generated types
#[handler(query)]
async fn hello_world() -> String {
    "Hello, world!".to_string()
}

let router = Router::new()
    .handler(hello_world);

router
    .as_codegen()
    .write_type("./bindings", TypeScript::new());
  1. Attach the Qubit router to an Axum router, and start it
// Create a service and handle
let (qubit_service, qubit_handle) = router
    .as_rpc()
    .into_service(());

// Nest into an Axum router
let axum_router = axum::Router::<()>::new()
    .nest_service("/rpc", qubit_service);

// Start a Hyper server
axum::serve(
    tokio::net::TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 9944)))
        .await
        .unwrap(),
    axum_router,
)
.await
.unwrap();

qubit_handle.stop().unwrap();
  1. Make requests from the TypeScript client
// Import transport from client, and generated server type
import { build_client, http } from "@qubit-rs/client";
import type { QubitServer } from "./bindings";

// Connect with the API
const api = build_client<QubitServer>(http("http://localhost:9944/rpc"));

// Call the handlers
const message = await api.hello_world.query();
console.log("received from server:", message);

Examples

Checkout all the examples in the examples directory.

Cargo Features

Feature Description
ts-format Format generated TypeScript types. Note: This adds a lot of dependencies.
ts-esm Ensure import statements conform with the ES Modules spec by appending .js to paths. This likely isn't needed if a bundler is in use.
ts-serde-json Add TypeScript support for serde_json.
ts-chrono Add TypeScript support for chrono.
ts-bigdecimal Add TypeScript support for bigdecimal.
ts-url Add TypeScript support for url.
ts-uuid Add TypeScript support for uuid.
ts-bson-uuid Add TypeScript support for bson-uuid.
ts-bytes Add TypeScript support for bytes.
ts-indexmap Add TypeScript support for indexmap.
ts-ordered-float Add TypeScript support for ordered-float.
ts-heapless Add TypeScript support for heapless.
ts-semver Add TypeScript support for semver.
ts-smol-str Add TypeScript support for smol_str.
ts-tokio Add TypeScript support for tokio.

FAQs

Qubit?

The term "Qubit" refers to the fundamental unit of quantum information. Just as a qubit can exist in a superposition of states, Qubit bridges the gap between Rust and TypeScript, empowering developers to create truly exceptional applications.

Prior Art

  • rspc: Similar concept, however uses a bespoke solution for generating TypeScript types from Rust structs, which isn't completely compatible with all of Serde's features for serialising and deserialising structs.

  • trpc: Needs no introduction, however it being restricted to TypeScript backends makes it relatively useless for Rust developers.

Extension points exported contracts — how you extend this code

RegisterableHandler (Interface)
Registration implementation differs depending on the return type of the handler. This is to account for handlers which m [4 …
src/handler/mod.rs
RouterModule (Interface)
Common functionality exposed by router modules. The module will be provided a handler, which it must generate a type-era [2 …
src/router/mod.rs
Backend (Interface)
Code generation backend implementation. [1 implementers]
src/codegen/backend/mod.rs
FromRequestExtensions (Interface)
(no doc) [6 implementers]
src/handler/ctx.rs
RouterModuleHandler (Interface)
Converts a handler existing as a generic (`F`) into a type-erased value. Intended for use with the [`RouterModule`] trai [2 …
src/router/mod.rs
HandlerBackend (Interface)
Backend implementation for handlers. [1 implementers]
src/codegen/backend/mod.rs
HandlerReturnMarker (Interface)
Marker trait for any markers that can be used as a return value from [`QubitHandler`]. [3 implementers]
src/handler/marker.rs
TypeBackend (Interface)
Backend implementation for handlers. [1 implementers]
src/codegen/backend/mod.rs

Core symbols most depended-on inside this repo

handler
called by 35
src/router/mod.rs
query
called by 14
crates/qubit-macros/src/macros/handler/parse.rs
as_rpc
called by 11
src/router/mod.rs
insert
called by 9
src/util/node.rs
into_module
called by 7
src/router/rpc.rs
insert_prefix
called by 7
src/util/graph.rs
collect
called by 7
src/handler/ts.rs
build_client
called by 6
packages/client/src/client.ts

Shape

Function 150
Method 114
Class 54
Interface 12
Enum 11

Languages

Rust89%
TypeScript11%

Modules by API surface

examples/chaos/src/main.rs22 symbols
examples/chat-room-react/src-rust/src/manager.rs19 symbols
crates/qubit-macros/src/macros/handler/parse.rs19 symbols
src/router/mod.rs18 symbols
src/handler/mod.rs16 symbols
crates/qubit-macros/src/macros/handler/analyse.rs16 symbols
src/codegen/reflection/dependent_types.rs14 symbols
src/codegen/backend/typescript.rs14 symbols
tests/handlers.rs13 symbols
src/util/graph.rs12 symbols
src/codegen/reflection/param_visitor.rs10 symbols
src/codegen/backend/mod.rs10 symbols

For agents

$ claude mcp add qubit \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact