ruma_client_api/discovery/
get_supported_versions.rs
1use std::collections::{BTreeMap, BTreeSet};
8
9use ruma_common::{
10 api::{request, response, MatrixVersion, Metadata},
11 metadata,
12};
13
14const METADATA: Metadata = metadata! {
15 method: GET,
16 rate_limited: false,
17 authentication: AccessTokenOptional,
18 history: {
19 1.0 => "/_matrix/client/versions",
20 }
21};
22
23#[request(error = crate::Error)]
25#[derive(Default)]
26pub struct Request {}
27
28#[response(error = crate::Error)]
30pub struct Response {
31 pub versions: Vec<String>,
33
34 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
39 pub unstable_features: BTreeMap<String, bool>,
40}
41
42impl Request {
43 pub fn new() -> Self {
45 Self {}
46 }
47}
48
49impl Response {
50 pub fn new(versions: Vec<String>) -> Self {
52 Self { versions, unstable_features: BTreeMap::new() }
53 }
54
55 pub fn known_versions(&self) -> BTreeSet<MatrixVersion> {
63 self.versions
64 .iter()
65 .flat_map(|s| s.parse::<MatrixVersion>())
67 .collect()
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use std::collections::BTreeSet;
75
76 use ruma_common::api::MatrixVersion;
77
78 use super::Response;
79
80 #[test]
81 fn known_versions() {
82 let none = Response::new(vec![]);
83 assert_eq!(none.known_versions(), BTreeSet::new());
84
85 let single_known = Response::new(vec!["r0.6.0".to_owned()]);
86 assert_eq!(single_known.known_versions(), BTreeSet::from([MatrixVersion::V1_0]));
87
88 let single_unknown = Response::new(vec!["v0.0".to_owned()]);
89 assert_eq!(single_unknown.known_versions(), BTreeSet::new());
90 }
91
92 #[test]
93 fn known_versions_order() {
94 let sorted = Response::new(vec![
95 "r0.0.1".to_owned(),
96 "r0.5.0".to_owned(),
97 "r0.6.0".to_owned(),
98 "r0.6.1".to_owned(),
99 "v1.1".to_owned(),
100 "v1.2".to_owned(),
101 ]);
102 assert_eq!(
103 sorted.known_versions(),
104 BTreeSet::from([MatrixVersion::V1_0, MatrixVersion::V1_1, MatrixVersion::V1_2])
105 );
106
107 let sorted_reverse = Response::new(vec![
108 "v1.2".to_owned(),
109 "v1.1".to_owned(),
110 "r0.6.1".to_owned(),
111 "r0.6.0".to_owned(),
112 "r0.5.0".to_owned(),
113 "r0.0.1".to_owned(),
114 ]);
115 assert_eq!(
116 sorted_reverse.known_versions(),
117 BTreeSet::from([MatrixVersion::V1_0, MatrixVersion::V1_1, MatrixVersion::V1_2])
118 );
119
120 let random_order = Response::new(vec![
121 "v1.1".to_owned(),
122 "r0.6.1".to_owned(),
123 "r0.5.0".to_owned(),
124 "r0.6.0".to_owned(),
125 "r0.0.1".to_owned(),
126 "v1.2".to_owned(),
127 ]);
128 assert_eq!(
129 random_order.known_versions(),
130 BTreeSet::from([MatrixVersion::V1_0, MatrixVersion::V1_1, MatrixVersion::V1_2])
131 );
132 }
133}