postcard_rpc/
macros.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/// ## Endpoint macro
///
/// Used to define a single Endpoint marker type that implements the
/// [Endpoint][crate::Endpoint] trait.
///
/// Prefer the [`endpoints!()`][crate::endpoints] macro instead.
///
/// ```rust
/// # use postcard_schema::Schema;
/// # use serde::{Serialize, Deserialize};
/// use postcard_rpc::endpoint;
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Req1 {
///     a: u8,
///     b: u64,
/// }
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Resp1 {
///     c: [u8; 4],
///     d: i32,
/// }
///
/// endpoint!(Endpoint1, Req1, Resp1, "endpoint/1");
/// ```
///
/// If the path is omitted, the type name is used instead.
#[macro_export]
macro_rules! endpoint {
    ($tyname:ident, $req:ty, $resp:ty) => {
        endpoint!($tyname, $req, $resp, stringify!($tyname));
    };
    ($tyname:ident, $req:ty, $resp:ty, $path:expr,) => {
        endpoint!($tyname, $req, $resp, $path)
    };
    ($tyname:ident, $req:ty, $resp:ty, $path:expr) => {
        pub struct $tyname;

        impl $crate::Endpoint for $tyname {
            type Request = $req;
            type Response = $resp;
            const PATH: &'static str = $path;
            const REQ_KEY: $crate::Key = $crate::Key::for_path::<$req>($path);
            const RESP_KEY: $crate::Key = $crate::Key::for_path::<$resp>($path);
        }
    };
}

/// ## Endpoints macro
///
/// Used to define multiple Endpoint marker types that implements the
/// [Endpoint][crate::Endpoint] trait.
///
/// ```rust
/// # use postcard_schema::Schema;
/// # use serde::{Serialize, Deserialize};
/// use postcard_rpc::endpoints;
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Req1 {
///     a: u8,
///     b: u64,
/// }
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Resp1 {
///     c: [u8; 4],
///     d: i32,
/// }
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Req2 {
///     a: i8,
///     b: i64,
/// }
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Resp2 {
///     c: [i8; 4],
///     d: u32,
/// }
///
/// endpoints!{
///     list = ENDPOINTS_LIST;
///     | EndpointTy     | RequestTy     | ResponseTy    | Path              |
///     | ----------     | ---------     | ----------    | ----              |
///     | Endpoint1      | Req1          | Resp1         | "endpoints/one"   |
///     | Endpoint2      | Req2          | Resp2         | "endpoints/two"   |
/// }
/// ```
#[macro_export]
macro_rules! endpoints {
    (@ep_tys $([[$($meta:meta)?] $ep_name:ident])*) => {
        $crate::endpoints!(@ep_tys omit_std=false; $([[$($meta)?] $ep_name])*)
    };
    (@ep_tys omit_std=true; $([[$($meta:meta)?] $ep_name:ident])*) => {
        const {
            const LISTS: &[&[&'static postcard_schema::schema::NamedType]] = &[
                $(
                    $(#[$meta])?
                    $crate::unique_types!(<$ep_name as $crate::Endpoint>::Request),
                    $(#[$meta])?
                    $crate::unique_types!(<$ep_name as $crate::Endpoint>::Response),
                )*
            ];

            const TTL_COUNT: usize = $crate::uniques::total_len(LISTS);
            const BIG_RPT: ([Option<&'static postcard_schema::schema::NamedType>; TTL_COUNT], usize) = $crate::uniques::merge_nty_lists(LISTS);
            const SMALL_RPT: [&'static postcard_schema::schema::NamedType; BIG_RPT.1] = $crate::uniques::cruncher(BIG_RPT.0.as_slice());
            SMALL_RPT.as_slice()
        }
    };
    (@ep_tys omit_std=false; $([[$($meta:meta)?] $ep_name:ident])*) => {
        const {
            const USER_TYS: &[&'static postcard_schema::schema::NamedType] =
                $crate::endpoints!(@ep_tys omit_std=true; $([[$($meta)?] $ep_name])*);
            const STD_TYS: &[&'static postcard_schema::schema::NamedType]
                = $crate::standard_icd::STANDARD_ICD_ENDPOINTS.types;

            const BOTH: &[&[&'static postcard_schema::schema::NamedType]] = &[
                USER_TYS, STD_TYS,
            ];
            const TTL_COUNT: usize = $crate::uniques::total_len(BOTH);
            const BIG_RPT: ([Option<&'static postcard_schema::schema::NamedType>; TTL_COUNT], usize) = $crate::uniques::merge_nty_lists(BOTH);
            const SMALL_RPT: [&'static postcard_schema::schema::NamedType; BIG_RPT.1] = $crate::uniques::cruncher(BIG_RPT.0.as_slice());
            SMALL_RPT.as_slice()
        }
    };
    (@ep_eps $([[$($meta:meta)?] $ep_name:ident])*) => {
        $crate::endpoints!(@ep_eps omit_std=false; $([[$($meta)?] $ep_name])*)
    };
    (@ep_eps omit_std=true; $([[$($meta:meta)?] $ep_name:ident])*) => {
        &[
            $(
                $(#[$meta])?
                (
                    <$ep_name as $crate::Endpoint>::PATH,
                    <$ep_name as $crate::Endpoint>::REQ_KEY,
                    <$ep_name as $crate::Endpoint>::RESP_KEY,
                ),
            )*
        ]
    };
    (@ep_eps omit_std=false; $([[$($meta:meta)?] $ep_name:ident])*) => {
        const {
            const USER_EPS: &[(&str, $crate::Key, $crate::Key)] =
                $crate::endpoints!(@ep_eps omit_std=true; $([[$($meta)?] $ep_name])*);
            const NULL_KEY: $crate::Key = unsafe { $crate::Key::from_bytes([0u8; 8]) };
            const STD_EPS: &[(&str, $crate::Key, $crate::Key)] =
                $crate::standard_icd::STANDARD_ICD_ENDPOINTS.endpoints;

            $crate::concat_arrays! {
                init = ("", NULL_KEY, NULL_KEY);
                ty = (&str, $crate::Key, $crate::Key);
                [STD_EPS, USER_EPS]
            }
        }
    };
    (
           list = $list_name:ident;
           $(omit_std = $omit:tt;)?
           | EndpointTy     | RequestTy                                | ResponseTy                                  | Path              | $( Cfg           |)?
           | $(-)*          | $(-)*                                    | $(-)*                                       | $(-)*             | $($(-)*          |)?
        $( | $ep_name:ident | $req_ty:tt $(< $($req_lt:lifetime),+ >)? | $resp_ty:tt $(< $($resp_lt:lifetime),+ >)?  | $path_str:literal | $($meta:meta)? $(|)? )*
    ) => {
        // struct definitions and trait impls
        $(
            /// Macro Generated Marker Type
            $(#[$meta])?
            pub struct $ep_name < $($($req_lt,)+)? $($($resp_lt,)+)? > {
                $(
                    _plt_req: core::marker::PhantomData<($(& $req_lt (),)+)>,
                )?
                $(
                    _plt_resp: core::marker::PhantomData<($(& $resp_lt (),)+)>,
                )?
                _priv: core::marker::PhantomData<()>,
            }

            $(#[$meta])?
            impl < $($($req_lt,)+)? $($($resp_lt,)+)? > $crate::Endpoint for $ep_name < $($($req_lt,)+)? $($($resp_lt,)+)? > {
                type Request = $req_ty $(< $($req_lt,)+ >)?;
                type Response = $resp_ty $(< $($resp_lt,)+ >)?;
                const PATH: &'static str = $path_str;
                const REQ_KEY: $crate::Key = $crate::Key::for_path::<$req_ty>($path_str);
                const RESP_KEY: $crate::Key = $crate::Key::for_path::<$resp_ty>($path_str);
            }
        )*

        /// Macro Generated Endpoint Map
        pub const $list_name: $crate::EndpointMap = $crate::EndpointMap {
            types: $crate::endpoints!(@ep_tys $(omit_std = $omit;)? $([[$($meta)?] $ep_name])*),
            endpoints: $crate::endpoints!(@ep_eps $(omit_std = $omit;)? $([[$($meta)?] $ep_name])*),
        };
    };
}

/// ## Topic macro
///
/// Used to define a single Topic marker type that implements the
/// [Topic][crate::Topic] trait.
///
/// Prefer the [`topics!()` macro](crate::topics) macro instead.
///
/// ```rust
/// # use postcard_schema::Schema;
/// # use serde::{Serialize, Deserialize};
/// use postcard_rpc::topic;
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Message1 {
///     a: u8,
///     b: u64,
/// }
///
/// topic!(Topic1, Message1, "topic/1");
/// ```
///
/// If the path is omitted, the type name is used instead.
#[macro_export]
macro_rules! topic {
    ($tyname:ident, $msg:ty) => {
        topic!($tyname, $msg, stringify!($tyname));
    };
    ($tyname:ident, $msg:ty, $path:expr,) => {
        topic!($tyname, $msg, $path)
    };
    ($tyname:ident, $msg:ty, $path:expr) => {
        /// $tyname - A Topic definition type
        ///
        /// Generated by the `topic!()` macro
        pub struct $tyname;

        impl $crate::Topic for $tyname {
            type Message = $msg;
            const PATH: &'static str = $path;
            const TOPIC_KEY: $crate::Key = $crate::Key::for_path::<$msg>($path);
        }
    };
}

/// ## Topics macro
///
/// Used to define multiple Topic marker types that implements the
/// [Topic][crate::Topic] trait.
///
/// ```rust
/// # use postcard_schema::Schema;
/// # use serde::{Serialize, Deserialize};
/// use postcard_rpc::{topics, TopicDirection};
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Message1 {
///     a: u8,
///     b: u64,
/// }
///
/// #[derive(Debug, Serialize, Deserialize, Schema)]
/// pub struct Message2 {
///     a: i8,
///     b: i64,
/// }
///
/// topics!{
///    list = TOPIC_LIST_NAME;
///    direction = TopicDirection::ToServer;
///    | TopicTy        | MessageTy     | Path              |
///    | -------        | ---------     | ----              |
///    | Topic1         | Message1      | "topics/one"      |
///    | Topic2         | Message2      | "topics/two"      |
/// }
/// ```

#[macro_export]
macro_rules! topics {
    (@tp_tys ( $dir:expr ) $([[$($meta:meta)?] $tp_name:ident])*) => {
        $crate::topics!(@tp_tys ( $dir ) omit_std=false; $([[$($meta)?] $tp_name])*)
    };
    (@tp_tys ( $dir:expr ) omit_std=true; $([[$($meta:meta)?] $tp_name:ident])*) => {
        const {
            const LISTS: &[&[&'static postcard_schema::schema::NamedType]] = &[
                $(
                    $(#[$meta])?
                    $crate::unique_types!(<$tp_name as $crate::Topic>::Message),
                )*
            ];

            const TTL_COUNT: usize = $crate::uniques::total_len(LISTS);
            const BIG_RPT: ([Option<&'static postcard_schema::schema::NamedType>; TTL_COUNT], usize) = $crate::uniques::merge_nty_lists(LISTS);
            const SMALL_RPT: [&'static postcard_schema::schema::NamedType; BIG_RPT.1] = $crate::uniques::cruncher(BIG_RPT.0.as_slice());
            SMALL_RPT.as_slice()
        }
    };
    (@tp_tys ( $dir:expr ) omit_std=false; $([[$($meta:meta)?] $tp_name:ident])*) => {
        const {
            const USER_TYS: &[&'static postcard_schema::schema::NamedType] =
                $crate::topics!(@tp_tys ( $dir ) omit_std=true; $([[$($meta)?] $tp_name])*);
            const STD_TYS: &[&'static postcard_schema::schema::NamedType] = const {
                match $dir {
                    $crate::TopicDirection::ToServer => $crate::standard_icd::STANDARD_ICD_TOPICS_IN.types,
                    $crate::TopicDirection::ToClient => $crate::standard_icd::STANDARD_ICD_TOPICS_OUT.types,
                }
            };

            const BOTH: &[&[&'static postcard_schema::schema::NamedType]] = &[
                STD_TYS, USER_TYS,
            ];
            const TTL_COUNT: usize = $crate::uniques::total_len(BOTH);
            const BIG_RPT: ([Option<&'static postcard_schema::schema::NamedType>; TTL_COUNT], usize) = $crate::uniques::merge_nty_lists(BOTH);
            const SMALL_RPT: [&'static postcard_schema::schema::NamedType; BIG_RPT.1] = $crate::uniques::cruncher(BIG_RPT.0.as_slice());
            SMALL_RPT.as_slice()
        }
    };
    (@tp_tps ( $dir:expr ) $([[$($meta:meta)?] $tp_name:ident])*) => {
        $crate::topics!(@tp_tps ( $dir ) omit_std=false; $([[$($meta)?] $tp_name])*)
    };
    (@tp_tps ( $dir:expr ) omit_std=true; $([[$($meta:meta)?] $tp_name:ident])*) => {
        &[
            $(
                $(#[$meta])?
                (
                    <$tp_name as $crate::Topic>::PATH,
                    <$tp_name as $crate::Topic>::TOPIC_KEY,
                ),
            )*
        ]
    };
    (@tp_tps ( $dir:expr ) omit_std=false; $([[$($meta:meta)?] $tp_name:ident])*) => {
        const {
            const USER_TPS: &[(&str, $crate::Key)] =
                $crate::topics!(@tp_tps ( $dir ) omit_std=true; $([[$($meta)?] $tp_name])*);
            const NULL_KEY: $crate::Key = unsafe { $crate::Key::from_bytes([0u8; 8]) };
            const STD_TPS: &[(&str, $crate::Key)] = const {
                match $dir {
                    $crate::TopicDirection::ToServer => $crate::standard_icd::STANDARD_ICD_TOPICS_IN.topics,
                    $crate::TopicDirection::ToClient => $crate::standard_icd::STANDARD_ICD_TOPICS_OUT.topics,
                }
            };

            $crate::concat_arrays! {
                init = ("", NULL_KEY);
                ty = (&str, $crate::Key);
                [STD_TPS, USER_TPS]
            }
        }
    };
    (
        list = $list_name:ident;
        direction = $direction:expr;
        $(omit_std = $omit:tt;)?
        | TopicTy        | MessageTy                                | Path              | $( Cfg           |)?
        | $(-)*          | $(-)*                                    | $(-)*             | $($(-)*          |)?
      $(| $tp_name:ident | $msg_ty:tt $(< $($msg_lt:lifetime),+ >)? | $path_str:literal | $($meta:meta)? $(|)?)*
    ) => {
        // struct definitions and trait impls
        $(
            /// $tp_name - A Topic definition type
            ///
            /// Generated by the `topics!()` macro
            $(#[$meta])?
            pub struct $tp_name $(< $($msg_lt,)+ >)? {
                $(
                    _plt: core::marker::PhantomData<($(& $msg_lt (),)+)>,
                )?
                _priv: core::marker::PhantomData<()>,
            }

            $(#[$meta])?
            impl $(< $($msg_lt),+ >)? $crate::Topic for $tp_name $(< $($msg_lt,)+ >)? {
                type Message = $msg_ty $(< $($msg_lt,)+ >)?;
                const PATH: &'static str = $path_str;
                const TOPIC_KEY: $crate::Key = $crate::Key::for_path::<$msg_ty>($path_str);
            }
        )*

        /// Macro Generated Topic Map
        pub const $list_name: $crate::TopicMap = $crate::TopicMap {
            direction: $direction,
            types: $crate::topics!(@tp_tys ( $direction ) $(omit_std = $omit;)? $([[$($meta)?] $tp_name])*),
            topics: $crate::topics!(@tp_tps ( $direction ) $(omit_std = $omit;)? $([[$($meta)?] $tp_name])*),
        };
    };
}

/// A macro for turning `&[&[T]]` into `&[T]`
#[macro_export]
macro_rules! concat_arrays {
    (
        init = $init:expr;
        ty = $tyname:ty;
        [$($arr:ident),+]
    ) => {
        const {
            const SLI: &[&[$tyname]] = &[
                $($arr,)+
            ];
            const LEN: usize = $crate::uniques::total_len(SLI);
            const ARR: [$tyname; LEN] = $crate::uniques::combine_with_copy(SLI, $init);

            ARR.as_slice()
        }
    };
}

#[cfg(test)]
mod concat_test {
    #[test]
    fn concats() {
        const A: &[u32] = &[1, 2, 3];
        const B: &[u32] = &[4, 5, 6];
        const BOTH: &[u32] = concat_arrays!(
            init = 0xFFFF_FFFF;
            ty = u32;
            [A, B]
        );
        assert_eq!(BOTH, [1, 2, 3, 4, 5, 6]);
    }
}

/// A helper function for logging with the [Sender][crate::server::Sender]
#[macro_export]
macro_rules! sender_fmt {
    ($sender:ident, $($arg:tt)*) => {
        $sender.log_fmt(format_args!($($arg)*))
    };
    ($($arg:tt)*) => {
        compile_error!("You must pass the sender to `sender_log`!");
    }
}

#[cfg(test)]
mod endpoints_test {
    use postcard_schema::{schema::owned::OwnedNamedType, Schema};
    use serde::{Deserialize, Serialize};

    #[derive(Serialize, Deserialize, Schema)]
    pub struct AReq(pub u8);
    #[derive(Serialize, Deserialize, Schema)]
    pub struct AResp(pub u16);
    #[derive(Serialize, Deserialize, Schema)]
    pub struct BTopic(pub u32);

    endpoints! {
        list = ENDPOINT_LIST;
        | EndpointTy     | RequestTy     | ResponseTy    | Path              |
        | ----------     | ---------     | ----------    | ----              |
        | AlphaEndpoint1 | AReq          | AResp         | "test/alpha1"     |
        | AlphaEndpoint2 | AReq          | AResp         | "test/alpha2"     |
        | AlphaEndpoint3 | AReq          | AResp         | "test/alpha3"     |
    }

    topics! {
        list = TOPICS_IN_LIST;
        direction = crate::TopicDirection::ToServer;
        | TopicTy        | MessageTy     | Path              |
        | ----------     | ---------     | ----              |
        | BetaTopic1     | BTopic        | "test/in/beta1"   |
        | BetaTopic2     | BTopic        | "test/in/beta2"   |
        | BetaTopic3     | BTopic        | "test/in/beta3"   |
    }

    topics! {
        list = TOPICS_OUT_LIST;
        direction = crate::TopicDirection::ToClient;
        | TopicTy        | MessageTy     | Path              |
        | ----------     | ---------     | ----              |
        | BetaTopic4     | BTopic        | "test/out/beta1"  |
    }

    #[test]
    fn eps() {
        for ep in ENDPOINT_LIST.types {
            println!("{}", OwnedNamedType::from(*ep));
        }
        assert_eq!(ENDPOINT_LIST.types.len(), 3);
        for ep in ENDPOINT_LIST.endpoints {
            println!("{}", ep.0);
        }
        assert_eq!(ENDPOINT_LIST.endpoints.len(), 5);
    }

    #[test]
    fn tps() {
        for tp in TOPICS_IN_LIST.types {
            println!("TY IN:  {}", OwnedNamedType::from(*tp));
        }
        for tp in TOPICS_IN_LIST.topics {
            println!("TP IN:  {}", tp.0);
        }
        for tp in TOPICS_OUT_LIST.types {
            println!("TY OUT: {}", OwnedNamedType::from(*tp));
        }
        for tp in TOPICS_OUT_LIST.topics {
            println!("TP OUT: {}", tp.0);
        }
        assert_eq!(TOPICS_IN_LIST.types.len(), 1);
        assert_eq!(TOPICS_IN_LIST.topics.len(), 3);
        assert_eq!(TOPICS_OUT_LIST.types.len(), 5);
        assert_eq!(TOPICS_OUT_LIST.topics.len(), 3);
    }
}