mangadex_api/
http_client.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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#[cfg(all(
    not(feature = "multi-thread"),
    not(feature = "tokio-multi-thread"),
    not(feature = "rw-multi-thread")
))]
use std::cell::RefCell;
#[cfg(all(
    not(feature = "multi-thread"),
    not(feature = "tokio-multi-thread"),
    not(feature = "rw-multi-thread")
))]
use std::rc::Rc;
#[cfg(any(
    feature = "multi-thread",
    feature = "tokio-multi-thread",
    feature = "rw-multi-thread"
))]
use std::sync::Arc;

use derive_builder::Builder;
#[cfg(all(feature = "multi-thread", not(feature = "tokio-multi-thread")))]
use futures::lock::Mutex;
use mangadex_api_schema::v5::oauth::ClientInfo;
use mangadex_api_schema::{ApiResult, Endpoint, FromResponse, Limited, UrlSerdeQS};
use mangadex_api_types::error::Error;
use reqwest::{Client, Response};
use serde::de::DeserializeOwned;
#[cfg(feature = "tokio-multi-thread")]
use tokio::sync::Mutex;
#[cfg(feature = "rw-multi-thread")]
use tokio::sync::RwLock;
use url::Url;

use crate::v5::AuthTokens;
use crate::{API_DEV_URL, API_URL};
use mangadex_api_types::error::Result;

#[cfg(all(
    not(feature = "multi-thread"),
    not(feature = "tokio-multi-thread"),
    not(feature = "rw-multi-thread")
))]
#[cfg_attr(
    docsrs,
    doc(cfg(all(
        not(feature = "multi-thread"),
        not(feature = "tokio-multi-thread"),
        not(feature = "rw-multi-thread")
    )))
)]
pub type HttpClientRef = Rc<RefCell<HttpClient>>;
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "multi-thread", feature = "tokio-multi-thread")))
)]
pub type HttpClientRef = Arc<Mutex<HttpClient>>;
#[cfg(feature = "rw-multi-thread")]
#[cfg_attr(docsrs, doc(cfg(feature = "rw-multi-thread")))]
pub type HttpClientRef = Arc<RwLock<HttpClient>>;

#[derive(Debug, Builder, Clone)]
#[builder(
    setter(into, strip_option),
    default,
    build_fn(error = "mangadex_api_types::error::BuilderError")
)]
#[cfg(not(feature = "oauth"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "oauth"))))]
pub struct HttpClient {
    pub client: Client,
    pub base_url: Url,
    auth_tokens: Option<AuthTokens>,
    captcha: Option<String>,
}

#[derive(Debug, Builder, Clone)]
#[builder(
    setter(into, strip_option),
    default,
    build_fn(error = "mangadex_api_types::error::BuilderError")
)]
#[cfg(feature = "oauth")]
#[cfg_attr(docsrs, doc(cfg(feature = "oauth")))]
pub struct HttpClient {
    pub client: Client,
    pub base_url: Url,
    auth_tokens: Option<AuthTokens>,
    captcha: Option<String>,
    client_info: Option<ClientInfo>,
}

#[cfg(feature = "oauth")]
impl Default for HttpClient {
    fn default() -> Self {
        Self {
            client: crate::get_default_client_api(),
            base_url: Url::parse(API_URL).expect("error parsing the base url"),
            auth_tokens: None,
            captcha: None,
            client_info: None,
        }
    }
}

#[cfg(not(feature = "oauth"))]
impl Default for HttpClient {
    fn default() -> Self {
        Self {
            client: crate::get_default_client_api(),
            base_url: Url::parse(API_URL).expect("error parsing the base url"),
            auth_tokens: None,
            captcha: None,
        }
    }
}

impl HttpClient {
    /// Create a new `HttpClient` with a custom [`reqwest::Client`](https://docs.rs/reqwest/latest/reqwest/struct.Client.html).
    pub fn new(client: Client) -> Self {
        Self {
            client,
            base_url: Url::parse(API_URL).expect("error parsing the base url"),
            ..Default::default()
        }
    }

    /// Get a builder struct to customize the `HttpClient` fields.
    ///
    /// # Examples
    ///
    /// ```
    /// use url::Url;
    ///
    /// use mangadex_api::{MangaDexClient, HttpClient};
    ///
    /// # async fn run() -> anyhow::Result<()> {
    /// let http_client = HttpClient::builder()
    ///     .base_url(Url::parse("127.0.0.1:8000")?)
    ///     .build()?;
    ///
    /// let mangadex_client = MangaDexClient::new_with_http_client(http_client);
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> HttpClientBuilder {
        HttpClientBuilder::default()
            .client(crate::get_default_client_api())
            .base_url(Url::parse(API_URL).expect("error parsing the base url"))
            .clone()
    }

