ruma_common/api/metadata.rs
1use std::{
2 cmp::Ordering,
3 collections::{BTreeMap, BTreeSet},
4 fmt::Display,
5 str::FromStr,
6};
7
8use bytes::BufMut;
9use http::Method;
10use ruma_macros::StringEnum;
11
12use super::{auth_scheme::AuthScheme, error::UnknownVersionError, path_builder::PathBuilder};
13use crate::{PrivOwnedStr, RoomVersionId, api::error::IntoHttpError, serde::slice_to_buf};
14
15/// Convenient constructor for [`Metadata`] implementation.
16///
17/// ## Definition
18///
19/// By default, `Metadata` is implemented on a type named `Request` that is in scope. This can be
20/// overridden by adding `@for MyType` at the beginning of the declaration.
21///
22/// The rest of the definition of the macro is made to look like a struct, with the following
23/// fields:
24///
25/// * `method` - The HTTP method to use for the endpoint. Its value must be one of the associated
26/// constants of [`http::Method`]. In most cases it should be one of `GET`, `POST`, `PUT` or
27/// `DELETE`.
28/// * `rate_limited` - Whether the endpoint should be rate-limited, according to the specification.
29/// Its value must be a `bool`.
30/// * `authentication` - The type of authentication that is required for the endpoint, according to
31/// the specification. The type must be in scope and implement [`AuthScheme`].
32///
33/// And either of the following fields to define the path(s) of the endpoint.
34///
35/// * `history` - The history of the paths of the endpoint. This should be used for endpoints from
36/// Matrix APIs that have a `/versions` endpoint that returns a list a [`MatrixVersion`]s and
37/// possibly features, like the Client-Server API or the Identity Service API. However, a few
38/// endpoints from those APIs shouldn't use this field because they cannot be versioned, like the
39/// `/versions` or the `/.well-known` endpoints.
40///
41/// Its definition is made to look like match arms and must include at least one arm. The match
42/// arms accept the following syntax:
43///
44/// * `unstable => "unstable/endpoint/path/{variable}"` - An unstable version of the endpoint as
45/// defined in the MSC that adds it, if the MSC does **NOT** define an unstable feature in the
46/// `unstable_features` field of the client-server API's `/versions` endpoint.
47/// * `unstable("org.bar.unstable_feature") => "unstable/endpoint/path/{variable}"` - An unstable
48/// version of the endpoint as defined in the MSC that adds it, if the MSC defines an unstable
49/// feature in the `unstable_features` field of the client-server API's `/versions` endpoint.
50/// * `1.0 | stable("org.bar.feature.stable") => "stable/endpoint/path/{variable}"` - A stable
51/// version of the endpoint as defined in an MSC or the Matrix specification. The match arm can
52/// be a Matrix version, a stable feature, or both separated by `|`.
53///
54/// A stable feature can be defined in an MSC alongside an unstable feature, and can be found in
55/// the `unstable_features` field of the client-server API's `/versions` endpoint. It is meant
56/// to be used by homeservers if they want to declare stable support for a feature before they
57/// can declare support for a whole Matrix version that supports it.
58///
59/// * `1.2 => deprecated` - The Matrix version that deprecated the endpoint, if any. It must be
60/// preceded by a match arm with a stable path and a different Matrix version.
61/// * `1.3 => removed` - The Matrix version that removed the endpoint, if any. It must be preceded
62/// by a match arm with a deprecation and a different Matrix version.
63///
64/// A Matrix version is a `float` representation of the version that looks like `major.minor`.
65/// It must match one of the variants of [`MatrixVersion`]. For example `1.0` matches
66/// [`MatrixVersion::V1_0`], `1.1` matches [`MatrixVersion::V1_1`], etc.
67///
68/// It is expected that the match arms are ordered by descending age. Usually the older unstable
69/// paths would be before the newer unstable paths, then we would find the stable paths, and
70/// finally the deprecation and removal.
71///
72/// The following checks occur at compile time:
73///
74/// * All unstable and stable paths contain the same variables (or lack thereof).
75/// * Matrix versions in match arms are all different and in ascending order.
76///
77/// This field is represented as the [`VersionHistory`](super::path_builder::VersionHistory) type
78/// in the generated implementation.
79/// * `path` - The only path of the endpoint. This should be used for endpoints from Matrix APIs
80/// that do NOT have a `/versions` endpoint that returns a list a [`MatrixVersion`]s, like the
81/// Server-Server API or the Appservice API. It should also be used for endpoints that cannot be
82/// versioned, like the `/versions` or the `/.well-known` endpoints.
83///
84/// Its value must be a static string representing the path, like `"endpoint/path/{variable}"`.
85///
86/// This field is represented as the [`SinglePath`](super::path_builder::SinglePath) type in the
87/// generated implementation.
88///
89/// ## Example
90///
91/// ```
92/// use ruma_common::{
93/// api::auth_scheme::{AccessToken, NoAuthentication},
94/// metadata,
95/// };
96///
97/// /// A Request with a path version history.
98/// pub struct Request {
99/// body: Vec<u8>,
100/// }
101///
102/// metadata! {
103/// method: GET,
104/// rate_limited: true,
105/// authentication: AccessToken,
106///
107/// history: {
108/// unstable => "/_matrix/unstable/org.bar.msc9000/baz",
109/// unstable("org.bar.msc9000.v1") => "/_matrix/unstable/org.bar.msc9000.v1/qux",
110/// 1.0 | stable("org.bar.msc9000.stable") => "/_matrix/media/r0/qux",
111/// 1.1 => "/_matrix/media/v3/qux",
112/// 1.2 => deprecated,
113/// 1.3 => removed,
114/// }
115/// };
116///
117/// /// A request with a single path.
118/// pub struct MySinglePathRequest {
119/// body: Vec<u8>,
120/// }
121///
122/// metadata! {
123/// @for MySinglePathRequest,
124///
125/// method: GET,
126/// rate_limited: false,
127/// authentication: NoAuthentication,
128/// path: "/_matrix/key/query",
129/// };
130/// ```
131#[doc(hidden)]
132#[macro_export]
133macro_rules! metadata {
134 ( @for $request_type:ty, $( $field:ident: $rhs:tt ),+ $(,)? ) => {
135 #[allow(deprecated)]
136 impl $crate::api::Metadata for $request_type {
137 $( $crate::metadata!(@field $field: $rhs); )+
138 }
139 };
140
141 ( $( $field:ident: $rhs:tt ),+ $(,)? ) => {
142 $crate::metadata!{ @for Request, $( $field: $rhs),+ }
143 };
144
145 ( @field method: $method:ident ) => {
146 const METHOD: $crate::exports::http::Method = $crate::exports::http::Method::$method;
147 };
148
149 ( @field rate_limited: $rate_limited:literal ) => { const RATE_LIMITED: bool = $rate_limited; };
150
151 ( @field authentication: $scheme:path ) => {
152 type Authentication = $scheme;
153 };
154
155 ( @field path: $path:literal ) => {
156 type PathBuilder = $crate::api::path_builder::SinglePath;
157 const PATH_BUILDER: $crate::api::path_builder::SinglePath = $crate::api::path_builder::SinglePath::new($path);
158 };
159
160 ( @field history: {
161 $( unstable $(($unstable_feature:literal))? => $unstable_path:literal, )*
162 $( stable ($stable_feature_only:literal) => $stable_feature_path:literal, )*
163 $( $version:literal $(| stable ($stable_feature:literal))? => $stable_rhs:tt, )*
164 } ) => {
165 $crate::metadata! {
166 @history_impl
167 $( unstable $( ($unstable_feature) )? => $unstable_path, )*
168 $( stable ($stable_feature_only) => $stable_feature_path, )*
169 // Flip left and right to avoid macro parsing ambiguities
170 $( $stable_rhs = $version $( | stable ($stable_feature) )?, )*
171 }
172 };
173
174 ( @history_impl
175 $( unstable $(($unstable_feature:literal))? => $unstable_path:literal, )*
176 $( stable ($stable_feature_only:literal) => $stable_feature_path:literal, )*
177 $( $stable_path:literal = $version:literal $(| stable ($stable_feature:literal))?, )*
178 $( deprecated = $deprecated_version:literal, )?
179 $( removed = $removed_version:literal, )?
180 ) => {
181 type PathBuilder = $crate::api::path_builder::VersionHistory;
182 const PATH_BUILDER: $crate::api::path_builder::VersionHistory = $crate::api::path_builder::VersionHistory::new(
183 &[ $(($crate::metadata!(@optional_feature $($unstable_feature)?), $unstable_path)),* ],
184 &[
185 $((
186 $crate::metadata!(@stable_path_selector stable($stable_feature_only)),
187 $stable_feature_path
188 ),)*
189 $((
190 $crate::metadata!(@stable_path_selector $version $( | stable($stable_feature) )?),
191 $stable_path
192 ),)*
193 ],
194 $crate::metadata!(@optional_version $( $deprecated_version )?),
195 $crate::metadata!(@optional_version $( $removed_version )?),
196 );
197 };
198
199 ( @optional_feature ) => { None };
200 ( @optional_feature $feature:literal ) => { Some($feature) };
201 ( @stable_path_selector stable($feature:literal)) => {
202 $crate::api::path_builder::StablePathSelector::Feature($feature)
203 };
204 ( @stable_path_selector $version:literal | stable($feature:literal)) => {
205 $crate::api::path_builder::StablePathSelector::FeatureAndVersion {
206 feature: $feature,
207 version: $crate::api::MatrixVersion::from_lit(stringify!($version)),
208 }
209 };
210 ( @stable_path_selector $version:literal) => {
211 $crate::api::path_builder::StablePathSelector::Version(
212 $crate::api::MatrixVersion::from_lit(stringify!($version))
213 )
214 };
215 ( @optional_version ) => { None };
216 ( @optional_version $version:literal ) => { Some($crate::api::MatrixVersion::from_lit(stringify!($version))) }
217}
218
219/// Metadata about an API endpoint.
220pub trait Metadata: Sized {
221 /// The HTTP method used by this endpoint.
222 const METHOD: Method;
223
224 /// Whether or not this endpoint is rate limited by the server.
225 const RATE_LIMITED: bool;
226
227 /// What authentication scheme the server uses for this endpoint.
228 type Authentication: AuthScheme;
229
230 /// The type used to build an endpoint's path.
231 type PathBuilder: PathBuilder;
232
233 /// All info pertaining to an endpoint's path.
234 const PATH_BUILDER: Self::PathBuilder;
235
236 /// Returns an empty request body for this Matrix request.
237 ///
238 /// For `GET` requests, it returns an entirely empty buffer, for others it returns an empty JSON
239 /// object (`{}`).
240 fn empty_request_body<B>() -> B
241 where
242 B: Default + BufMut,
243 {
244 if Self::METHOD == Method::GET { Default::default() } else { slice_to_buf(b"{}") }
245 }
246
247 /// Generate the endpoint URL for this endpoint.
248 fn make_endpoint_url(
249 path_builder_input: <Self::PathBuilder as PathBuilder>::Input<'_>,
250 base_url: &str,
251 path_args: &[&dyn Display],
252 query_string: &str,
253 ) -> Result<String, IntoHttpError> {
254 Self::PATH_BUILDER.make_endpoint_url(path_builder_input, base_url, path_args, query_string)
255 }
256
257 /// The list of path parameters in the metadata.
258 ///
259 /// Used for `#[test]`s generated by the API macros.
260 #[doc(hidden)]
261 fn _path_parameters() -> Vec<&'static str> {
262 Self::PATH_BUILDER._path_parameters()
263 }
264}
265
266/// The Matrix versions Ruma currently understands to exist.
267///
268/// Matrix, since fall 2021, has a quarterly release schedule, using a global `vX.Y` versioning
269/// scheme. Usually `Y` is bumped for new backwards compatible changes, but `X` can be bumped
270/// instead when a large number of `Y` changes feel deserving of a major version increase.
271///
272/// Every new version denotes stable support for endpoints in a *relatively* backwards-compatible
273/// manner.
274///
275/// Matrix has a deprecation policy, read more about it here: <https://spec.matrix.org/latest/#deprecation-policy>.
276///
277/// Ruma keeps track of when endpoints are added, deprecated, and removed. It'll automatically
278/// select the right endpoint stability variation to use depending on which Matrix versions you
279/// pass to [`try_into_http_request`](super::OutgoingRequest::try_into_http_request), see its
280/// respective documentation for more information.
281///
282/// The `PartialOrd` and `Ord` implementations of this type sort the variants by release date. A
283/// newer release is greater than an older release.
284///
285/// `MatrixVersion::is_superset_of()` is used to keep track of compatibility between versions.
286#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
287#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
288pub enum MatrixVersion {
289 /// Matrix 1.0 was a release prior to the global versioning system and does not correspond to a
290 /// version of the Matrix specification.
291 ///
292 /// It matches the following per-API versions:
293 ///
294 /// * Client-Server API: r0.5.0 to r0.6.1
295 /// * Identity Service API: r0.2.0 to r0.3.0
296 ///
297 /// The other APIs are not supported because they do not have a `GET /versions` endpoint.
298 ///
299 /// See <https://spec.matrix.org/latest/#legacy-versioning>.
300 V1_0,
301
302 /// Version 1.1 of the Matrix specification, released in Q4 2021.
303 ///
304 /// See <https://spec.matrix.org/v1.1/>.
305 V1_1,
306
307 /// Version 1.2 of the Matrix specification, released in Q1 2022.
308 ///
309 /// See <https://spec.matrix.org/v1.2/>.
310 V1_2,
311
312 /// Version 1.3 of the Matrix specification, released in Q2 2022.
313 ///
314 /// See <https://spec.matrix.org/v1.3/>.
315 V1_3,
316
317 /// Version 1.4 of the Matrix specification, released in Q3 2022.
318 ///
319 /// See <https://spec.matrix.org/v1.4/>.
320 V1_4,
321
322 /// Version 1.5 of the Matrix specification, released in Q4 2022.
323 ///
324 /// See <https://spec.matrix.org/v1.5/>.
325 V1_5,
326
327 /// Version 1.6 of the Matrix specification, released in Q1 2023.
328 ///
329 /// See <https://spec.matrix.org/v1.6/>.
330 V1_6,
331
332 /// Version 1.7 of the Matrix specification, released in Q2 2023.
333 ///
334 /// See <https://spec.matrix.org/v1.7/>.
335 V1_7,
336
337 /// Version 1.8 of the Matrix specification, released in Q3 2023.
338 ///
339 /// See <https://spec.matrix.org/v1.8/>.
340 V1_8,
341
342 /// Version 1.9 of the Matrix specification, released in Q4 2023.
343 ///
344 /// See <https://spec.matrix.org/v1.9/>.
345 V1_9,
346
347 /// Version 1.10 of the Matrix specification, released in Q1 2024.
348 ///
349 /// See <https://spec.matrix.org/v1.10/>.
350 V1_10,
351
352 /// Version 1.11 of the Matrix specification, released in Q2 2024.
353 ///
354 /// See <https://spec.matrix.org/v1.11/>.
355 V1_11,
356
357 /// Version 1.12 of the Matrix specification, released in Q3 2024.
358 ///
359 /// See <https://spec.matrix.org/v1.12/>.
360 V1_12,
361
362 /// Version 1.13 of the Matrix specification, released in Q4 2024.
363 ///
364 /// See <https://spec.matrix.org/v1.13/>.
365 V1_13,
366
367 /// Version 1.14 of the Matrix specification, released in Q1 2025.
368 ///
369 /// See <https://spec.matrix.org/v1.14/>.
370 V1_14,
371
372 /// Version 1.15 of the Matrix specification, released in Q2 2025.
373 ///
374 /// See <https://spec.matrix.org/v1.15/>.
375 V1_15,
376
377 /// Version 1.16 of the Matrix specification, released in Q3 2025.
378 ///
379 /// See <https://spec.matrix.org/v1.16/>.
380 V1_16,
381
382 /// Version 1.17 of the Matrix specification, released in Q4 2025.
383 ///
384 /// See <https://spec.matrix.org/v1.17/>.
385 V1_17,
386}
387
388impl TryFrom<&str> for MatrixVersion {
389 type Error = UnknownVersionError;
390
391 fn try_from(value: &str) -> Result<MatrixVersion, Self::Error> {
392 use MatrixVersion::*;
393
394 Ok(match value {
395 // Identity service API versions between Matrix 1.0 and 1.1.
396 // They might match older client-server API versions but that should not be a problem in practice.
397 "r0.2.0" | "r0.2.1" | "r0.3.0" |
398 // Client-server API versions between Matrix 1.0 and 1.1.
399 "r0.5.0" | "r0.6.0" | "r0.6.1" => V1_0,
400 "v1.1" => V1_1,
401 "v1.2" => V1_2,
402 "v1.3" => V1_3,
403 "v1.4" => V1_4,
404 "v1.5" => V1_5,
405 "v1.6" => V1_6,
406 "v1.7" => V1_7,
407 "v1.8" => V1_8,
408 "v1.9" => V1_9,
409 "v1.10" => V1_10,
410 "v1.11" => V1_11,
411 "v1.12" => V1_12,
412 "v1.13" => V1_13,
413 "v1.14" => V1_14,
414 "v1.15" => V1_15,
415 "v1.16" => V1_16,
416 "v1.17" => V1_17,
417 _ => return Err(UnknownVersionError),
418 })
419 }
420}
421
422impl FromStr for MatrixVersion {
423 type Err = UnknownVersionError;
424
425 fn from_str(s: &str) -> Result<Self, Self::Err> {
426 Self::try_from(s)
427 }
428}
429
430impl MatrixVersion {
431 /// Checks whether a version is compatible with another.
432 ///
433 /// Currently, all versions of Matrix are considered backwards compatible with all the previous
434 /// versions, so this is equivalent to `self >= other`. This behaviour may change in the future,
435 /// if a new release is considered to be breaking compatibility with the previous ones.
436 ///
437 /// > ⚠ Matrix has a deprecation policy, and Matrix versioning is not as straightforward as this
438 /// > function makes it out to be. This function only exists to prune breaking changes between
439 /// > versions, and versions too new for `self`.
440 pub fn is_superset_of(self, other: Self) -> bool {
441 self >= other
442 }
443
444 /// Get a string representation of this Matrix version.
445 ///
446 /// This is the string that can be found in the response to one of the `GET /versions`
447 /// endpoints. Parsing this string will give the same variant.
448 ///
449 /// Returns `None` for [`MatrixVersion::V1_0`] because it can match several per-API versions.
450 pub const fn as_str(self) -> Option<&'static str> {
451 let string = match self {
452 MatrixVersion::V1_0 => return None,
453 MatrixVersion::V1_1 => "v1.1",
454 MatrixVersion::V1_2 => "v1.2",
455 MatrixVersion::V1_3 => "v1.3",
456 MatrixVersion::V1_4 => "v1.4",
457 MatrixVersion::V1_5 => "v1.5",
458 MatrixVersion::V1_6 => "v1.6",
459 MatrixVersion::V1_7 => "v1.7",
460 MatrixVersion::V1_8 => "v1.8",
461 MatrixVersion::V1_9 => "v1.9",
462 MatrixVersion::V1_10 => "v1.10",
463 MatrixVersion::V1_11 => "v1.11",
464 MatrixVersion::V1_12 => "v1.12",
465 MatrixVersion::V1_13 => "v1.13",
466 MatrixVersion::V1_14 => "v1.14",
467 MatrixVersion::V1_15 => "v1.15",
468 MatrixVersion::V1_16 => "v1.16",
469 MatrixVersion::V1_17 => "v1.17",
470 };
471
472 Some(string)
473 }
474
475 /// Decompose the Matrix version into its major and minor number.
476 const fn into_parts(self) -> (u8, u8) {
477 match self {
478 MatrixVersion::V1_0 => (1, 0),
479 MatrixVersion::V1_1 => (1, 1),
480 MatrixVersion::V1_2 => (1, 2),
481 MatrixVersion::V1_3 => (1, 3),
482 MatrixVersion::V1_4 => (1, 4),
483 MatrixVersion::V1_5 => (1, 5),
484 MatrixVersion::V1_6 => (1, 6),
485 MatrixVersion::V1_7 => (1, 7),
486 MatrixVersion::V1_8 => (1, 8),
487 MatrixVersion::V1_9 => (1, 9),
488 MatrixVersion::V1_10 => (1, 10),
489 MatrixVersion::V1_11 => (1, 11),
490 MatrixVersion::V1_12 => (1, 12),
491 MatrixVersion::V1_13 => (1, 13),
492 MatrixVersion::V1_14 => (1, 14),
493 MatrixVersion::V1_15 => (1, 15),
494 MatrixVersion::V1_16 => (1, 16),
495 MatrixVersion::V1_17 => (1, 17),
496 }
497 }
498
499 /// Try to turn a pair of (major, minor) version components back into a `MatrixVersion`.
500 const fn from_parts(major: u8, minor: u8) -> Result<Self, UnknownVersionError> {
501 match (major, minor) {
502 (1, 0) => Ok(MatrixVersion::V1_0),
503 (1, 1) => Ok(MatrixVersion::V1_1),
504 (1, 2) => Ok(MatrixVersion::V1_2),
505 (1, 3) => Ok(MatrixVersion::V1_3),
506 (1, 4) => Ok(MatrixVersion::V1_4),
507 (1, 5) => Ok(MatrixVersion::V1_5),
508 (1, 6) => Ok(MatrixVersion::V1_6),
509 (1, 7) => Ok(MatrixVersion::V1_7),
510 (1, 8) => Ok(MatrixVersion::V1_8),
511 (1, 9) => Ok(MatrixVersion::V1_9),
512 (1, 10) => Ok(MatrixVersion::V1_10),
513 (1, 11) => Ok(MatrixVersion::V1_11),
514 (1, 12) => Ok(MatrixVersion::V1_12),
515 (1, 13) => Ok(MatrixVersion::V1_13),
516 (1, 14) => Ok(MatrixVersion::V1_14),
517 (1, 15) => Ok(MatrixVersion::V1_15),
518 (1, 16) => Ok(MatrixVersion::V1_16),
519 (1, 17) => Ok(MatrixVersion::V1_17),
520 _ => Err(UnknownVersionError),
521 }
522 }
523
524 /// Constructor for use by the `metadata!` macro.
525 ///
526 /// Accepts string literals and parses them.
527 #[doc(hidden)]
528 pub const fn from_lit(lit: &'static str) -> Self {
529 use konst::{option, primitive::parse_u8, result, string};
530
531 let major: u8;
532 let minor: u8;
533
534 let mut lit_iter = string::split(lit, ".").next();
535
536 {
537 let (checked_first, checked_split) = option::unwrap!(lit_iter); // First iteration always succeeds
538
539 major = result::unwrap_or_else!(parse_u8(checked_first), |_| panic!(
540 "major version is not a valid number"
541 ));
542
543 lit_iter = checked_split.next();
544 }
545
546 match lit_iter {
547 Some((checked_second, checked_split)) => {
548 minor = result::unwrap_or_else!(parse_u8(checked_second), |_| panic!(
549 "minor version is not a valid number"
550 ));
551
552 lit_iter = checked_split.next();
553 }
554 None => panic!("could not find dot to denote second number"),
555 }
556
557 if lit_iter.is_some() {
558 panic!("version literal contains more than one dot")
559 }
560
561 result::unwrap_or_else!(Self::from_parts(major, minor), |_| panic!(
562 "not a valid version literal"
563 ))
564 }
565
566 // Internal function to do ordering in const-fn contexts
567 pub(super) const fn const_ord(&self, other: &Self) -> Ordering {
568 let self_parts = self.into_parts();
569 let other_parts = other.into_parts();
570
571 use konst::primitive::cmp::cmp_u8;
572
573 let major_ord = cmp_u8(self_parts.0, other_parts.0);
574 if major_ord.is_ne() { major_ord } else { cmp_u8(self_parts.1, other_parts.1) }
575 }
576
577 // Internal function to check if this version is the legacy (v1.0) version in const-fn contexts
578 pub(super) const fn is_legacy(&self) -> bool {
579 let self_parts = self.into_parts();
580
581 use konst::primitive::cmp::cmp_u8;
582
583 cmp_u8(self_parts.0, 1).is_eq() && cmp_u8(self_parts.1, 0).is_eq()
584 }
585
586 /// Get the default [`RoomVersionId`] for this `MatrixVersion`.
587 pub fn default_room_version(&self) -> RoomVersionId {
588 match self {
589 // <https://spec.matrix.org/historical/index.html#complete-list-of-room-versions>
590 MatrixVersion::V1_0
591 // <https://spec.matrix.org/v1.1/rooms/#complete-list-of-room-versions>
592 | MatrixVersion::V1_1
593 // <https://spec.matrix.org/v1.2/rooms/#complete-list-of-room-versions>
594 | MatrixVersion::V1_2 => RoomVersionId::V6,
595 // <https://spec.matrix.org/v1.3/rooms/#complete-list-of-room-versions>
596 MatrixVersion::V1_3
597 // <https://spec.matrix.org/v1.4/rooms/#complete-list-of-room-versions>
598 | MatrixVersion::V1_4
599 // <https://spec.matrix.org/v1.5/rooms/#complete-list-of-room-versions>
600 | MatrixVersion::V1_5 => RoomVersionId::V9,
601 // <https://spec.matrix.org/v1.6/rooms/#complete-list-of-room-versions>
602 MatrixVersion::V1_6
603 // <https://spec.matrix.org/v1.7/rooms/#complete-list-of-room-versions>
604 | MatrixVersion::V1_7
605 // <https://spec.matrix.org/v1.8/rooms/#complete-list-of-room-versions>
606 | MatrixVersion::V1_8
607 // <https://spec.matrix.org/v1.9/rooms/#complete-list-of-room-versions>
608 | MatrixVersion::V1_9
609 // <https://spec.matrix.org/v1.10/rooms/#complete-list-of-room-versions>
610 | MatrixVersion::V1_10
611 // <https://spec.matrix.org/v1.11/rooms/#complete-list-of-room-versions>
612 | MatrixVersion::V1_11
613 // <https://spec.matrix.org/v1.12/rooms/#complete-list-of-room-versions>
614 | MatrixVersion::V1_12
615 // <https://spec.matrix.org/v1.13/rooms/#complete-list-of-room-versions>
616 | MatrixVersion::V1_13 => RoomVersionId::V10,
617 // <https://spec.matrix.org/v1.14/rooms/#complete-list-of-room-versions>
618 | MatrixVersion::V1_14
619 // <https://spec.matrix.org/v1.15/rooms/#complete-list-of-room-versions>
620 | MatrixVersion::V1_15 => RoomVersionId::V11,
621 // <https://spec.matrix.org/v1.16/rooms/#complete-list-of-room-versions>
622 MatrixVersion::V1_16
623 // <https://spec.matrix.org/v1.17/rooms/#complete-list-of-room-versions>
624 | MatrixVersion::V1_17 => RoomVersionId::V12,
625 }
626 }
627}
628
629/// The list of Matrix versions and features supported by a homeserver.
630#[derive(Debug, Clone)]
631#[allow(clippy::exhaustive_structs)]
632pub struct SupportedVersions {
633 /// The Matrix versions that are supported by the homeserver.
634 ///
635 /// This set contains only known versions.
636 pub versions: BTreeSet<MatrixVersion>,
637
638 /// The features that are supported by the homeserver.
639 ///
640 /// This matches the `unstable_features` field of the `/versions` endpoint, without the boolean
641 /// value.
642 pub features: BTreeSet<FeatureFlag>,
643}
644
645impl SupportedVersions {
646 /// Construct a `SupportedVersions` from the parts of a `/versions` response.
647 ///
648 /// Matrix versions that can't be parsed to a `MatrixVersion`, and features with the boolean
649 /// value set to `false` are discarded.
650 pub fn from_parts(versions: &[String], unstable_features: &BTreeMap<String, bool>) -> Self {
651 Self {
652 versions: versions.iter().flat_map(|s| s.parse::<MatrixVersion>()).collect(),
653 features: unstable_features
654 .iter()
655 .filter(|(_, enabled)| **enabled)
656 .map(|(feature, _)| feature.as_str().into())
657 .collect(),
658 }
659 }
660}
661
662/// The Matrix features supported by Ruma.
663///
664/// Features that are not behind a cargo feature are features that are part of the Matrix
665/// specification and that Ruma still supports, like the unstable version of an endpoint or a stable
666/// feature. Features behind a cargo feature are only supported when this feature is enabled.
667#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
668#[derive(Clone, StringEnum, Hash)]
669#[non_exhaustive]
670pub enum FeatureFlag {
671 /// `fi.mau.msc2246` ([MSC])
672 ///
673 /// Asynchronous media uploads.
674 ///
675 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/2246
676 #[ruma_enum(rename = "fi.mau.msc2246")]
677 Msc2246,
678
679 /// `org.matrix.msc2432` ([MSC])
680 ///
681 /// Updated semantics for publishing room aliases.
682 ///
683 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/2432
684 #[ruma_enum(rename = "org.matrix.msc2432")]
685 Msc2432,
686
687 /// `fi.mau.msc2659` ([MSC])
688 ///
689 /// Application service ping endpoint.
690 ///
691 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/2659
692 #[ruma_enum(rename = "fi.mau.msc2659")]
693 Msc2659,
694
695 /// `fi.mau.msc2659` ([MSC])
696 ///
697 /// Stable version of the application service ping endpoint.
698 ///
699 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/2659
700 #[ruma_enum(rename = "fi.mau.msc2659.stable")]
701 Msc2659Stable,
702
703 /// `uk.half-shot.msc2666.query_mutual_rooms` ([MSC])
704 ///
705 /// Get rooms in common with another user.
706 ///
707 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/2666
708 #[cfg(feature = "unstable-msc2666")]
709 #[ruma_enum(rename = "uk.half-shot.msc2666.query_mutual_rooms")]
710 Msc2666,
711
712 /// `org.matrix.msc3030` ([MSC])
713 ///
714 /// Jump to date API endpoint.
715 ///
716 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3030
717 #[ruma_enum(rename = "org.matrix.msc3030")]
718 Msc3030,
719
720 /// `org.matrix.msc3882` ([MSC])
721 ///
722 /// Allow an existing session to sign in a new session.
723 ///
724 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3882
725 #[ruma_enum(rename = "org.matrix.msc3882")]
726 Msc3882,
727
728 /// `org.matrix.msc3916` ([MSC])
729 ///
730 /// Authentication for media.
731 ///
732 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916
733 #[ruma_enum(rename = "org.matrix.msc3916")]
734 Msc3916,
735
736 /// `org.matrix.msc3916.stable` ([MSC])
737 ///
738 /// Stable version of authentication for media.
739 ///
740 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916
741 #[ruma_enum(rename = "org.matrix.msc3916.stable")]
742 Msc3916Stable,
743
744 /// `org.matrix.msc4108` ([MSC])
745 ///
746 /// Mechanism to allow OIDC sign in and E2EE set up via QR code.
747 ///
748 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4108
749 #[cfg(feature = "unstable-msc4108")]
750 #[ruma_enum(rename = "org.matrix.msc4108")]
751 Msc4108,
752
753 /// `org.matrix.msc4140` ([MSC])
754 ///
755 /// Delayed events.
756 ///
757 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4140
758 #[cfg(feature = "unstable-msc4140")]
759 #[ruma_enum(rename = "org.matrix.msc4140")]
760 Msc4140,
761
762 /// `org.matrix.simplified_msc3575` ([MSC])
763 ///
764 /// Simplified Sliding Sync.
765 ///
766 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4186
767 #[cfg(feature = "unstable-msc4186")]
768 #[ruma_enum(rename = "org.matrix.simplified_msc3575")]
769 Msc4186,
770
771 /// `org.matrix.msc4380_invite_permission_config` ([MSC])
772 ///
773 /// Invite Blocking.
774 ///
775 /// [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4380
776 #[cfg(feature = "unstable-msc4380")]
777 #[ruma_enum(rename = "org.matrix.msc4380")]
778 Msc4380,
779
780 #[doc(hidden)]
781 _Custom(PrivOwnedStr),
782}