plexus-macros 0.7.0

Procedural macros for Plexus RPC activations
Documentation
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! MACRO-13 · `#[activation]` needs nothing.
//!
//! On `impl FooActivation` the stem `Foo` names the namespace (`foo`), the
//! trait (`trait Foo`, always emitted — the owner's `always`, 2026-10-16) and
//! the client (`FooClient<T>`); `trait = "Bar"` names them whatever the type
//! is called. Every `pub async fn` on `&self` returning `impl Stream<Item =
//! E>` is a wire method (`methods = marked` restores the `#[method]`-only
//! rule). A crate that declares a `server` feature gets its server half gated
//! on it. A bodiless activation is a contract: the macro emits
//! `FooActivation<D: Foo>`, delegating to `D`, and `Foo::server(self)`.
//!
//! A type not named `…Activation` and without `trait = "…"` cannot be named,
//! and is refused — except an activation written before MACRO-13, which says
//! its `namespace`: it expands as it did (no trait, the client on the
//! `client` flag, only `#[method]`s on the wire), with a deprecation warning
//! asking for the rename, so that every repository that takes the macro by
//! path keeps building until its own rename lands.

use crate::parse::{extract_stream_item_type, HubMethodsAttrs, MethodInfo};
use proc_macro2::{Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{format_ident, quote};
use syn::{
    parse::{Parse, ParseStream},
    FnArg, ImplItem, ImplItemFn, Signature, Token, Type, Visibility,
};

const SUFFIX: &str = "Activation";

/// What the name rule makes of an activation.
pub(crate) struct Names {
    /// The rule applies: the type is named `…Activation`, or the attribute
    /// names the trait. `false` only for an activation written before
    /// MACRO-13 (a type the rule cannot name, with its `namespace` given).
    pub new_form: bool,
    /// The trait's name — the stem, or `trait = "…"`. `None` exactly when
    /// `new_form` is false.
    pub trait_ident: Option<syn::Ident>,
    pub namespace: String,
    pub client_ident: syn::Ident,
}

fn refusal(struct_name: &syn::Ident) -> syn::Error {
    syn::Error::new(
        struct_name.span(),
        format!(
            "`{struct_name}` does not end in `Activation`, so #[plexus_macros::activation] cannot \
             name the trait, the client or the namespace. Name the type `{struct_name}Activation`, \
             or write `trait = \"…\"` in the attribute."
        ),
    )
}

/// The stem the name rule takes from a type name: `FooActivation` → `Foo`.
pub(crate) fn stem_of(type_name: &str) -> Option<&str> {
    type_name.strip_suffix(SUFFIX).filter(|s| !s.is_empty())
}

impl Names {
    pub fn resolve(args: &HubMethodsAttrs, struct_name: &syn::Ident) -> syn::Result<Self> {
        let type_name = struct_name.to_string();
        let type_stem = stem_of(&type_name).map(|s| syn::Ident::new(s, struct_name.span()));
        let explicit = args
            .trait_attr
            .as_ref()
            .map(|lit| syn::Ident::new(&lit.value(), lit.span()));
        let stem = explicit.or(type_stem);

        let Some(stem) = stem else {
            // Before MACRO-13 every activation said its namespace; one that
            // still does keeps its old expansion, with a deprecation warning
            // (`legacy_name_warning`). One that does not cannot be named.
            return match &args.namespace {
                Some(ns) => Ok(Names {
                    new_form: false,
                    trait_ident: None,
                    namespace: ns.clone(),
                    client_ident: format_ident!("{}Client", struct_name),
                }),
                None => Err(refusal(struct_name)),
            };
        };

        Ok(Names {
            new_form: true,
            namespace: args.namespace.clone().unwrap_or_else(|| snake_case(&stem.to_string())),
            client_ident: format_ident!("{}Client", stem),
            trait_ident: Some(stem),
        })
    }
}

/// An activation written before MACRO-13 on a type the name rule cannot
/// name: it still builds, as it did, and says what to rename.
pub(crate) fn legacy_name_warning(struct_name: &syn::Ident) -> TokenStream {
    let span = struct_name.span();
    let const_name = format_ident!("_PLEXUS_MACROS_DEPRECATED_UNNAMED_ACTIVATION_{}", struct_name, span = span);
    let touch = format_ident!("_plexus_macros_touch_unnamed_activation_{}", struct_name, span = span);
    let note = format!(
        "plexus-macros (MACRO-13): `{struct_name}` does not end in `Activation`, so no trait is \
         emitted for it and its client is `{struct_name}Client`. Rename it `{struct_name}Activation` \
         (or write `trait = \"…\"`); then `namespace`, `client`, `crate_path`, `server_cfg` and \
         `#[method]` may all go. A later release refuses it."
    );
    quote::quote_spanned! { span =>
        #[doc(hidden)]
        #[allow(non_upper_case_globals, non_snake_case, dead_code)]
        #[deprecated(since = "0.6.0", note = #note)]
        const #const_name: () = ();

        #[doc(hidden)]
        #[allow(non_snake_case, dead_code)]
        fn #touch() {
            let _ = #const_name;
        }
    }
}