    /// Send the request to the endpoint but don't deserialize the response.
    ///
    /// This is useful to handle things such as response header data for more control over areas
    /// such as rate limiting.
    pub(crate) async fn send_request_without_deserializing_with_other_base_url<E>(
        &self,
        endpoint: &E,
        base_url: &url::Url,
    ) -> Result<reqwest::Response>
    where
        E: Endpoint,
    {
        let mut endpoint_url = base_url.join(&endpoint.path())?;
        if let Some(query) = endpoint.query() {
            endpoint_url = endpoint_url.query_qs(query);
        }

        let mut req = self.client.request(endpoint.method(), endpoint_url);

        if let Some(body) = endpoint.body() {
            req = req.json(body);
        }

        if let Some(multipart) = endpoint.multipart() {
            req = req.multipart(multipart);
        }
        if endpoint.require_auth() {
            let tokens = self.get_tokens().ok_or(Error::MissingTokens)?;
            req = req.bearer_auth(&tokens.session);
        }
        if let Some(captcha) = self.get_captcha() {
            req = req.header("X-Captcha-Result", captcha);
        }

        Ok(req.send().await?)
    }

    /// Send the request to the endpoint but don't deserialize the response.
    ///
    /// This is useful to handle things such as response header data for more control over areas
    /// such as rate limiting.
    pub(crate) async fn send_request_without_deserializing<E>(
        &self,
        endpoint: &E,
    ) -> Result<reqwest::Response>
    where
        E: Endpoint,
    {
        self.send_request_without_deserializing_with_other_base_url(endpoint, &self.base_url)
            .await
    }

    pub(crate) async fn send_request_with_checks<E>(
        &self,
        endpoint: &E,
    ) -> Result<reqwest::Response>
    where
        E: Endpoint,
    {
        let res = self.send_request_without_deserializing(endpoint).await?;

        let status_code = res.status();

        if status_code.as_u16() == 429 {
            return Err(Error::RateLimitExcedeed);
        }

        if status_code.is_server_error() {
            return Err(Error::ServerError(status_code.as_u16(), res.text().await?));
        }
        Ok(res)
    }

    pub(crate) async fn handle_result<T>(&self, res: Response) -> Result<T>
    where
        T: DeserializeOwned,
    {
        /*let res_text = res.text().await?;
        eprintln!("{}", res_text);
        Ok(serde_json::from_str::<ApiResult<T>>(&res_text)
        .map_err(|e| Error::UnexpectedError(anyhow::Error::msg(e.to_string())))?
        .into_result()?)
        */
        Ok(res.json::<ApiResult<T>>().await?.into_result()?)
    }

    /// Send the request to the endpoint and deserialize the response body.
    pub(crate) async fn send_request<E>(&self, endpoint: &E) -> Result<E::Response>
    where
        E: Endpoint,
        <<E as Endpoint>::Response as FromResponse>::Response: DeserializeOwned,
    {
        let res = self.send_request_with_checks(endpoint).await?;

        let res = res
            .json::<<E::Response as FromResponse>::Response>()
            .await?;

        Ok(FromResponse::from_response(res))
    }

    /// Send the request to the endpoint and deserialize the response body.
    #[cfg(not(feature = "serialize"))]
    #[cfg_attr(docsrs, doc(cfg(not(feature = "serialize"))))]
    pub(crate) async fn send_request_with_rate_limit<E>(
        &self,
        endpoint: &E,
    ) -> Result<Limited<E::Response>>
    where
        E: Endpoint,
        <<E as Endpoint>::Response as FromResponse>::Response: DeserializeOwned,
        <E as mangadex_api_schema::Endpoint>::Response: Clone,
    {
        use mangadex_api_types::rate_limit::RateLimit;

        let resp = self.send_request_with_checks(endpoint).await?;

        let some_rate_limit = <RateLimit as TryFrom<&Response>>::try_from(&resp);

        let res = self
            .handle_result::<<E::Response as FromResponse>::Response>(resp)
            .await?;

        Ok(Limited {
            rate_limit: some_rate_limit?,
            body: FromResponse::from_response(res),
        })
    }

