Crate ash_rpc_core

Crate ash_rpc_core 

Source
Expand description

§ash-rpc-core

A comprehensive JSON-RPC 2.0 implementation with transport support.

§Features

  • Complete JSON-RPC 2.0 support - Request, response, notification, and batch handling
  • Multiple transports - TCP, TCP streaming, HTTP via Axum, and Tower middleware
  • Type-safe builders - Fluent API for constructing requests and responses
  • Method registry - Organize and dispatch JSON-RPC methods
  • Auto-documentation - Generate OpenAPI/Swagger specs from method definitions
  • Macro support - Convenient macros for common response patterns

§Quick Start

use ash_rpc_core::*;
use serde_json::Value;

// Create a method registry
let registry = MethodRegistry::new()
    .register("ping", |_params, id| {
        rpc_success!("pong", id)
    })
    .register("add", |params, id| {
        if let Some(params) = params {
            let nums: Vec<i32> = serde_json::from_value(params).unwrap();
            if nums.len() == 2 {
                rpc_success!(nums[0] + nums[1], id)
            } else {
                rpc_error!(error_codes::INVALID_PARAMS, "Expected 2 numbers", id)
            }
        } else {
            rpc_error!(error_codes::INVALID_PARAMS, "Parameters required", id)
        }
    });

// Call a method
let response = registry.call("ping", None, Some(Value::Number(serde_json::Number::from(1))));

Re-exports§

pub use types::*;
pub use builders::*;
pub use traits::*;
pub use registry::*;
pub use transport::*;

Modules§

builders
Builder patterns for JSON-RPC types.
macros
Convenience macros for JSON-RPC response creation.
registry
Method registry for organizing and dispatching JSON-RPC methods.
traits
Core traits for JSON-RPC handlers and processors.
transport
types
Core JSON-RPC 2.0 types and data structures.
utils
Utility functions for JSON-RPC documentation generation.

Macros§

rpc_error
Create an error response with code, message, and optional ID
rpc_error_obj
Create a JSON-RPC error object (not a response)
rpc_error_with_data
Create an error response with code, message, additional data and optional ID
rpc_internal_error
rpc_invalid_params
Common error response shortcuts using predefined error codes
rpc_method_not_found
rpc_notification
Create a JSON-RPC notification
rpc_parse_error
rpc_request
Create a JSON-RPC request
rpc_success
Create a success response with a result value and optional ID