/// `TicketsApi` → `tickets_api`, `HTTPServer` → `http_server`.
pub(crate) fn snake_case(s: &str) -> String {
    let chars: Vec<char> = s.chars().collect();
    let mut out = String::new();
    for (i, &c) in chars.iter().enumerate() {
        if c.is_uppercase() {
            let prev = i.checked_sub(1).map(|j| chars[j]);
            let next = chars.get(i + 1).copied();
            let boundary = match prev {
                None => false,
                Some(p) if p == '_' => false,
                Some(p) if p.is_lowercase() || p.is_ascii_digit() => true,
                Some(p) if p.is_uppercase() => next.is_some_and(|n| n.is_lowercase()),
                _ => false,
            };
            if boundary {
                out.push('_');
            }
            out.extend(c.to_lowercase());
        } else {
            out.push(c);
        }
    }
    out
}

/// `pub async fn f(&self, …) -> T;` — a method with no body. syn hands it to
/// us as `ImplItem::Verbatim` (rustc refuses it only after expansion).
struct BodilessFn {
    attrs: Vec<syn::Attribute>,
    vis: Visibility,
    sig: Signature,
}

impl Parse for BodilessFn {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(syn::Attribute::parse_outer)?;
        let vis: Visibility = input.parse()?;
        let sig: Signature = input.parse()?;
        input.parse::<Token![;]>()?;
        Ok(BodilessFn { attrs, vis, sig })
    }
}

/// Turn every bodiless method of the impl into an `ImplItem::Fn` with a
/// placeholder body; return their names, in source order. The placeholder is
/// replaced by the contract's delegation, or the activation is refused.
pub(crate) fn take_bodiless(input_impl: &mut syn::ItemImpl) -> Vec<syn::Ident> {
    let mut names = Vec::new();
    for item in &mut input_impl.items {
        if let ImplItem::Verbatim(tokens) = item {
            if let Ok(f) = syn::parse2::<BodilessFn>(tokens.clone()) {
                names.push(f.sig.ident.clone());
                *item = ImplItem::Fn(ImplItemFn {
                    attrs: f.attrs,
                    vis: f.vis,
                    defaultness: None,
                    sig: f.sig,
                    block: syn::parse_quote!({ ::core::unreachable!() }),
                });
            }
        }
    }
    names
}

