Skip to main content

Crate opencore_jsonrpc_rust

Crate opencore_jsonrpc_rust 

Source
Expand description

§OpenCore JSON-RPC Rust

A simple and elegant library for creating JSON-RPC servers that communicate with TypeScript frameworks via stdin/stdout.

§Features

  • Simple handler registration with type-safe function signatures
  • Line-delimited JSON protocol for easy integration
  • Automatic request/response serialization
  • Binary event emission compatible with @BinaryEvent
  • Clean error handling

§Quick Start

use opencore_jsonrpc_rust::server::BinaryServer;
use serde_json::Value;

fn add(params: Vec<Value>) -> Result<Value, String> {
    if params.len() != 2 {
        return Err("Expected 2 parameters".into());
    }
    let a = params[0].as_i64().ok_or("Invalid number")?;
    let b = params[1].as_i64().ok_or("Invalid number")?;
    Ok(Value::from(a + b))
}

fn main() {
    let mut server = BinaryServer::new();
    server.register("add", add);
    server
        .emit_event("worker.ready", serde_json::json!({ "pid": std::process::id() }))
        .unwrap();
    server.run();
}

§Protocol

§Request Format

{
  "id": "unique-request-id",
  "action": "method-name",
  "params": [arg1, arg2, ...]
}

§Response Format (Success)

{
  "status": "ok",
  "id": "unique-request-id",
  "result": <value>
}

§Response Format (Error)

{
  "status": "error",
  "id": "unique-request-id",
  "error": "error message"
}

§Event Format

{
  "type": "event",
  "event": "worker.ready",
  "data": {"pid": 1234}
}

Modules§

protocol
JSON-RPC protocol types for request/response communication.
server
Binary JSON-RPC server implementation.