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
//! Core types used to define the requests and responses for each endpoint in the various
//! [Matrix API specifications][apis].
//!
//! When implementing a new Matrix API, each endpoint has a request type which implements
//! [`IncomingRequest`] and [`OutgoingRequest`], and a response type connected via an associated
//! type.
//!
//! An implementation of [`IncomingRequest`] or [`OutgoingRequest`] contains all the information
//! about the HTTP method, the path and input parameters for requests, and the structure of a
//! successful response. Such types can then be used by client code to make requests, and by server
//! code to fulfill those requests.
//!
//! [apis]: https://spec.matrix.org/latest/#matrix-apis
use ;
use BufMut;
/// Generates [`OutgoingRequest`] and [`IncomingRequest`] implementations.
///
/// The `OutgoingRequest` impl is feature-gated behind `cfg(feature = "client")`.
/// The `IncomingRequest` impl is feature-gated behind `cfg(feature = "server")`.
///
/// The generated code expects the `Request` type to implement [`Metadata`], alongside a
/// `Response` type that implements [`OutgoingResponse`] (for `cfg(feature = "server")`) and /
/// or [`IncomingResponse`] (for `cfg(feature = "client")`).
///
/// The `Content-Type` header of the `OutgoingRequest` is unset for endpoints using the `GET`
/// method, and defaults to `application/json` for all other methods, except if the `raw_body`
/// attribute is set on a field, in which case it defaults to `application/octet-stream`.
///
/// By default, the type this macro is used on gets a `#[non_exhaustive]` attribute. This
/// behavior can be controlled by setting the `ruma_unstable_exhaustive_types` compile-time
/// `cfg` setting as `--cfg=ruma_unstable_exhaustive_types` using `RUSTFLAGS` or
/// `.cargo/config.toml` (under `[build]` -> `rustflags = ["..."]`). When that setting is
/// activated, the attribute is not applied so the type is exhaustive.
///
/// ## Container Attributes
///
/// * `#[request(error = ERROR_TYPE)]`: Override the `EndpointError` associated type of the
/// `OutgoingRequest` and `IncomingRequest` implementations. The default error type is
/// [`MatrixError`](error::MatrixError).
///
/// ## Field Attributes
///
/// To declare which part of the request a field belongs to:
///
/// * `#[ruma_api(header = HEADER_NAME)]`: Fields with this attribute will be treated as HTTP
/// headers on the request. The value must implement `ToString` and `FromStr`. Generally this
/// is a `String`. The attribute value shown above as `HEADER_NAME` must be a `const`
/// expression of the type `http::header::HeaderName`, like one of the constants from
/// `http::header`, e.g. `CONTENT_TYPE`. During deserialization of the request, if the field
/// is an `Option` and parsing the header fails, the error will be ignored and the value will
/// be `None`.
/// * `#[ruma_api(path)]`: Fields with this attribute will be inserted into the matching path
/// component of the request URL. If there are multiple of these fields, the order in which
/// they are declared must match the order in which they occur in the request path.
/// * `#[ruma_api(query)]`: Fields with this attribute will be inserting into the URL's query
/// string.
/// * `#[ruma_api(query_all)]`: Instead of individual query fields, one query_all field, of any
/// type that can be (de)serialized by [serde_html_form], can be used for cases where
/// multiple endpoints should share a query fields type, the query fields are better
/// expressed as an `enum` rather than a `struct`, or the endpoint supports arbitrary query
/// parameters.
/// * No attribute: Fields without an attribute are part of the body. They can use `#[serde]`
/// attributes to customize (de)serialization.
/// * `#[ruma_api(body)]`: Use this if multiple endpoints should share a request body type, or
/// the request body is better expressed as an `enum` rather than a `struct`. The value of
/// the field will be used as the JSON body (rather than being a field in the request body
/// object).
/// * `#[ruma_api(raw_body)]`: Like `body` in that the field annotated with it represents the
/// entire request body, but this attribute is for endpoints where the body can be anything,
/// not just JSON. The field type must be `Vec<u8>`.
///
/// ## Examples
///
/// ```
/// pub mod do_a_thing {
/// use ruma_common::{OwnedRoomId, api::request};
/// # use ruma_common::{api::{auth_scheme::NoAuthentication, response}, metadata};
///
/// // metadata! { ... };
/// # metadata! {
/// # method: POST,
/// # rate_limited: false,
/// # authentication: NoAuthentication,
/// # history: {
/// # unstable => "/_matrix/some/endpoint/{room_id}",
/// # },
/// # }
///
/// #[request]
/// pub struct Request {
/// #[ruma_api(path)]
/// pub room_id: OwnedRoomId,
///
/// #[ruma_api(query)]
/// pub bar: String,
///
/// #[serde(default)]
/// pub foo: String,
/// }
///
/// // #[response]
/// // pub struct Response { ... }
/// # #[response]
/// # pub struct Response {}
/// }
///
/// pub mod upload_file {
/// use http::header::CONTENT_TYPE;
/// use ruma_common::api::request;
/// # use ruma_common::{api::{auth_scheme::NoAuthentication, response}, metadata};
///
/// // metadata! { ... };
/// # metadata! {
/// # method: POST,
/// # rate_limited: false,
/// # authentication: NoAuthentication,
/// # history: {
/// # unstable => "/_matrix/some/endpoint/{file_name}",
/// # },
/// # }
///
/// #[request]
/// pub struct Request {
/// #[ruma_api(path)]
/// pub file_name: String,
///
/// #[ruma_api(header = CONTENT_TYPE)]
/// pub content_type: String,
///
/// #[ruma_api(raw_body)]
/// pub file: Vec<u8>,
/// }
///
/// // #[response]
/// // pub struct Response { ... }
/// # #[response]
/// # pub struct Response {}
/// }
/// ```
///
/// [serde_html_form]: https://crates.io/crates/serde_html_form
pub use request;
/// Generates [`OutgoingResponse`] and [`IncomingResponse`] implementations.
///
/// The `OutgoingResponse` impl is feature-gated behind `cfg(feature = "server")`.
/// The `IncomingResponse` impl is feature-gated behind `cfg(feature = "client")`.
///
/// The `Content-Type` header of the `OutgoingResponse` defaults to `application/json`, except
/// if the `raw_body` attribute is set on a field, in which case it defaults to
/// `application/octet-stream`.
///
/// By default, the type this macro is used on gets a `#[non_exhaustive]` attribute. This
/// behavior can be controlled by setting the `ruma_unstable_exhaustive_types` compile-time
/// `cfg` setting as `--cfg=ruma_unstable_exhaustive_types` using `RUSTFLAGS` or
/// `.cargo/config.toml` (under `[build]` -> `rustflags = ["..."]`). When that setting is
/// activated, the attribute is not applied so the type is exhaustive.
///
/// ## Container Attributes
///
/// * `#[response(error = ERROR_TYPE)]`: Override the `EndpointError` associated type of the
/// `IncomingResponse` implementation. The default error type is
/// [`MatrixError`](error::MatrixError).
/// * `#[response(status = HTTP_STATUS)]`: Override the status code of `OutgoingResponse`.
/// `HTTP_STATUS` must be a status code constant from [`http::StatusCode`], e.g.
/// `IM_A_TEAPOT`. The default status code is [`200 OK`](http::StatusCode::OK);
///
/// ## Field Attributes
///
/// To declare which part of the response a field belongs to:
///
/// * `#[ruma_api(header = HEADER_NAME)]`: Fields with this attribute will be treated as HTTP
/// headers on the response. `HEADER_NAME` must implement
/// `TryInto<http::header::HeaderName>`, this is usually a constant from [`http::header`].
/// The value of the field must implement `ToString` and `FromStr`, this is usually a
/// `String`. During deserialization of the response, if the field is an `Option` and parsing
/// the header fails, the error will be ignored and the value will be `None`.
/// * No attribute: Fields without an attribute are part of the body. They can use `#[serde]`
/// attributes to customize (de)serialization.
/// * `#[ruma_api(body)]`: Use this if multiple endpoints should share a response body type, or
/// the response body is better expressed as an `enum` rather than a `struct`. The value of
/// the field will be used as the JSON body (rather than being a field in the response body
/// object).
/// * `#[ruma_api(raw_body)]`: Like `body` in that the field annotated with it represents the
/// entire response body, but this attribute is for endpoints where the body can be anything,
/// not just JSON. The field type must be `Vec<u8>`.
///
/// ## Examples
///
/// ```
/// pub mod do_a_thing {
/// use ruma_common::{OwnedRoomId, api::response};
/// # use ruma_common::{api::{auth_scheme::NoAuthentication, request}, metadata};
///
/// // metadata! { ... };
/// # metadata! {
/// # method: POST,
/// # rate_limited: false,
/// # authentication: NoAuthentication,
/// # history: {
/// # unstable => "/_matrix/some/endpoint",
/// # },
/// # }
///
/// // #[request]
/// // pub struct Request { ... }
/// # #[request]
/// # pub struct Request { }
///
/// #[response(status = IM_A_TEAPOT)]
/// pub struct Response {
/// #[serde(skip_serializing_if = "Option::is_none")]
/// pub foo: Option<String>,
/// }
/// }
///
/// pub mod download_file {
/// use http::header::CONTENT_TYPE;
/// use ruma_common::api::response;
/// # use ruma_common::{api::{auth_scheme::NoAuthentication, request}, metadata};
///
/// // metadata! { ... };
/// # metadata! {
/// # method: POST,
/// # rate_limited: false,
/// # authentication: NoAuthentication,
/// # history: {
/// # unstable => "/_matrix/some/endpoint",
/// # },
/// # }
///
/// // #[request]
/// // pub struct Request { ... }
/// # #[request]
/// # pub struct Request { }
///
/// #[response]
/// pub struct Response {
/// #[ruma_api(header = CONTENT_TYPE)]
/// pub content_type: String,
///
/// #[ruma_api(raw_body)]
/// pub file: Vec<u8>,
/// }
/// }
/// ```
pub use response;
use ;
use ;
pub use cratemetadata;
use crate::;
pub use ;
/// A request type for a Matrix API endpoint, used for sending requests.
/// A response type for a Matrix API endpoint, used for receiving responses.
/// An extension to [`OutgoingRequest`] which provides Appservice specific methods.
///
/// This is only implemented for implementors of [`AuthScheme`](auth_scheme::AuthScheme) that use a
/// [`SendAccessToken`](auth_scheme::SendAccessToken), because application services should only use
/// these methods with the Client-Server API.
/// A request type for a Matrix API endpoint, used for receiving requests.