BinaryServer

Struct BinaryServer 

Source
pub struct BinaryServer { /* private fields */ }
Expand description

A JSON-RPC server that communicates via stdin/stdout.

The server reads line-delimited JSON requests from stdin and writes line-delimited JSON responses to stdout. This makes it easy to integrate with TypeScript or other language frameworks using process spawning.

§Example

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

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

fn main() {
    let mut server = BinaryServer::new();
    server.register("multiply", multiply);
    server.run();
}

Implementations§

Source§

impl BinaryServer

Source

pub fn new() -> Self

Creates a new empty server with no registered handlers.

§Example
use opencore_jsonrpc_rust::server::BinaryServer;

let server = BinaryServer::new();
Source

pub fn register(&mut self, action: &str, handler: Handler)

Registers a handler function for a specific action name.

When a request with the given action is received, the corresponding handler will be invoked with the request parameters.

§Arguments
  • action - The action name to register (e.g., “sum”, “multiply”)
  • handler - The handler function to invoke for this action
§Example
use opencore_jsonrpc_rust::server::BinaryServer;
use serde_json::Value;

fn echo(params: Vec<Value>) -> Result<Value, String> {
    params.first()
        .cloned()
        .ok_or_else(|| "No parameter provided".into())
}

let mut server = BinaryServer::new();
server.register("echo", echo);
Source

pub fn run(&self)

Starts the server loop, reading from stdin and writing to stdout.

This method blocks indefinitely, processing requests as they arrive. Each line from stdin should be a valid JSON request. Responses are written as JSON lines to stdout.

§Panics

This method will not panic under normal circumstances. I/O errors are handled gracefully by continuing to the next request.

Trait Implementations§

Source§

impl Debug for BinaryServer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BinaryServer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.