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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//!
//! # Maybe-Async-Cfg Procedure Macro
//!
//! **Why bother writing similar code twice for blocking and async code?**
//!
//! [](https://github.com/nvksv/maybe-async-cfg/actions)
//! [](./LICENSE)
//! [](https://crates.io/crates/maybe-async-cfg)
//! [](https://docs.rs/maybe-async-cfg)
//!
//! When implementing both sync and async versions of API in a crate, most API of the two version
//! are almost the same except for some async/await keyword.
//!
//! `maybe-async-cfg` help unifying async and sync implementation by **procedural macro**.
//! - Write async code with normal `async`, `await`, and let `maybe_async_cfg` handles those `async`
//! and `await` when you need a blocking code.
//! - Add `maybe` attributes and specify feature-based conditions under which sync or async code
//! should be generated.
//! - Use `only_if` (or `remove_if`) to keep code in specified version if necessary.
//!
//! The `maybe` procedural macro can be applied to the following codes:
//! - use declaration
//! - trait declaration
//! - trait implementation
//! - function definition
//! - struct and enum definition
//!
//! **RECOMMENDATION**: Enable **resolver ver2** in your crate, which is introduced in Rust 1.51. If
//! not, two crates in dependency with conflict version (one async and another blocking) can fail
//! complilation.
//!
//!
//! ## Motivation
//!
//! The async/await language feature alters the async world of rust. Comparing with the map/and_then
//! style, now the async code really resembles sync version code.
//!
//! In many crates, the async and sync version of crates shares the same API, but the minor
//! difference that all async code must be awaited prevent the unification of async and sync code.
//! In other words, we are forced to write an async and an sync implementation repectively.
//!
//!
//! ## Macros in Detail
//!
//! To use `maybe-async-cfg`, we must know which block of codes is only used on sync implementation,
//! and which on async. These two versions of the implementation should share the same function
//! signatures except for async/await keywords.
//!
//! Use `maybe` macro for code that is the same in both async and sync versions except for
//! async/await keywords. Specify in the macro parameters the conditions (based on features) under
//! which async and/or sync versions of the code should appear.
//!
//! - attribute macro **`maybe`**
//!
//! Offers a unified way to provide sync and async conversion on demand depending on features,
//! enabled for your crate, with **async first** policy.
//!
//! ```toml
//! [dependencies]
//! maybe_async_cfg = "0.1"
//!
//! [features]
//! use_sync = []
//! use_async = []
//! ```
//!
//! In this and all the following examples, we use two features. But you can use any conditions
//! that are convenient for you, for example, replacing `feature="use_sync"` with
//! `not(feature="use_async")` everywhere. Feel free, `maybe-async-cfg` does not analyze the
//! conditions in any way, just substituting them as is.
//!
//! Add the `maybe` attribute before all the items that need to be changed in different versions
//! of the code (sync or async).
//!
//! Want to keep async code? Specify the `async` parameter with the condition (based on
//! features) when your code should be async.
//!
//! Wanna convert async code to sync? Specify the `sync` parameter with the condition when the
//! sync code should be generated.
//!
//! ```rust, no_run
//! #[maybe_async_cfg::maybe(
//! idents(Foo),
//! sync(feature="use_sync"),
//! async(feature="use_async")
//! )]
//! struct Struct {
//! f: Foo,
//! }
//!
//! ```
//! After convertation:
//! ```rust, no_run
//! #[cfg(feature="use_sync")]
//! struct StructSync {
//! f: FooSync,
//! }
//! #[cfg(feature="use_async")]
//! struct StructAsync {
//! f: FooAsync,
//! }
//! ```
//!
//! - procedural macro **`content`**
//!
//! The `content` macro allows you to specify common parameters for many `maybe` macros. Use the
//! internal `default` attribute with the required parameters inside the `content` macro.
//!
//! ```rust, no_run
//! maybe_async_cfg::content!{
//! #![maybe_async_cfg::default(
//! idents(Foo, Bar),
//! )]
//!
//! #[maybe_async_cfg::maybe(sync(feature="use_sync"), async(feature="use_async"))]
//! struct Struct {
//! f: Foo,
//! }
//!
//! #[maybe_async_cfg::maybe(sync(feature="use_sync"), async(feature="use_async"))]
//! async fn func(b: Bar) {
//! todo!()
//! }
//! } // content!
//! ```
//! After convertation:
//! ```rust, no_run
//! #[cfg(feature="use_sync")]
//! struct StructSync {
//! f: FooSync,
//! }
//! #[cfg(feature="use_async")]
//! struct StructAsync {
//! f: FooAsync,
//! }
//!
//! #[cfg(feature="use_sync")]
//! fn func_sync(b: BarSync) {
//! todo!()
//! }
//! #[cfg(feature="use_async")]
//! async fn func_async(b: BarAsync) {
//! todo!()
//! }
//! ```
//!
//!
//! ## Examples
//!
//! ### rust client for services
//!
//! When implementing rust client for any services, like awz3. The higher level API of async and
//! sync version is almost the same, such as creating or deleting a bucket, retrieving an object and
//! etc.
//!
//! The example `service_client` is a proof of concept that `maybe_async_cfg` can actually free us
//! from writing almost the same code for sync and async. We can toggle between a sync AWZ3 client
//! and async one by `is_sync` feature gate when we add `maybe-async-cfg` to dependency.
//!
//!
//! ## Аppreciations
//!
//! This crate is a redesigned fork of these wonderful crates:
//!
//! - [fMeow/maybe-async-rs](https://github.com/fMeow/maybe-async-rs)
//!
//! - [marioortizmanero/maybe-async-rs](https://github.com/fMeow/maybe-async-rs)
//!
//! Thanks!
//!
//!
//! # License
//! MIT
use TokenStream;
use proc_macro_error;
const DEFAULT_CRATE_NAME: &'static str = "maybe_async_cfg";
const STANDARD_MACROS: &'static = &;
/// Marks the code that can be presented in several versions.
///
/// ### The `maybe` macro has the following parameters:
///
/// - `disable`
///
/// The macro with `disable` parameter will do nothing, like `noop`. Use it to write and debug
/// initial async code.
///
/// - `prefix`
///
/// The name of `maybe-async-cfg` crate. If not set, `"maybe_async_cfg"` will be used.
///
/// - `sync`, `async`
///
/// Defines versions of the code: the item to which the attribute `maybe` refers will be
/// replaced with multiple copies (one for each version), which will be modified according to
/// the version kind and its parameters.
///
/// For the `sync` version, the item will be converted from async to sync code by deleting
/// the `async` and `await` keywords. The types `Future<Output=XXX>` will also be replaced with just
/// `XXX`. For the `async` version, the item will be left async.
///
/// In any case, the item will be converted according to all the parameters described below. For
/// functions, structs/enums and traits, the name will be changed as if it is mentioned in the
/// `idents` list (if it is not explicitly specified there and if `keep_self` is not present).
///
/// - All other parameters will be passed to all versions (with merging).
///
/// Therefore, those parts of the version parameters that match in all versions can be specified
/// here. For example, this is the expected behavior for the `idents` list.
///
/// ### Every version has the following parameters:
///
/// - `key`
///
/// Defines unique name of the version to use it in `only_if`/`remove_if` conditions. If
/// omitted, `sync`/`async` will be used.
///
/// ```rust, no_run
/// #[maybe_async_cfg::maybe(
/// sync(key="foo", feature="use_sync"),
/// async(key="bar", feature="use_async"),
/// )]
/// struct Struct {
/// f: usize,
///
/// // This field will only be present in sync version
/// #[maybe_async_cfg::only_if(key="foo")]
/// sync_only_field: bool,
/// }
/// ```
/// After convertation:
/// ```rust, no_run
/// #[cfg(feature="use_sync")]
/// struct StructSync {
/// f: usize,
/// sync_only_field: bool,
/// }
/// #[cfg(feature="use_async")]
/// struct StructAsync {
/// f: usize,
/// }
/// ```
///
/// - `cfg`
///
/// Defines the condition (based on features), under which the current version should appear.
///
/// Note: conditions like `feature = "..."`, `not(...)`, `all(...)`, `any(...)` will be
/// processed correctly, even if the `cfg(...)` was omitted.
///
/// ```rust, no_run
/// #[maybe_async_cfg::maybe(
/// sync(cfg(feature="use_sync")),
/// async(feature="use_async")
/// )]
/// struct Struct {
/// f: Foo,
/// }
///
/// ```
/// After convertation:
/// ```rust, no_run
/// #[cfg(feature="use_sync")]
/// struct StructSync {
/// f: Foo,
/// }
/// #[cfg(feature="use_async")]
/// struct StructAsync {
/// f: Foo,
/// }
/// ```
///
/// - `idents`
///
/// Defines a list of identifiers that should be renamed depending on the version of the code.
///
/// Each identifier can have the following clarifying parameters:
///
/// - `fn`
///
/// means that this is the name of the function and it should be converted by adding the
/// suffixes `"_sync"`/`"_async"` (otherwise, the suffixes `"Sync"`/`"Async"` will be used).
///
/// - `use`
///
/// in `use` lists, using this identifier will result in renaming via the `as` expression,
/// rather than a simple replacement as is. In other cases, a simple replacement will be used.
///
/// - `keep`
///
/// this identifier will not be converted anywhere
///
/// - `sync`, `async`
///
/// specifies the name that will be used in the corresponding version of the code. Overrides
/// the standard scheme of suffixes used by default. If the parameter value is omitted,
/// the identifier will not be renamed in this case.
///
/// ```rust, no_run
/// #[maybe_async_cfg::maybe(
/// idents(
/// Foo,
/// Bar,
/// baz(fn),
/// Qux(use),
/// waldo(sync, async="async_waldo"),
/// xyzzy(fn, use, sync="xizzy_the_sync_func"),
/// ),
/// sync(feature="use_sync"),
/// async(feature="use_async"),
/// )]
/// async fn func() {
/// struct Foo {}
/// use extcrate::{
/// Bar,
/// baz,
/// Qux,
/// waldo::{
/// plugh,
/// xyzzy
/// }
/// };
/// let _ = baz( Foo {}, Bar::new() ).await;
/// let _ = xizzy( Qux::flob(b).await );
/// }
/// ```
/// After convertation:
/// ```rust, no_run
/// #[cfg(feature="use_sync")]
/// fn func_sync() {
/// struct FooSync {}
/// use extcrate::{
/// BarSync,
/// baz_sync,
/// Qux as QuxSync,
/// waldo::{
/// plugh,
/// xyzzy as xizzy_the_sync_func
/// }
/// };
/// let _ = baz_sync( FooSync {}, BarAsync::new() );
/// let _ = xizzy_the_sync_func( QuxSync::flob() );
/// }
/// #[cfg(feature="use_async")]
/// async fn func_async() {
/// struct FooAsync {}
/// use extcrate::{
/// BarAsync,
/// baz_async,
/// Qux as QuxAsync,
/// async_waldo::{
/// plugh,
/// xyzzy as xyzzy_async
/// }
/// };
/// let _ = baz_async( FooAsync {}, BarAsync::new() ).await;
/// let _ = xyzzy_async( QuxAsync::flob().await );
/// }
/// ```
///
/// - `keep_self`
///
/// Do not change name of item to which attribute `maybe` refers.
///
/// - `send`
///
/// If `send = "Send"` or `send = "true"` is present, the attribute
/// `#[async_trait::async_trait]` will be added before the async code. If `send = "?Send"` or
/// `send = "false"` then `#[async_trait::async_trait(?Send)]` will be added.
///
/// - `drop_attrs`
///
/// Remove any attributes with specified names.
///
/// ```rust, no_run
/// #[maybe_async_cfg::maybe(
/// sync(feature="use_sync", drop_attrs(attr)),
/// async(feature="use_async"),
/// )]
/// struct Struct {
/// f: usize,
///
/// // This attribute will be removed in sync version
/// #[attr(param)]
/// field1: bool,
/// }
/// ```
/// After convertation:
/// ```rust, no_run
/// #[cfg(feature="use_sync")]
/// struct StructSync {
/// f: usize,
/// field1: bool,
/// }
/// #[cfg(feature="use_async")]
/// struct StructAsync {
/// f: usize,
/// #[attr(param)]
/// field1: bool,
/// }
/// ```
///
/// - `replace_features`
///
/// Replace one feature name with another.
///
/// ```rust, no_run
/// #[maybe_async_cfg::maybe(
/// sync(feature="use_sync", replace_features(secure(secure_sync))),
/// async(feature="use_async"),
/// )]
/// struct Struct {
/// f: usize,
/// // In sync version "secure" feature will be replaced with "secure_sync" feature
/// #[cfg(feature="secure")]
/// field: bool,
/// }
/// ```
/// After convertation:
/// ```rust, no_run
/// #[cfg(feature="use_sync")]
/// struct StructSync {
/// f: usize,
/// #[cfg(feature="secure_sync")]
/// field: bool,
/// }
/// #[cfg(feature="use_async")]
/// struct StructAsync {
/// f: usize,
/// #[cfg(feature="secure")]
/// field: bool,
/// }
/// ```
///
/// - `inner`, `outer`
///
/// Adds some attributes to the generated code. Inner attributes will appear below attribute
/// `#[cfg(...)]`, outer attributes will appear above it.
///
/// Note: if the version parameter is not parsed as a parameter of some other type, it will be
/// interpreted as an inner attribute.
///
/// Useful for testing: just write `test` in version parameters.
///
/// ```rust, no_run
/// #[maybe_async_cfg::maybe(
/// sync(feature="secure_sync", test, "resource(path = \"/foo/bar\")", outer(xizzy)),
/// async(feature="secure_sync", inner(baz(qux), async_attributes::test)),
/// )]
/// async fn test_func() {
/// todo!()
/// }
/// ```
/// After convertation:
/// ```rust, no_run
/// #[xizzy]
/// #[cfg(feature="use_sync")]
/// #[test]
/// #[resource(path = "/foo/bar")]
/// fn test_func_sync() {
/// todo!()
/// }
/// #[cfg(feature="use_async")]
/// #[baz(qux)]
/// #[async_attributes::test]
/// async fn test_func_async() {
/// todo!()
/// }
/// ```
///
/// - In other cases, the following rules apply:
///
/// - name-value pairs (`xxx = "yyy"`) with a name other than `key`, `prefix`, `send` and
/// `feature` will produce an error.
///
/// - `feature = "..."`, `not(...)`, `all(...)`, `any(...)` will be interpreted as condition for
/// current version (as wrapped in `cfg(...)`).
///
/// - all another parameters will be interpreted as inner attribute for current version (as
/// wrapped in `inner(...)`).
///
/// Process marked async code. **Internal macro, don't use directly.**
/// Convert marked async code to sync code. **Internal macro, don't use directly.**
/// Marks conditional content that should only be used in the specified version of the code.
/// Marks conditional content that should be used in all versions of the code except the specified
/// one.
/// Do nothing.
/// Remove marked content.
/// A wrapper for code with common `maybe` parameters
///
/// The `content` macro allows you to specify common parameters for many `maybe` macros. Use the
/// internal `default` attribute with the required parameters inside the `content` macro.
///
/// ```rust, no_run
/// maybe_async_cfg::content!{
/// #![maybe_async_cfg::default(
/// idents(Foo, Bar),
/// )]
///
/// #[maybe_async_cfg::maybe(sync(feature="use_sync"), async(feature="use_async"))]
/// struct Struct {
/// f: Foo,
/// }
///
/// #[maybe_async_cfg::maybe(sync(feature="use_sync"), async(feature="use_async"))]
/// async fn func(b: Bar) {
/// todo!()
/// }
/// } // content!
/// ```
/// After convertation:
/// ```rust, no_run
/// #[cfg(feature="use_sync")]
/// struct StructSync {
/// f: FooSync,
/// }
/// #[cfg(feature="use_async")]
/// struct StructAsync {
/// f: FooAsync,
/// }
///
/// #[cfg(feature="use_sync")]
/// fn func_sync(b: BarSync) {
/// todo!()
/// }
/// #[cfg(feature="use_async")]
/// async fn func_async(b: BarAsync) {
/// todo!()
/// }
/// ```
///