Skip to main content

opencore_jsonrpc_rust/
lib.rs

1//! # OpenCore JSON-RPC Rust
2//!
3//! A simple and elegant library for creating JSON-RPC servers that communicate
4//! with TypeScript frameworks via stdin/stdout.
5//!
6//! ## Features
7//!
8//! - Simple handler registration with type-safe function signatures
9//! - Line-delimited JSON protocol for easy integration
10//! - Automatic request/response serialization
11//! - Binary event emission compatible with `@BinaryEvent`
12//! - Clean error handling
13//!
14//! ## Quick Start
15//!
16//! ```no_run
17//! use opencore_jsonrpc_rust::server::BinaryServer;
18//! use serde_json::Value;
19//!
20//! fn add(params: Vec<Value>) -> Result<Value, String> {
21//!     if params.len() != 2 {
22//!         return Err("Expected 2 parameters".into());
23//!     }
24//!     let a = params[0].as_i64().ok_or("Invalid number")?;
25//!     let b = params[1].as_i64().ok_or("Invalid number")?;
26//!     Ok(Value::from(a + b))
27//! }
28//!
29//! fn main() {
30//!     let mut server = BinaryServer::new();
31//!     server.register("add", add);
32//!     server
33//!         .emit_event("worker.ready", serde_json::json!({ "pid": std::process::id() }))
34//!         .unwrap();
35//!     server.run();
36//! }
37//! ```
38//!
39//! ## Protocol
40//!
41//! ### Request Format
42//!
43//! ```json
44//! {
45//!   "id": "unique-request-id",
46//!   "action": "method-name",
47//!   "params": [arg1, arg2, ...]
48//! }
49//! ```
50//!
51//! ### Response Format (Success)
52//!
53//! ```json
54//! {
55//!   "status": "ok",
56//!   "id": "unique-request-id",
57//!   "result": <value>
58//! }
59//! ```
60//!
61//! ### Response Format (Error)
62//!
63//! ```json
64//! {
65//!   "status": "error",
66//!   "id": "unique-request-id",
67//!   "error": "error message"
68//! }
69//! ```
70//!
71//! ### Event Format
72//!
73//! ```json
74//! {
75//!   "type": "event",
76//!   "event": "worker.ready",
77//!   "data": {"pid": 1234}
78//! }
79//! ```
80
81pub mod protocol;
82pub mod server;