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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
extern crate proc_macro;
use TokenStream;
/// The `#[service]` macro is applied to an `impl Trait for Struct` block to automatically
/// generate the `ServiceStatic` implementation for the type.
///
/// - When applied to a trait `impl` block, all methods defined in the trait will be registered as service methods.
/// - All service methods must return `Result<T, RpcError<E>>`, where `E` is a user-defined error type that implements `RpcErrCodec`.
///
/// The service method recognizes:
/// - `async fn`
/// - `impl Future`
/// - trait methods wrapped by `async_trait`
///
/// # Usage
///
/// Define a service trait and implement it for your service struct:
///
/// ```rust
/// use razor_rpc::server::service;
/// use razor_rpc::error::RpcError;
/// use serde::{Deserialize, Serialize};
/// use std::future::Future;
///
/// #[derive(Debug, Deserialize, Serialize)]
/// pub struct EchoArgs {
/// message: String,
/// }
///
/// #[derive(Debug, Deserialize, Serialize)]
/// pub struct EchoResp {
/// echoed: String,
/// }
///
/// pub trait EchoService: Send + Sync {
/// fn echo(&self, args: EchoArgs) -> impl Future<Output = Result<EchoResp, RpcError<()>>> + Send;
/// }
///
/// pub struct EchoServiceImpl;
///
/// #[service]
/// impl EchoService for EchoServiceImpl {
/// fn echo(&self, args: EchoArgs) -> impl Future<Output = Result<EchoResp, RpcError<()>>> + Send {
/// async move {
/// Ok(EchoResp { echoed: args.message })
/// }
/// }
/// }
/// ```
/// The `#[service_mux_struct]` macro is applied to a **struct** to implement `ServiceTrait` on it.
/// It acts as a dispatcher, routing `serve()` calls to the correct service based on the `req.service` field.
///
/// Each field in the struct must hold a service that implements `ServiceTrait` (e.g., wrapped in an `Arc`).
/// The macro generates a `serve` implementation that matches `req.service` against the field names of the struct.
///
/// # Usage
///
/// ```rust
/// use razor_rpc::server::{service, service_mux_struct};
/// use razor_rpc::error::RpcError;
/// use serde::{Deserialize, Serialize};
/// use std::sync::Arc;
///
/// // Define request/response types
/// #[derive(Debug, Deserialize, Serialize)]
/// pub struct MathArgs {
/// value: i32,
/// }
///
/// #[derive(Debug, Deserialize, Serialize)]
/// pub struct MathResp {
/// result: i32,
/// }
///
/// // Define service trait
/// pub trait MathService {
/// async fn add(&self, args: MathArgs) -> Result<MathResp, RpcError<()>>;
/// async fn multiply(&self, args: MathArgs) -> Result<MathResp, RpcError<()>>;
/// }
///
/// // Define services
/// pub struct AddService;
/// #[service]
/// impl MathService for AddService {
/// async fn add(&self, args: MathArgs) -> Result<MathResp, RpcError<()>> {
/// Ok(MathResp { result: args.value + 1 })
/// }
/// async fn multiply(&self, _args: MathArgs) -> Result<MathResp, RpcError<()>> {
/// unimplemented!()
/// }
/// }
///
/// pub struct MultiplyService;
/// #[service]
/// impl MathService for MultiplyService {
/// async fn add(&self, _args: MathArgs) -> Result<MathResp, RpcError<()>> {
/// unimplemented!()
/// }
/// async fn multiply(&self, args: MathArgs) -> Result<MathResp, RpcError<()>> {
/// Ok(MathResp { result: args.value * 2 })
/// }
/// }
///
/// // Create a service multiplexer
/// #[service_mux_struct]
/// pub struct ServiceMux {
/// pub add: Arc<AddService>,
/// pub multiply: Arc<MultiplyService>,
/// }
///
/// // Usage:
/// // let mux = ServiceMux {
/// // add: Arc::new(AddService),
/// // multiply: Arc::new(MultiplyService),
/// // };
/// ```
/// The `endpoint_client!` macro generates a client struct that can be used to make remote API calls.
///
/// This macro should be used first to define the client struct, then use `#[endpoint_async]`
/// to implement service traits for the client.
///
/// # Usage
///
/// ```rust
/// use razor_rpc::client::endpoint_client;
///
/// endpoint_client!(MyClient);
///
/// // This generates MyClient<C> struct with:
/// // - new(caller: C) constructor
/// // - Clone implementation
/// // - AsyncEndpoint<C> implementation
/// ```
/// The `#[endpoint_async]` macro applies to a trait to implement it for a client struct.
///
/// The client struct must be defined beforehand using `#[endpoint_client(ClientName)]`.
///
/// Rules:
/// - trait can be wrapped async_trait, optionally.
/// - If trait is not async_trait, then the method should use the signature `impl Future + Send`
/// - No fn is allowed.
/// - All method should have one and only argument, and return type should be `Result<Resp, RpcError<E>>`, where `E: RpcErrCodec`
///
/// # Usage
///
/// ```rust
/// use razor_rpc::client::{endpoint_client, endpoint_async};
/// use razor_rpc::error::RpcError;
/// use serde::{Deserialize, Serialize};
/// use std::future::Future;
///
/// #[derive(Debug, Deserialize, Serialize)]
/// pub struct AddArgs {
/// pub a: i32,
/// pub b: i32,
/// }
///
/// #[derive(Debug, Deserialize, Serialize, Default)]
/// pub struct AddResp {
/// pub result: i32,
/// }
///
/// // Step 1: Define the client struct
/// endpoint_client!(CalculatorClient);
///
/// // Step 2: Define a service trait and implement it for the client
/// #[endpoint_async(CalculatorClient)]
/// pub trait CalculatorService {
/// fn add(&self, args: AddArgs) -> impl Future<Output = Result<AddResp, RpcError<()>>> + Send;
/// }
///
/// // Usage:
/// // let client = CalculatorClient::new(caller);
/// // let result = client.add(AddArgs { a: 1, b: 2 }).await;
/// ```
///
/// The macro also supports `async fn` methods when used with `#[async_trait]`.