/// Under the name rule (and not `methods = marked`): is this unmarked method
/// on the wire? `pub async fn` on `&self` returning `impl Stream<Item = E>`
/// is; one returning anything else is refused; any other shape is a plain
/// method.
pub(crate) fn is_wire_shape(method: &ImplItemFn) -> syn::Result<bool> {
    let public = matches!(method.vis, Visibility::Public(_));
    let on_ref_self = matches!(
        method.sig.inputs.first(),
        Some(FnArg::Receiver(r)) if r.reference.is_some() && r.mutability.is_none()
    );
    if !(public && method.sig.asyncness.is_some() && on_ref_self) {
        return Ok(false);
    }
    let returns_stream = match &method.sig.output {
        syn::ReturnType::Type(_, ty) => extract_stream_item_type(ty).is_some(),
        syn::ReturnType::Default => false,
    };
    if returns_stream {
        return Ok(true);
    }
    let name = &method.sig.ident;
    let returns = match &method.sig.output {
        syn::ReturnType::Type(_, ty) => quote!(#ty).to_string(),
        syn::ReturnType::Default => "()".to_string(),
    };
    Err(syn::Error::new(
        name.span(),
        format!(
            "`pub async fn {name}(&self, …)` returns `{returns}`, not `impl Stream<Item = E>`, so \
             it cannot go on the wire — and every `pub async fn` on `&self` of an activation is a \
             wire method. Make `{name}` non-`pub`, or write `methods = marked` on the activation \
             (then only #[plexus_macros::method]s are on the wire)."
        ),
    ))
}

/// Does the consuming crate's manifest declare a `server` feature?
pub(crate) fn manifest_declares_server_feature() -> bool {
    let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") else { return false };
    let Ok(text) = std::fs::read_to_string(std::path::Path::new(&dir).join("Cargo.toml")) else {
        return false;
    };
    let mut table = String::new();
    for line in text.lines() {
        let line = line.split('#').next().unwrap_or("").trim();
        if line.starts_with('[') {
            table = line.trim_matches(|c| c == '[' || c == ']').trim().to_string();
            continue;
        }
        let Some((key, _)) = line.split_once('=') else { continue };
        let key = key.trim().trim_matches('"');
        if (table == "features" && key == "server") || (table.is_empty() && key == "features.server") {
            return true;
        }
    }
    false
}

/// The `BoxStream<'static, E>` a trait method returns: plexus-client's when
/// the crate has it, else futures' (directly or through the umbrella).
pub(crate) fn box_stream_path(client_path: Option<&syn::Path>, span: Span) -> syn::Result<TokenStream> {
    if let Some(client) = client_path {
        return Ok(quote! { #client::BoxStream });
    }
    if let Ok(found) = crate_name("futures") {
        let ident = match found {
            FoundCrate::Itself => format_ident!("futures"),
            FoundCrate::Name(n) => syn::Ident::new(&n, span),
        };
        return Ok(quote! { ::#ident::stream::BoxStream });
    }
    if let Some(umbrella) = crate::codegen::umbrella_crate(span) {
        return Ok(quote! { ::#umbrella::__deps::futures::stream::BoxStream });
    }
    Err(syn::Error::new(
        span,
        "#[plexus_macros::activation]: the activation's trait returns boxed streams, and this \
         crate depends on neither `plexus-client` nor `futures` to name one. Add `plexus-client` \
         (which also gives you the client) or `futures`.",
    ))
}

/// What the server injects into a method (and a caller never passes), if
/// anything: the activation cannot call such a method on itself, so it
/// cannot implement its own trait.
pub(crate) fn injected(m: &MethodInfo) -> Option<&'static str> {
    if !m.activation_params.is_empty() {
        Some("an `#[activation_param]`")
    } else if !m.auth_resolvers.is_empty() {
        Some("a `#[from_auth]` parameter")
    } else if m.requires_auth {
        Some("an `auth` parameter")
    } else if m.takes_reach {
        Some("a `reach` parameter")
    } else if !matches!(m.bidirectional, crate::parse::BidirType::None) {
        Some("a bidirectional `ctx` parameter")
    } else {
        None
    }
}

/// The checks a method must pass to be on the trait. The trait carries what
/// a caller passes; a method that also takes what the server injects is on
/// it (the client implements it), but the activation then does not implement
/// its own trait — it has no caller to take those from. Answers whether it
/// does: true when no wire method takes anything injected.
pub(crate) fn check_trait_methods(methods: &[MethodInfo], trait_ident: &syn::Ident) -> syn::Result<bool> {
    let mut first_event: Option<(String, &syn::Ident)> = None;
    let mut self_callable = true;
    for m in methods {
        if injected(m).is_some() {
            self_callable = false;
        }
        let Some(event) = &m.stream_item_type else {
            return Err(syn::Error::new(
                m.fn_name.span(),
                format!(
                    "`{}` does not return `impl Stream<Item = E>`, so trait `{trait_ident}` has no \
                     event type for it. Give it a stream return type.",
                    m.fn_name
                ),
            ));
        };
        let event_str = quote!(#event).to_string();
        match &first_event {
            None => first_event = Some((event_str, &m.fn_name)),
            Some((first, first_fn)) if *first != event_str => {
                return Err(syn::Error::new(
                    m.fn_name.span(),
                    format!(
                        "`{}` yields `{event_str}`, but `{first_fn}` yields `{first}`: the methods of \
                         trait `{trait_ident}` yield one event type across the activation.",
                        m.fn_name
                    ),
                ));
            }
            Some(_) => {}
        }
    }
    Ok(self_callable)
}

fn sig_params(m: &MethodInfo) -> (Vec<TokenStream>, Vec<&syn::Ident>) {
    let params = m
        .params
        .iter()
        .map(|p| {
            let (name, ty) = (&p.name, &p.ty);
            quote! { #name: #ty }
        })
        .collect();
    let names = m.params.iter().map(|p| &p.name).collect();
    (params, names)
}

fn event_ty(m: &MethodInfo) -> &Type {
    m.stream_item_type.as_ref().expect("checked by check_trait_methods")
}

/// `pub trait Foo: Send + Sync + 'static { fn m(&self, …) -> BoxStream<'static, E>; … }`,
/// its dyn-compatibility assertion, and — for a contract — `server()`.
pub(crate) fn trait_def(
    trait_ident: &syn::Ident,
    struct_name: &syn::Ident,
    description: &str,
    methods: &[MethodInfo],
    box_stream: &TokenStream,
    contract: bool,
    server_gate: Option<&syn::Meta>,
) -> TokenStream {
    let items = methods.iter().map(|m| {
        let fn_ident = &m.fn_name;
        let (params, _) = sig_params(m);
        let ev = event_ty(m);
        let doc = &m.description;
        let doc = if doc.is_empty() { quote! {} } else { quote! { #[doc = #doc] } };
        quote! {
            #doc
            #[allow(clippy::too_many_arguments)]
            fn #fn_ident(&self #(, #params)*) -> #box_stream<'static, #ev>;
        }
    });
    let server = if contract {
        let gate = server_gate.map(|m| quote! { #[cfg(#m)] });
        quote! {
            /// Serve this implementation on a hub: `hub.register(x.server())`.
            #gate
            fn server(self) -> #struct_name<Self>
            where
                Self: ::core::marker::Sized,
            {
                #struct_name(::std::sync::Arc::new(self))
            }
        }
    } else {
        quote! {}
    };
    let doc = if description.is_empty() {
        quote! {}
    } else {
        quote! { #[doc = #description] }
    };
    let assert_fn = format_ident!("__plexus_assert_{}_is_dyn_compatible", snake_case(&trait_ident.to_string()));
    quote! {
        #doc
        ///
        /// Emitted by `#[plexus_macros::activation]` (MACRO-13) from the
        /// activation's wire methods: implemented by the activation and by its
        /// client, so anything that implements it can be served and anything
        /// served can be called through it.
        pub trait #trait_ident: ::core::marker::Send + ::core::marker::Sync + 'static {
            #(#items)*
            #server
        }

        // MACRO-13: the trait is dyn-compatible by construction (boxed
        // streams, no generics, no `async fn`); this fails to compile the day
        // it is not.
        #[doc(hidden)]
        #[allow(dead_code, non_snake_case)]
        fn #assert_fn(_: &dyn #trait_ident) {}
    }
}

/// Server half, server form: `impl Foo for FooActivation`, each method the
/// inherent method's stream. The trait's stream is `'static` and the inherent
/// method's future borrows `self`, so the activation is cloned into the
/// stream (`Plexus::register` already asks every activation to be `Clone`).
pub(crate) fn activation_trait_impl(
    trait_ident: &syn::Ident,
    struct_name: &syn::Ident,
    self_ty: &Type,
    impl_generics: &syn::ImplGenerics,
    where_clause: Option<&syn::WhereClause>,
    methods: &[MethodInfo],
    box_stream: &TokenStream,
    deps: &syn::Ident,
) -> TokenStream {
    let items = methods.iter().map(|m| {
        let fn_ident = &m.fn_name;
        let (params, names) = sig_params(m);
        let ev = event_ty(m);
        quote! {
            #[allow(clippy::too_many_arguments)]
            fn #fn_ident(&self #(, #params)*) -> #box_stream<'static, #ev> {
                // The activation is cloned into the `'static` stream; a
                // type that is not `Clone` is told so, by name.
                #[diagnostic::on_unimplemented(
                    message = "`{Self}` must implement `Clone` to implement its activation trait",
                    label = "this activation is not `Clone`",
                    note = "#[plexus_macros::activation] (MACRO-13) implements the activation's trait by cloning the activation into each method's stream, as `register` does; derive or implement `Clone` for `{Self}` (wrap its state in `Arc`)"
                )]
                trait __PlexusActivationClone: ::core::marker::Sized {
                    fn __plexus_clone(&self) -> Self;
                }
                impl<__T: ::core::clone::Clone> __PlexusActivationClone for __T {
                    fn __plexus_clone(&self) -> Self {
                        ::core::clone::Clone::clone(self)
                    }
                }
                let __this = __PlexusActivationClone::__plexus_clone(self);
                ::std::boxed::Box::pin(#deps::futures::StreamExt::flatten(
                    #deps::futures::stream::once(async move {
                        #struct_name::#fn_ident(&__this #(, #names)*).await
                    }),
                ))
            }
        }
    });
    quote! {
        impl #impl_generics #trait_ident for #self_ty #where_clause {
            #(#items)*
        }
    }
}

/// The generic parameter of a contract's emitted struct.
pub(crate) fn contract_param() -> syn::Ident {
    format_ident!("__D")
}

/// Contract form: give each bodiless method its delegation, and make the
/// impl generic over the implementor.
pub(crate) fn rewrite_contract_impl(
    input_impl: &mut syn::ItemImpl,
    struct_name: &syn::Ident,
    trait_ident: &syn::Ident,
    methods: &[MethodInfo],
) -> syn::Result<()> {
    if !input_impl.generics.params.is_empty() {
        return Err(syn::Error::new_spanned(
            &input_impl.generics,
            "a bodiless activation is a contract, whose struct the macro emits generic over its \
             implementor; the impl takes no generics of its own",
        ));
    }
    match &*input_impl.self_ty {
        Type::Path(tp) if tp.qself.is_none() && tp.path.get_ident().is_some() => {}
        other => {
            return Err(syn::Error::new_spanned(
                other,
                "a bodiless activation is a contract: write it on the bare type name \
                 (`impl FooActivation`), whose struct the macro emits",
            ))
        }
    }
    let d = contract_param();
    input_impl.generics = syn::parse_quote!(<#d: #trait_ident>);
    input_impl.self_ty = Box::new(syn::parse_quote!(#struct_name<#d>));
    for item in &mut input_impl.items {
        if let ImplItem::Fn(f) = item {
            if let Some(m) = methods.iter().find(|m| m.fn_name == f.sig.ident) {
                let fn_ident = &m.fn_name;
                let (_, names) = sig_params(m);
                f.block = syn::parse_quote!({
                    <#d as #trait_ident>::#fn_ident(&*self.0 #(, #names)*)
                });
            }
        }
    }
    Ok(())
}

/// Contract form, server half: the struct, its constructors, `Clone` (what
/// `register` asks for, without asking it of the implementor), and
/// `impl<D: Foo> Foo for FooActivation<D>`.
pub(crate) fn contract_items(
    trait_ident: &syn::Ident,
    struct_name: &syn::Ident,
    methods: &[MethodInfo],
    box_stream: &TokenStream,
) -> TokenStream {
    let d = contract_param();
    let doc = format!(
        "The `{trait_ident}` contract served: an activation that delegates every wire method to \
         its implementor. Emitted by `#[plexus_macros::activation]` (MACRO-13) for a bodiless \
         activation; make one with `{trait_ident}::server(x)` or `{struct_name}::new(x)`."
    );
    let items = methods.iter().map(|m| {
        let fn_ident = &m.fn_name;
        let (params, names) = sig_params(m);
        let ev = event_ty(m);
        quote! {
            #[allow(clippy::too_many_arguments)]
            fn #fn_ident(&self #(, #params)*) -> #box_stream<'static, #ev> {
                <#d as #trait_ident>::#fn_ident(&*self.0 #(, #names)*)
            }
        }
    });
    quote! {
        #[doc = #doc]
        pub struct #struct_name<#d: #trait_ident>(::std::sync::Arc<#d>);

        impl<#d: #trait_ident> #struct_name<#d> {
            /// Serve `inner`.
            pub fn new(inner: #d) -> Self {
                Self(::std::sync::Arc::new(inner))
            }

            /// Serve an implementation already shared.
            pub fn from_arc(inner: ::std::sync::Arc<#d>) -> Self {
                Self(inner)
            }

            /// The implementation this activation serves.
            pub fn inner(&self) -> &#d {
                &self.0
            }
        }

        impl<#d: #trait_ident> ::core::clone::Clone for #struct_name<#d> {
            fn clone(&self) -> Self {
                Self(::std::sync::Arc::clone(&self.0))
            }
        }

        impl<#d: #trait_ident> #trait_ident for #struct_name<#d> {
            #(#items)*
        }
    }
}

/// `impl<T: Transport + Clone + Send + Sync + 'static> Foo for FooClient<T>`.
pub(crate) fn client_trait_impl(
    trait_ident: &syn::Ident,
    client_ident: &syn::Ident,
    methods: &[MethodInfo],
    client: &syn::Path,
) -> TokenStream {
    let items = methods.iter().map(|m| {
        let fn_ident = &m.fn_name;
        let (params, _) = sig_params(m);
        let ev = event_ty(m);
        let const_ident = crate::codegen::client::method_const_ident(m);
        let inserts = m.params.iter().map(|p| {
            crate::codegen::client::param_insert_with(p, client, |err| {
                quote! { return #client::error_events::<#ev>(#err); }
            })
        });
        quote! {
            #[allow(clippy::too_many_arguments)]
            fn #fn_ident(&self #(, #params)*) -> #client::BoxStream<'static, #ev> {
                let mut __params = #client::__rt::serde_json::Map::new();
                #(#inserts)*
                #client::into_events::<#ev>(#client::decode::<#ev>(#client::owned_call(
                    ::core::clone::Clone::clone(&self.transport),
                    Self::#const_ident.at(self.path),
                    #client::__rt::serde_json::Value::Object(__params),
                )))
            }
        }
    });
    quote! {
        impl<T> #trait_ident for #client_ident<T>
        where
            T: #client::Transport
                + ::core::clone::Clone
                + ::core::marker::Send
                + ::core::marker::Sync
                + 'static,
        {
            #(#items)*
        }
    }
}

