json_rpc_v2

Crates.iojson_rpc_v2
lib.rsjson_rpc_v2
version0.2.0
sourcesrc
created_at2023-03-18 05:57:09.058731
updated_at2023-03-18 13:35:19.889803
descriptionJSON-RPC 2.0
homepage
repositoryhttps://github.com/luoshuqi/json_rpc_v2
max_upload_size
id813529
size37,619
(luoshuqi)

documentation

README

json rpc

JSON-RPC 2.0 server implementation in Rust

Example

full example


#[json_rpc]
fn greet(name: String) -> Result<String, Infallible> {
    Ok(format!("Hello {}", name))
}

struct System;

#[json_rpc]
impl System {
    fn time() -> Result<u64, json_rpc_v2::Error> {
        SystemTime::now().duration_since(UNIX_EPOCH).map(|v| v.as_secs()).map_err(|_| json_rpc_v2::Error::server_error())
    }

    async fn issue() -> Result<String, json_rpc_v2::Error> {
        read_to_string("/etc/issue").await.map_err(|_| json_rpc_v2::Error::server_error())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let mut registry = Registry::new();
    registry.register_method("greet", greet);
    registry.register::<System>();
    
    let _ = registry.call(br#"{"jsonrpc":"2.0","method":"greet","params":["foo"],"id":1}"#).await;
    let _ = registry.call(br#"{"jsonrpc":"2.0","method":"system.time","params":[],"id":1}"#).await;
    let _ = registry.call(br#"{"jsonrpc":"2.0","method":"system.issue","params":[],"id":1}"#).await;
    Ok(())
}
Commit count: 6

cargo fmt