    /// Send the request to the endpoint and deserialize the response body.
    #[cfg(feature = "serialize")]
    #[cfg_attr(docsrs, doc(cfg(feature = "serialize")))]
    pub(crate) async fn send_request_with_rate_limit<E>(
        &self,
        endpoint: &E,
    ) -> Result<Limited<E::Response>>
    where
        E: Endpoint,
        <<E as Endpoint>::Response as FromResponse>::Response: DeserializeOwned,
        <E as mangadex_api_schema::Endpoint>::Response: serde::Serialize + Clone,
    {
        use mangadex_api_types::rate_limit::RateLimit;

        let resp = self.send_request_with_checks(endpoint).await?;

        let rate_limit: RateLimit = TryFrom::try_from(&resp)?;

        let res = self
            .handle_result::<<E::Response as FromResponse>::Response>(resp)
            .await?;

        Ok(Limited {
            rate_limit,
            body: FromResponse::from_response(res),
        })
    }

    /// Get the authentication tokens stored in the client.
    pub fn get_tokens(&self) -> Option<&AuthTokens> {
        self.auth_tokens.as_ref()
    }

    /// Set new authentication tokens into the client.
    pub fn set_auth_tokens(&mut self, auth_tokens: &AuthTokens) {
        self.auth_tokens = Some(auth_tokens.clone());
    }

    /// Remove all authentication tokens from the client.
    ///
    /// This is effectively the same as logging out, though will not remove the active session from
    /// the MangaDex server. Be sure to call the logout endpoint to ensure your session is removed.
    pub fn clear_auth_tokens(&mut self) {
        self.auth_tokens = None;
    }

    /// Get the captcha solution stored in the client.
    pub fn get_captcha(&self) -> Option<&String> {
        self.captcha.as_ref()
    }

    /// Set a new captcha solution into the client.
    ///
    /// The code needed for this can be found in the "X-Captcha-Sitekey" header field,
    /// or the `siteKey` parameter in the error context of a 403 response,
    /// `captcha_required_exception` error code.
    pub fn set_captcha<T: Into<String>>(&mut self, captcha: T) {
        self.captcha = Some(captcha.into());
    }

    /// Remove the captcha solution from the client.
    pub fn clear_captcha(&mut self) {
        self.captcha = None;
    }

    #[cfg(feature = "oauth")]
    pub fn set_client_info(&mut self, client_info: &ClientInfo) {
        self.client_info = Some(client_info.clone());
    }

    #[cfg(feature = "oauth")]
    pub fn get_client_info(&self) -> Option<&ClientInfo> {
        self.client_info.as_ref()
    }

    #[cfg(feature = "oauth")]
    #[cfg_attr(docsrs, doc(cfg(feature = "oauth")))]
    pub fn clear_client_info(&mut self) {
        self.client_info = None;
    }

    /// Create a new client of api.mangadex.dev
    #[cfg(not(feature = "oauth"))]
    pub fn api_dev_client() -> Self {
        Self {
            client: Client::new(),
            base_url: Url::parse(API_DEV_URL).expect("error parsing the base url"),
            auth_tokens: None,
            captcha: None,
        }
    }
    #[cfg(feature = "oauth")]
    pub fn api_dev_client() -> Self {
        Self {
            client: Client::new(),
            base_url: Url::parse(API_DEV_URL).expect("error parsing the base url"),
            auth_tokens: None,
            captcha: None,
            client_info: None,
        }
    }
}