/// `client` in the attribute of an activation the name rule applies to: the
/// client is emitted whenever the crate depends on plexus-client, so the flag
/// says nothing. Accepted for one release, with this warning.
pub(crate) fn client_flag_warning(span: Span, struct_name: &syn::Ident) -> TokenStream {
    let const_name = format_ident!("_PLEXUS_MACROS_DEPRECATED_CLIENT_FLAG_{}", struct_name, span = span);
    let touch = format_ident!("_plexus_macros_touch_deprecated_client_flag_{}", struct_name, span = span);
    quote::quote_spanned! { span =>
        #[doc(hidden)]
        #[allow(non_upper_case_globals, non_snake_case, dead_code)]
        #[deprecated(
            since = "0.6.0",
            note = "plexus-macros (MACRO-13): the `client` flag is ignored — the client is emitted whenever the crate depends on plexus-client. Remove `client` from the attribute; it will be refused in a later release."
        )]
        const #const_name: () = ();

        #[doc(hidden)]
        #[allow(non_snake_case, dead_code)]
        fn #touch() {
            let _ = #const_name;
        }
    }
}

/// The cfg predicate the server half is gated on, parsed.
pub(crate) fn parse_gate(predicate: &str, span: Span) -> syn::Result<syn::Meta> {
    syn::parse_str(predicate).map_err(|e| {
        syn::Error::new(
            span,
            format!(
                "server_cfg = \"...\" must be a `cfg` predicate, e.g. \
                 `feature = \"server\"` or `not(target_arch = \"wasm32\")` ({e})"
            ),
        )
    })
}

#[cfg(test)]
mod tests {
    use super::snake_case;

    #[test]
    fn the_stem_in_snake_case() {
        assert_eq!(snake_case("Echo"), "echo");
        assert_eq!(snake_case("TicketsApi"), "tickets_api");
        assert_eq!(snake_case("HTTPServer"), "http_server");
        assert_eq!(snake_case("Deploy2Driver"), "deploy2_driver");
    }
}