-
Notifications
You must be signed in to change notification settings - Fork 3
/
c00-readme.rs
99 lines (83 loc) · 2.49 KB
/
c00-readme.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#![allow(unused)] // For examples.
use rpc_router::{
resources_builder, router_builder, CallResponse, FromResources, IntoParams, Request, Resources, RpcHandlerError,
RpcParams, RpcResource,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, thiserror::Error, RpcHandlerError)]
pub enum MyError {
// TBC
#[error("TitleCannotBeEmpty")]
TitleCannotBeEmpty,
}
// Make it a Resource with RpcResource derive macro
#[derive(Clone, RpcResource)]
pub struct ModelManager {}
// Make it a Resource by implementing FromResources
#[derive(Clone)]
pub struct AiManager {}
impl FromResources for AiManager {}
// Make it a Params with RpcParams derive macro
#[derive(Serialize, Deserialize, RpcParams)]
pub struct TaskForCreate {
title: String,
done: Option<bool>,
}
// Make it a Params by implementing IntoParams
#[derive(Deserialize)]
pub struct ParamsIded {
pub id: i64,
}
impl IntoParams for ParamsIded {}
// Return values just need to implement Serialize
#[derive(Serialize)]
pub struct Task {
id: i64,
title: String,
done: bool,
}
pub async fn get_task(mm: ModelManager, params: ParamsIded) -> Result<Task, MyError> {
Ok(Task {
id: params.id,
title: "fake task".to_string(),
done: false,
})
}
pub async fn create_task(mm: ModelManager, aim: AiManager, params: TaskForCreate) -> Result<i64, MyError> {
Ok(123) // return fake id
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build the Router with the handlers and common resources
let rpc_router = router_builder!(
handlers: [get_task, create_task], // will be turned into routes
resources: [ModelManager {}, AiManager {}] // common resources for all calls
)
.build();
// Can do the same with `Router::builder().append()/append_resource()`
// Create and parse rpc request example.
let rpc_request: Request = json!({
"jsonrpc": "2.0",
"id": "some-client-req-id", // the json rpc id, that will get echoed back, can be null
"method": "get_task",
"params": {
"id": 123
}
})
.try_into()?;
// Async Execute the RPC Request with the router common resources
let call_response = rpc_router.call(rpc_request).await?;
// Or `call_with_resources` for additional per-call Resources that override router common resources.
// e.g., rpc_router.call_with_resources(rpc_request, additional_resources)
// Display the response.
let CallResponse { id, method, value } = call_response;
println!(
r#"RPC call response:
id: {id:?},
method: {method},
value: {value:?},
"#
);
Ok(())
}