/// Helper macros for implementing the send function on the builder
///
/// Introduced in v3.0.0-alpha.1
///
///
macro_rules! builder_send {
    {
        #[$builder:ident] $typ:ty,
        $(#[$out_res:ident])? $out_type:ty
    } => {
        builder_send! { @send $(:$out_res)?, $typ, $out_type }
    };
    { @send, $typ:ty, $out_type:ty } => {
        impl $typ {
            pub async fn send(&self) -> mangadex_api_types::error::Result<$out_type>{
                self.build()?.send().await
            }
        }
    };
    { @send:discard_result, $typ:ty, $out_type:ty } => {
        impl $typ {
            pub async fn send(&self) -> mangadex_api_types::error::Result<()>{
                self.build()?.send().await?;
                Ok(())
            }
        }
    };
    { @send:flatten_result, $typ:ty, $out_type:ty } => {
        impl $typ {
            pub async fn send(&self) -> $out_type{
                self.build()?.send().await
            }
        }
    };
    { @send:rate_limited, $typ:ty, $out_type:ty } => {
        impl $typ {

            pub async fn send(&self) -> mangadex_api_types::error::Result<mangadex_api_schema::Limited<$out_type>>{
                self.build()?.send().await
            }
        }
    };
    { @send:no_send, $typ:ty, $out_type:ty } => {
        impl $typ {
            pub async fn send(&self) -> $out_type{
                self.build()?.send().await
            }
        }
    };
}

/// Helper macro to quickly implement the `Endpoint` trait,
/// and optionally a `send()` method for the input struct.
///
/// The arguments are ordered as follows:
///
/// 1. HTTP method and endpoint path.
/// 2. Input data to serialize unless `no_data` is specified.
/// 3. Response struct to deserialize into.
///
/// with the following format:
///
/// 1. \<HTTP Method\> "\<ENDPOINT PATH\>"
/// 2. \#\[\<ATTRIBUTE\>\] \<INPUT STRUCT\>
/// 3. \#\[\<OPTIONAL ATTRIBUTE\>\] \<OUTPUT STRUCT\>
///
/// The endpoint is specified by the HTTP method, followed by the path. To get a dynamic path
/// based on the input structure, surround the path with parenthesis:
///
/// ```rust, ignore
/// POST ("/account/activate/{}", id)
/// ```
///
/// The format is the same as the `format!()` macro, except `id` will be substituted by `self.id`,
/// where `self` represents an instance of the second parameter.
///
/// The input structure is preceded by an attribute-like structure.
///
/// - `query`: The input structure will be serialized as the query string.
/// - `body`: The input structure will be serialized as a JSON body.
/// - `no_data`: No data will be sent with the request.
/// - `auth`: If this is included, the request will not be made if the user is not authenticated.
///
/// Some examples of valid tags are:
///
/// ```rust, ignore
/// #[query] QueryReq
/// #[body] BodyReq
/// #[query auth] QueryReq
/// #[no_data] QueryStruct
/// ```
///
/// The input structure itself should implement `serde::Serialize` if it is used as a body or query.
///
/// The third argument is the output type, tagged similarly to the input, to modify the behaviour
/// of the generated `send()` method.
///
/// - \<no tag\>: `send()` will simply return `Result<Output>`.
/// - `flatten_result`: If `Output = Result<T>`, the return type will be simplified to `Result<T>`.
/// - `discard_result`: If `Output = Result<T>`, discard `T`, and return `Result<()>`.
/// - `no_send`: Do not implement a `send()` function.
/// - `rate_limited`: `send()` will return `Result<Limited<Output>>`
///
/// # Examples
///
/// ```rust, ignore
/// endpoint! {
///     GET "/path/to/endpoint", // Endpoint.
///     #[query] StructWithData<'_>, // Input data; this example will be serialized as a query string.
///     #[flatten_result] Result<ResponseType> // Response struct; this example will return `Ok(res)` or `Err(e)` instead of `Result<ResponseType>` because of `#[flatten_result]`.
/// }
/// ```
macro_rules! endpoint {
    {
        $method:ident $path:tt,
        #[$payload:ident $($auth:ident)?] $typ:ty,
        $(#[$out_res:ident])? $out:ty
        $(,$builder_ty:ty)?
    } => {
        impl mangadex_api_schema::Endpoint for $typ {
            /// The response type.
            type Response = $out;

            /// Get the method of the request.
            fn method(&self) -> reqwest::Method {
                reqwest::Method::$method
            }

            endpoint! { @path $path }
            endpoint! { @payload $payload }
            // If the `auth` attribute is set, make the request require authentication.
            $(endpoint! { @$auth })?
        }

        endpoint! { @send $(:$out_res)?, $typ, $out $(,$builder_ty)? }

    };

    { @path ($path:expr, $($arg:ident),+) } => {
        /// Get the path of the request.
        fn path(&self) -> std::borrow::Cow<str> {
            std::borrow::Cow::Owned(format!($path, $(self.$arg),+))
        }
    };
    { @path $path:expr } => {
        /// Get the path of the request.
        fn path(&self) -> std::borrow::Cow<str> {
            std::borrow::Cow::Borrowed($path)
        }
    };

    // Set a query string.
    { @payload query } => {
        type Query = Self;
        type Body = ();

        /// Get the query of the request.
        fn query(&self) -> Option<&Self::Query> {
            Some(&self)
        }
    };
    // Set a JSON body.
    { @payload body } => {
        type Query = ();
        type Body = Self;

        /// Get the body of the request.
        fn body(&self) -> Option<&Self::Body> {
            Some(&self)
        }
    };
    // Don't send any additional data with the request.
    { @payload no_data } => {
        type Query = ();
        type Body = ();
    };

    { @auth } => {
        /// Get whether auth is required for this request.
        fn require_auth(&self) -> bool {
            true
        }
    };

    // Return the response as a `Result`.
    { @send, $typ:ty, $out:ty $(,$builder_ty:ty)? } => {
        impl $typ {
            /// Send the request.
            pub async fn send(&self) -> mangadex_api_types::error::Result<$out> {
                #[cfg(all(not(feature = "multi-thread"), not(feature = "tokio-multi-thread"), not(feature = "rw-multi-thread")))]
                {
                    self.http_client.try_borrow()?.send_request(self).await
                }
                #[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
                {
                    self.http_client.lock().await.send_request(self).await
                }
                #[cfg(feature = "rw-multi-thread")]
                {
                    self.http_client.read().await.send_request(self).await
                }
            }
        }

        $(
            builder_send! {
                #[builder] $builder_ty,
                $out
            }
        )?
    };
    // Return the response as a `Result`.
    { @send:rate_limited, $typ:ty, $out:ty $(,$builder_ty:ty)? } => {
        impl $typ {
            /// Send the request.
            pub async fn send(&self) -> mangadex_api_types::error::Result<mangadex_api_schema::Limited<$out>> {
                #[cfg(all(not(feature = "multi-thread"), not(feature = "tokio-multi-thread"), not(feature = "rw-multi-thread")))]
                {
                    self.http_client.try_borrow()?.send_request_with_rate_limit(self).await
                }
                #[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
                {
                    self.http_client.lock().await.send_request_with_rate_limit(self).await
                }
                #[cfg(feature = "rw-multi-thread")]
                {
                    self.http_client.read().await.send_request_with_rate_limit(self).await
                }
            }
        }

        $(
            builder_send! {
                #[builder] $builder_ty,
                #[rate_limited] $out
            }
        )?
    };
    // Return the `Result` variants, `Ok` or `Err`.
    { @send:flatten_result, $typ:ty, $out:ty $(,$builder_ty:ty)? } => {
        impl $typ {
            /// Send the request.
            #[allow(dead_code)]
            pub async fn send(&self) -> $out {
                #[cfg(all(not(feature = "multi-thread"), not(feature = "tokio-multi-thread"), not(feature = "rw-multi-thread")))]
                {
                    self.http_client.try_borrow()?.send_request(self).await?
                }
                #[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
                {
                    self.http_client.lock().await.send_request(self).await?
                }
                #[cfg(feature = "rw-multi-thread")]
                {
                    self.http_client.read().await.send_request(self).await?
                }
            }
        }

        $(
            builder_send! {
                #[builder] $builder_ty,
                #[flatten_result] $out
            }
        )?
    };
    // Don't return any data from the response.
    { @send:discard_result, $typ:ty, $out:ty $(,$builder_ty:ty)? } => {
        impl $typ {
            /// Send the request.
            #[allow(dead_code)]
            pub async fn send(&self) -> mangadex_api_types::error::Result<()> {
                #[cfg(all(not(feature = "multi-thread"), not(feature = "tokio-multi-thread"), not(feature = "rw-multi-thread")))]
                self.http_client.try_borrow()?.send_request(self).await??;
                #[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
                self.http_client.lock().await.send_request(self).await??;
                #[cfg(feature = "rw-multi-thread")]
                self.http_client.read().await.send_request(self).await??;

                Ok(())
            }
        }

        $(
            builder_send! {
                #[builder] $builder_ty,
                #[discard_result] $out
            }
        )?
    };
    // Don't implement `send()` and require manual implementation.
    { @send:no_send, $typ:ty, $out:ty $(,$builder_ty:ty)? } => {
        $(
            builder_send! {
                #[builder] $builder_ty,
                #[no_send] $out
            }
        )?
    };

}

macro_rules! create_endpoint_node {
    {
        #[$name:ident] $sname:ident $tname:ident,
        #[$args:ident] {$($arg_name:ident: $arg_ty:ty,)+},
        #[$methods:ident] {$($func:ident($($farg_name:ident: $farg_ty:ty,)*) -> $output:ty;)*}
    } => {
        #[derive(Debug)]
        pub struct $sname {
            $( $arg_name: $arg_ty, )+
        }
        trait $tname {
            $(
                fn $func(&self, $( $farg_name: $farg_ty, )*) -> $output;
            )*
        }
        impl $sname {
            pub fn new($( $arg_name: $arg_ty, )+) -> Self {
                Self {
                    $( $arg_name, )+
                }
            }
            $(
                pub fn $func(&self, $( $farg_name: $farg_ty, )*) -> $output {
                    <Self as $tname>::$func(&self, $( $farg_name,)*)
                }
            )*
        }
        $(
            impl From<&$sname> for $arg_ty {
                fn from(value: &$sname) -> Self {
                    value.$arg_name.clone()
                }
            }
        )+
    }
}