Skip to content

Commit

Permalink
add macros
Browse files Browse the repository at this point in the history
  • Loading branch information
kardeiz committed Dec 6, 2019
1 parent 0e51740 commit 5bdb06c
Show file tree
Hide file tree
Showing 10 changed files with 663 additions and 232 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/target
**/*.rs.bk
Cargo.lock
3 changes: 3 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use_small_heuristics = "Max"
use_field_init_shorthand = true
use_try_shorthand = true
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Jacob Brown <[email protected]>"]
edition = "2018"
name = "jsonrpc-v2"
version = "0.4.1"
version = "0.5.0-alpha.1"
description = "JSONRPC 2.0 server"
keywords = ["jsonrpc", "json-rpc", "jsonrpc2", "rpc"]
license = "MIT"
Expand All @@ -11,9 +11,10 @@ repository = "https://github.com/kardeiz/jsonrpc-v2"

[features]
default = ["actix-integration"]
easy_errors = []
easy-errors = []
actix-integration = ["actix-web", "actix-service"]
hyper-integration = ["hyper", "tower-service"]
macros = ["jsonrpc-v2-macros"]

[dependencies]
bytes = "0.4"
Expand All @@ -25,6 +26,8 @@ actix-service = { version = "0.4", optional = true }
hyper = { version = "0.13.0-alpha.4", optional = true }
async-trait = "0.1.17"
tower-service = { version = "0.3.0-alpha.2", optional = true }
extensions = "0.2"
jsonrpc-v2-macros = { version = "0.1.0", path = "./jsonrpc-v2-macros", optional = true }

[dependencies.serde]
features = ["derive"]
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for anything that implements `Display`, and the display value will be provided i
Otherwise, custom errors should implement [`ErrorLike`](trait.ErrorLike.html) to map errors to the JSON-RPC 2.0 `Error` response.

Individual method handlers are `async` functions that can take various kinds of args (things that can be extracted from the request, like
the `Params` or `State`), and should return a `Result<Item, Error>` where the `Item` is serializable. See examples below.
the `Params` or `Data`), and should return a `Result<Item, Error>` where the `Item` is serializable. See examples below.

## Usage

Expand All @@ -36,13 +36,13 @@ async fn sub(Params(params): Params<(usize, usize)>) -> Result<usize, Error> {
Ok(params.0 - params.1)
}

async fn message(state: State<String>) -> Result<String, Error> {
Ok(String::from(&*state))
async fn message(data: Data<String>) -> Result<String, Error> {
Ok(String::from(&*data))
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc = Server::with_state(String::from("Hello!"))
.with_method("add", add)
let rpc = Server::new()
.with_data(Data::new(String::from("Hello!")))
.with_method("sub", sub)
.with_method("message", message)
.finish();
Expand All @@ -62,6 +62,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
```

Current version: 0.4.0
Current version: 0.4.1

License: MIT
14 changes: 11 additions & 3 deletions examples/actix-web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ struct TwoNums {
b: usize,
}

pub struct Foo(String);

async fn test(Params(params): Params<serde_json::Value>) -> Result<String, Error> {
Ok(serde_json::to_string_pretty(&params)?)
}

async fn add(Params(params): Params<TwoNums>) -> Result<usize, Error> {
Ok(params.a + params.b)
}
Expand All @@ -14,14 +20,16 @@ async fn sub(Params(params): Params<(usize, usize)>) -> Result<usize, Error> {
Ok(params.0 - params.1)
}

async fn message(state: State<String>) -> Result<String, Error> {
Ok(String::from(&*state))
async fn message(data: Data<Foo>) -> Result<String, Error> {
Ok(String::from(&(&*data.0).0))
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc = Server::with_state(String::from("Hello!"))
let rpc = Server::new()
.with_data(Data::new(String::from("Hello!")))
.with_method("add", add)
.with_method("sub", sub)
.with_method("test", test)
.with_method("message", message)
.finish();

Expand Down
7 changes: 4 additions & 3 deletions examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ async fn sub(Params(params): Params<(usize, usize)>) -> Result<usize, Error> {
Ok(params.0 - params.1)
}

async fn message(state: State<String>) -> Result<String, Error> {
Ok(String::from(&*state))
async fn message(data: Data<String>) -> Result<String, Error> {
Ok(String::from(&*data))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc = Server::with_state(String::from("Hello!"))
let rpc = Server::new()
.with_data(Data::new(String::from("Hello!")))
.with_method("add", add)
.with_method("sub", sub)
.with_method("message", message)
Expand Down
37 changes: 31 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,51 @@ async fn sub(Params(params): Params<(usize, usize)>) -> Result<usize, Error> {
Ok(params.0 - params.1)
}

async fn message(state: State<String>) -> Result<String, Error> {
Ok(String::from(&*state))
async fn message(data: Data<String>) -> Result<String, Error> {
Ok(String::from(&*data))
}

#[jsonrpc_v2_method(extern = true)]
async fn message_test(a: i32, b: i32) -> Result<String, Error> {
Ok(format!("{} + {}", a, b))
}

// async fn message_test_2(params: Params<Option<serde_json::Value>>) -> Result<serde_json::Value, Error> {
// message_test(params).await
// }

// async fn message_test(Params(p): Params<Option<serde_json::Value>>) -> Result<String, Error> {
// Ok(format!("{}", "HI"))
// }

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc = Server::with_state(String::from("Hello!"))
let rpc = Server::new()
.with_data(Data::new(String::from("Hello!")))
.with_method("add", add)
.with_method("sub", sub)
.with_method("message", message)
.finish_direct();
.with_method("message-test", |params| async move { message_test(params).await })
.finish_unwrapped();

let req = RequestObject::request()
.with_method("sub")
.with_params(serde_json::json!([12, 12]))
.finish()?;
.finish();

let res = rpc.handle(req).await;

let res = rpc.handle(req).await.unwrap();
println!("{}", serde_json::to_string_pretty(&res)?);

let req = RequestObject::request()
.with_method("message-test")
.with_params(serde_json::json!([12, 1]))
.finish();

let res = rpc.handle(req).await;

println!("{}", serde_json::to_string_pretty(&res)?);

Ok(())
}

19 changes: 19 additions & 0 deletions jsonrpc-v2-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "jsonrpc-v2-macros"
version = "0.1.0"
authors = ["Jacob Brown <[email protected]>"]
edition = "2018"
description = "Proc macros for jsonrpc-v2"
keywords = ["jsonrpc", "json-rpc", "jsonrpc2", "rpc"]
license = "MIT"
readme = "../README.md"
repository = "https://github.com/kardeiz/jsonrpc-v2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quote = "1"
syn = {version = "1", features = ["full", "extra-traits"] }
proc-macro2 = "1"

[lib]
proc-macro = true
Loading

0 comments on commit 5bdb06c

Please sign in to comment.